【问题标题】:How do I store gzipped files using PigStorage in Apache Pig?如何在 Apache Pig 中使用 PigStorage 存储 gzip 文件?
【发布时间】:2011-06-25 12:38:27
【问题描述】:

Apache Pig v0.7 可以轻松读取 gzip 压缩文件,例如:

MyData = LOAD '/tmp/data.csv.gz' USING PigStorage(',') AS (timestamp, user, url);

我可以处理该数据并将其输出到磁盘,好吧:

PerUser = GROUP MyData BY user;
UserCount = FOREACH PerUser GENERATE group AS user, COUNT(MyData) AS count;
STORE UserCount INTO '/tmp/usercount' USING PigStorage(',');

但是输出文件没有被压缩:

/tmp/usercount/part-r-00000

有没有办法告诉STORE 命令以gzip 格式输出内容?请注意,理想情况下,我想要一个适用于 Pig 0.6 的答案,因为我希望使用 Amazon Elastic MapReduce;但如果有任何版本的 Pig 的解决方案,我想听听。

【问题讨论】:

    标签: apache-pig


    【解决方案1】:

    对于 Pig r0.8.0,答案很简单,只需为输出路径提供“.gz”扩展名(如果您更喜欢 bzip,则为“.bz”)。

    您的代码的最后一行应修改为:

    STORE UserCount INTO '/tmp/usercount.gz' USING PigStorage(',');
    

    根据您的示例,您的输出文件将被找到为

    /tmp/usercount.gz/part-r-00000.gz
    

    欲了解更多信息,请参阅:https://pig.apache.org/docs/r0.8.1/piglatin_ref2.html#PigStorage

    【讨论】:

    • 很好的答案。不幸的是,Amazon Elastic Map-Reduce 仅支持 Pig v0.6。
    • 仅供参考:EMR 目前默认运行的是 Pig 版本 0.9.2,所以现在应该可以使用了。
    【解决方案2】:

    有两种方式:

    1. 如上所述,在存储中你可以说输出目录为

      usercount.gz STORE UserCount INTO '/tmp/usercount.gz' USING PigStorage(',');

    2. 在脚本中设置压缩方法。

      set output.compression.enabled true; set output.compression.codec org.apache.hadoop.io.compress.GzipCodec;

    【讨论】:

      【解决方案3】:

      根据 PigStorage 的 Pig 文档,有两种方法可以做到这一点

      使用“STORE”语句指定压缩格式

      STORE UserCount INTO '/tmp/usercount.gz' USING PigStorage(',');
      STORE UserCount INTO '/tmp/usercount.bz2' USING PigStorage(',');
      STORE UserCount INTO '/tmp/usercount.lzo' USING PigStorage(',');
      

      请注意上述陈述。 Pig 支持 3 种压缩格式,即 GZip、BZip2 和 LZO。为了让 LZO 工作,您必须单独安装它。有关 lzo 的更多信息,请参阅here

      通过作业属性指定压缩

      通过在您的猪脚本中设置以下属性,即output.compression.enabledoutput.compression.codec 通过以下代码

      set output.compression.enabled true;
      

      set output.compression.codec com.hadoop.compression.lzo.LzopCodec;
      set output.compression.codec org.apache.hadoop.io.compress.GzipCodec;
      set output.compression.codec org.apache.hadoop.io.compress.BZip2Codec;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-09
        • 2016-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-21
        相关资源
        最近更新 更多