【问题标题】:How to set avro compression codec for mapper output?如何为映射器输出设置 avro 压缩编解码器?
【发布时间】:2018-12-23 16:53:52
【问题描述】:

hadoop MR任务中使用Avro作为mapper的输出格式时,如何设置压缩编解码器?

"mapred" API提供了这个方法:

org.apache.avro.mapred.AvroJob.setOutputCodec(JobConf job, String codec)

但是,较新的 "mapreduce" API 中没有。如何在较新的“mapreduce”API 中设置编解码器?

我天真地尝试使用作业配置来设置编解码器但没有成功:

public int run(String[] args) throws Exception {
  [..]
  Job job = new Job(getConf());
  job.setJarByClass(MapReduceExample.class);
  job.setJobName("MRExample");
  // hm .. this doesn't seem to do work, output still has "null" codec
  job.getConfiguration().set(AvroJob.CONF_OUTPUT_CODEC,
                             CodecFactory.deflateCodec(6).toString());

  job.setMapperClass(ExampleMapper.class);
  [..]
  AvroJob.setMapOutputKeySchema(job, Schema.create(Schema.Type.STRING));
  AvroJob.setMapOutputValueSchema(job, Schema.create(Schema.Type.BYTES));
  // here I was hoping to use something like
  // AvroJob.setMapOutputCodec(job, "deflate")

  [..]
  return (job.waitForCompletion(true) ? 0 : 1);
}

当我用 python 打开生成的 avro 时

>>> from avro.datafile import DataFileReader
>>> from avro.io import DatumReader
>>> av_fh = open("output/part-r-00000.avro", "rb")
>>> av_rd = DataFileReader(av_fh, DatumReader())
>>> av_rd.codec
'null'

【问题讨论】:

    标签: hadoop mapreduce compression avro codec


    【解决方案1】:

    当我更改以下行时它会起作用

    job.getConfiguration().set(AvroJob.CONF_OUTPUT_CODEC,
                               CodecFactory.deflateCodec(6).toString());
    

    FileOutputFormat.setCompressOutput(job, true);
    job.getConfiguration().set(AvroJob.CONF_OUTPUT_CODEC,
                               DataFileConstants.DEFLATE_CODEC);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多