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

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

内容简介:如何用Apache Pig输出压缩格式的SequenceFile
转载须注明出处: 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压缩类型,默认压缩编码的文件。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

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

Web 2.0 Heroes

Web 2.0 Heroes

Bradley L. Jones / Wiley / 2008-04-14 / USD 24.99

Web 2.0 may be an elusive concept, but one thing is certain: using the Web as merely a means of retrieving and displaying information is history. Today?s Web is immediate, interactive, innovative. It ......一起来看看 《Web 2.0 Heroes》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器