如何用Apache Pig输出压缩格式的SequenceFile

栏目: Apache · 发布时间: 6年前

内容简介:如果你要用Apache Pig读取这种类型的数据,可以用 PiggyBank 中的SequenceFileLoader——我没有用过,但肯定是没问题的。但是,如果你保存在SequenceFile中的key或value是幸好我们有Twitter,它已经帮我们做好了这个工作。利用其开源的
转载须注明出处: http://www.codelast.com/

SequenceFile是Hadoop API提供的一种二进制文件,它将数据以<key,value>的形式序列化到文件中。

如果你要用Apache Pig读取这种类型的数据,可以用 PiggyBank 中的SequenceFileLoader——我没有用过,但肯定是没问题的。

但是,如果你保存在SequenceFile中的key或value是 ThriftWritable 类型的数据,那么,要用Pig来 load & store 这种数据,就不那么容易了。

幸好我们有Twitter,它已经帮我们做好了这个工作。利用其开源的 Elephant Bird ,我们可以轻松做到这一点。

Elephant Bird 中的 SequenceFileLoader 以及 SequenceFileStorage 就是用来干这个的。

例如,load数据的做法是:

A = LOAD 'xxx' USING com.twitter.elephantbird.pig.load.SequenceFileLoader(
 '-c com.mediav.elephantbird.pig.util.BooleanWritableConverter',
 '-c com.twitter.elephantbird.pig.util.ThriftWritableConverter com.codelast.MyThriftClass');

其中,这份SequenceFile的key是BooleanWritable类型,value是ThriftWritable类型,它对应的Thrift类是MyThriftClass,这是一个自定义的Thrift class。

文章来源: http://www.codelast.com/

store 数据的做法是:

STORE B INTO 'xxx' USING com.twitter.elephantbird.pig.store.SequenceFileStorage(
 '-c com.mediav.elephantbird.pig.util.BooleanWritableConverter',
 '-c com.twitter.elephantbird.pig.util.ThriftWritableConverter com.codelast.MyThriftClass');

其中,对key和value的说明和上面一样。

这样,就可以实现加载以及存储SequenceFile了。

文章来源: http://www.codelast.com/

但是你会发现,这样输出的SequenceFile是未压缩的,所以文件体积比较大。如果要压缩,该怎么做呢?

答案就是在Pig脚本中添加以下几句话就OK了:

SET output.compression.enabled 'true';
SET mapreduce.output.fileoutputformat.compress.type 'BLOCK';
SET output.compression.codec 'org.apache.hadoop.io.compress.DefaultCodec';

这会使得输出的SequenceFile是BLOCK压缩类型,默认压缩编码的文件。


以上所述就是小编给大家介绍的《如何用Apache Pig输出压缩格式的SequenceFile》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Using Google App Engine

Using Google App Engine

Charles Severance / O'Reilly Media / 2009-5-23 / USD 29.99

With this book, you can build exciting, scalable web applications quickly and confidently, using Google App Engine - even if you have little or no experience in programming or web development. App Eng......一起来看看 《Using Google App Engine》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具