目录

一、概述

二、MR⽀持的压缩编码 

三、Reducer输出压缩

四、Mapper输出压缩

五、压缩⽂件的读取


一、概述


        这是MapReduce 的⼀种优化策略:通过压缩编码对 mapper 或者 reducer 的输出进⾏压缩,以减少磁盘IO ,提⾼ MR 程序运⾏速度(但相应增加了 cpu 运算负担)



1) MapReduce ⽀持将 map 输出的结果或者 reduce 输出的结果进⾏压缩,以减少⽹络 IO或最终输出数据的体积


2) 压缩特性运⽤得当能提⾼性能,但运⽤不当也可能降低性能


3) 基本原则:

        -运算密集型的 job ,少⽤压缩

        -IO密集型的 job ,多⽤压缩


二、MR⽀持的压缩编码

hadoop继任者_hadoop继任者


考虑 Hadoop 应⽤处理的数据集⽐较⼤,因此需要借助压缩。下⾯是按照效率从⾼到低排列的



( 1 )使⽤容器格式⽂件,例如:顺序⽂件、 RCFile 、 Avro 数据格式⽀持压缩和切分⽂件。另外在配合使⽤⼀些快速压缩⼯具,例如:LZO 、 LZ4 或者 Snappy.


( 2 )使⽤⽀持切分压缩格式,例如 bzip2


( 3 )在应⽤中将⽂件切分成块,对每块进⾏任意格式压缩。这种情况确保压缩后的数据接近HDFS 块⼤⼩。


( 4 )存储未压缩⽂件,以原始⽂件存储。


三、Reducer输出压缩


在配置参数或在代码中都可以设置 reduce 的输出压缩



1) 在配置参数中设置




        压缩属性:MapReduce.output.fileoutputformat.compress 设置为 true




        压缩格式属性:MapReduce.output.fileoutputformat.compress.codec




        类库有:org.apache.hadoop.io.compress.DefaultCodec .deflate



                      org.apache.hadoop.io.compress.GzipCodec .gz



        压缩类型属性:




                      MapReduce.output.fileoutputformat.compress.type=RECORD




2) 也可以在代码中⽤




conf.set(“MapReduce.output.fileoutputformat.compress”,”true”)



3) 在代码中设置




Job job = Job.getInstance(conf);
FileOutputFormat.setCompressOutput(job, true);
FileOutputFormat.setOutputCompressorClass(job, (Class<? extends CompressionCodec>) Class.forName(""));

四、Mapper输出压缩



在配置参数或在代码中都可以设置 reduce 的输出压缩




1) 在配置参数中设置




MapReduce.map.output.compress=true




MapReduce.map.output.compress.codec=org.apache.hadoop.io.compress .DefaultCodec






2) 在代码中设置

conf.set(“MapReduce.map.output.compress”,” true”)
conf.setBoolean(Job.MAP_OUTPUT_COMPRESS, true);
conf.setClass(Job.MAP_OUTPUT_COMPRESS_CODEC, GzipCodec.class,CompressionCodec.class);

五、压缩⽂件的读取



Hadoop ⾃带的 InputFormat 类内置⽀持压缩⽂件的读取,⽐如 TextInputformat类,在其initialize ⽅法中:




public void initialize(InputSplit genericSplit,TaskAttemptContext context) throws IOException {
    FileSplit split = (FileSplit) genericSplit;
    Configuration job = context.getConfiguration();
    this.maxLineLength = job.getInt(MAX_LINE_LENGTH,Integer.MAX_VALUE);
    start = split.getStart();
    end = start + split.getLength();
    final Path file = split.getPath();
    // open the file and seek to the start of the split
    final FileSystem fs = file.getFileSystem(job);
    fileIn = fs.open(file);
    //根据⽂件后缀名创建相应压缩编码的codec
    CompressionCodec codec = new CompressionCodecFactory(job).getCodec(file);
    if (null!=codec) {
        isCompressedInput = true; 
        decompressor = CodecPool.getDecompressor(codec);
        //判断是否属于可切⽚压缩编码类型
        if (codec instanceof SplittableCompressionCodec) {
            final SplitCompressionInputStream cIn =((SplittableCompressionCodec)codec).createInputStream(fileIn, decompressor, start, end,SplittableCompressionCodec.READ_MODE.BYBLOCK);
            //如果是可切⽚压缩编码,则创建⼀个CompressedSplitLineReader读取压缩数据
            in = new CompressedSplitLineReader(cIn, job,this.recordDelimiterBytes);
            start = cIn.getAdjustedStart();
            end = cIn.getAdjustedEnd();
            filePosition = cIn;
        } else {
            //如果是不可切⽚压缩编码,则创建⼀个SplitLineReader读取压缩数据,并将⽂件输⼊流转换成解压数据流传递给普通SplitLineReader读取
            in = new SplitLineReader(codec.createInputStream(fileIn,decompressor), job, this.recordDelimiterBytes);
            filePosition = fileIn;
        }
    } else {
        fileIn.seek(start);
        //如果不是压缩⽂件,则创建普通SplitLineReader读取数据
        in = new SplitLineReader(fileIn, job,this.recordDelimiterBytes);
        filePosition = fileIn;
    }
}