概述

1)压缩的好处和坏处

压缩的优点:以减少磁盘IO、减少磁盘存储空间。
压缩的缺点:增加CPU开销。

2)压缩原则

(1)运算密集型的Job,少用压缩
(2)IO密集型的Job,多用压缩

MR支持的压缩编码

1)压缩算法对比介绍

压缩格式 Hadoop自带? 算法 文件扩展名 是否可切片 换成压缩格式后,原来的程序是否需要修改
DEFLATE 是,直接使用 DEFLATE .deflate 和文本处理一样,不需要修改
Gzip 是,直接使用 DEFLATE .gz 和文本处理一样,不需要修改
bzip2 是,直接使用 bzip2 .bz2 和文本处理一样,不需要修改
LZO 否,需要安装 LZO .lzo 需要建索引,还需要指定输入格式
Snappy 是,直接使用 Snappy .snappy 和文本处理一样,不需要修改

2)压缩性能的比较

压缩算法 原始文件大小 压缩文件大小 压缩速度 解压速度
gzip 8.3GB 1.8GB 17.5MB/s 58MB/s
bzip2 8.3GB 1.1GB 2.4MB/s 9.5MB/s
LZO 8.3GB 2.9GB 49.3MB/s 74.6MB/s

压缩方式选择

压缩方式选择时重点考虑:压缩/解压缩速度、压缩率(压缩后存储大小)、压缩后是否可以支持切片。

Gzip压缩

优点:压缩率比较高;
缺点:不支持Split;压缩/解压速度一般;

Bzip2压缩

优点:压缩率高;支持Split;
缺点:压缩/解压速度慢。

Lzo压缩

优点:压缩/解压速度比较快;支持Split;
缺点:压缩率一般;想支持切片需要额外创建索引。

Snappy压缩

优点:压缩和解压缩速度快;
缺点:不支持Split;压缩率一般;

压缩位置选择

压缩可以在MapReduce作用的任意阶段启用。
image.png

压缩参数配置

1 为了支持多种压缩/解压缩算法,Hadoop引入了编码/解码器

压缩格式 对应的编码/解码器
DEFLATE org.apache.hadoop.io.compress.DefaultCodec
gzip org.apache.hadoop.io.compress.GzipCodec
bzip2 org.apache.hadoop.io.compress.BZip2Codec
LZO com.hadoop.compression.lzo.LzopCodec
Snappy org.apache.hadoop.io.compress.SnappyCodec

2 要在Hadoop中启用压缩,可以配置如下参数

参数 默认值 阶段 建议
io.compression.codecs
(在core-site.xml中配置)
无,这个需要在命令行输入hadoop checknative查看 输入压缩 Hadoop使用文件扩展名判断是否支持某种编解码器
mapreduce.map.output.compress(在mapred-site.xml中配置) false mapper输出 这个参数设为true启用压缩
mapreduce.map.output.compress.codec(在mapred-site.xml中配置) org.apache.hadoop.io.compress.DefaultCodec mapper输出 企业多使用LZO或Snappy编解码器在此阶段压缩数据
mapreduce.output.fileoutputformat.compress(在mapred-site.xml中配置) false reducer输出 这个参数设为true启用压缩
mapreduce.output.fileoutputformat.compress.codec(在mapred-site.xml中配置) org.apache.hadoop.io.compress.DefaultCodec reducer输出 使用标准工具或者编解码器,如gzip和bzip2

压缩实操案例

Map输出端采用压缩

即使你的MapReduce的输入输出文件都是未压缩的文件,你仍然可以对Map任务的中间结果输出做压缩,因为它要写在硬盘并且通过网络传输到Reduce节点,对其压缩可以提高很多性能,这些工作只要设置两个属性即可,我们来看下代码怎么设置。

Hadoop源码支持的压缩格式有:BZip2Codec、DefaultCodec

  1. import java.io.IOException;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.fs.Path;
  4. import org.apache.hadoop.io.IntWritable;
  5. import org.apache.hadoop.io.Text;
  6. import org.apache.hadoop.io.compress.BZip2Codec;
  7. import org.apache.hadoop.io.compress.CompressionCodec;
  8. import org.apache.hadoop.io.compress.GzipCodec;
  9. import org.apache.hadoop.mapreduce.Job;
  10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  11. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  12. public class WordCountDriver {
  13. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
  14. Configuration conf = new Configuration();
  15. // 开启map端输出压缩
  16. conf.setBoolean("mapreduce.map.output.compress", true);
  17. // 设置map端输出压缩方式
  18. conf.setClass("mapreduce.map.output.compress.codec", BZip2Codec.class,CompressionCodec.class);
  19. Job job = Job.getInstance(conf);
  20. job.setJarByClass(WordCountDriver.class);
  21. job.setMapperClass(WordCountMapper.class);
  22. job.setReducerClass(WordCountReducer.class);
  23. job.setMapOutputKeyClass(Text.class);
  24. job.setMapOutputValueClass(IntWritable.class);
  25. job.setOutputKeyClass(Text.class);
  26. job.setOutputValueClass(IntWritable.class);
  27. FileInputFormat.setInputPaths(job, new Path(args[0]));
  28. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  29. boolean result = job.waitForCompletion(true);
  30. System.exit(result ? 0 : 1);
  31. }
  32. }

Mapper保持不变

  1. import java.io.IOException;
  2. import org.apache.hadoop.io.IntWritable;
  3. import org.apache.hadoop.io.LongWritable;
  4. import org.apache.hadoop.io.Text;
  5. import org.apache.hadoop.mapreduce.Mapper;
  6. public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
  7. Text k = new Text();
  8. IntWritable v = new IntWritable(1);
  9. @Override
  10. protected void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException {
  11. // 1 获取一行
  12. String line = value.toString();
  13. // 2 切割
  14. String[] words = line.split(" ");
  15. // 3 循环写出
  16. for(String word:words){
  17. k.set(word);
  18. context.write(k, v);
  19. }
  20. }
  21. }

Reducer保持不变

  1. import java.io.IOException;
  2. import org.apache.hadoop.io.IntWritable;
  3. import org.apache.hadoop.io.Text;
  4. import org.apache.hadoop.mapreduce.Reducer;
  5. public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
  6. IntWritable v = new IntWritable();
  7. @Override
  8. protected void reduce(Text key, Iterable<IntWritable> values,
  9. Context context) throws IOException, InterruptedException {
  10. int sum = 0;
  11. // 1 汇总
  12. for(IntWritable value:values){
  13. sum += value.get();
  14. }
  15. v.set(sum);
  16. // 2 输出
  17. context.write(key, v);
  18. }
  19. }

Reduce输出端采用压缩

基于WordCount案例处理。

修改驱动

  1. import java.io.IOException;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.fs.Path;
  4. import org.apache.hadoop.io.IntWritable;
  5. import org.apache.hadoop.io.Text;
  6. import org.apache.hadoop.io.compress.BZip2Codec;
  7. import org.apache.hadoop.io.compress.DefaultCodec;
  8. import org.apache.hadoop.io.compress.GzipCodec;
  9. import org.apache.hadoop.io.compress.Lz4Codec;
  10. import org.apache.hadoop.io.compress.SnappyCodec;
  11. import org.apache.hadoop.mapreduce.Job;
  12. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  13. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  14. public class WordCountDriver {
  15. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
  16. Configuration conf = new Configuration();
  17. Job job = Job.getInstance(conf);
  18. job.setJarByClass(WordCountDriver.class);
  19. job.setMapperClass(WordCountMapper.class);
  20. job.setReducerClass(WordCountReducer.class);
  21. job.setMapOutputKeyClass(Text.class);
  22. job.setMapOutputValueClass(IntWritable.class);
  23. job.setOutputKeyClass(Text.class);
  24. job.setOutputValueClass(IntWritable.class);
  25. FileInputFormat.setInputPaths(job, new Path(args[0]));
  26. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  27. // 设置reduce端输出压缩开启
  28. FileOutputFormat.setCompressOutput(job, true);
  29. // 设置压缩的方式
  30. FileOutputFormat.setOutputCompressorClass(job, BZip2Codec.class);
  31. // FileOutputFormat.setOutputCompressorClass(job, GzipCodec.class);
  32. // FileOutputFormat.setOutputCompressorClass(job, DefaultCodec.class);
  33. boolean result = job.waitForCompletion(true);
  34. System.exit(result?0:1);
  35. }
  36. }

Mapper和Reducer保持不变