大数据Hadoop-MapReduce学习之旅第六篇

258 阅读4分钟

「这是我参与11月更文挑战的第15天,活动详情查看:2021最后一次更文挑战」。

一、Hadoop 数据压缩

1、概述

  • 压缩的好处和坏处

    压缩的优点:以减少磁盘IO、减少磁盘存储空间。

    压缩的缺点:增加CPU开销。

  • 压缩原则

    运算密集型的Job,少用压缩。

    IO密集型的Job,多用压缩。

2、MR 支持的压缩编码

  • 压缩算法对比介绍

    image.png

  • 压缩性能的比较

    image.png

    http://google.github.io/snappy
    
    Snappy is a compression/decompression library. It does not aim for maximum compression, or compatibility with any other compression library; instead, it aims for very high speeds and reasonable compression. For instance, compared to the fastest mode of zlib, Snappy is an order of magnitude faster for most inputs, but the resulting compressed files are anywhere from 20% to 100% bigger.On a single core of a Core i7 processor in 64-bit mode, Snappy compresses at about 250 MB/sec or more and decompresses at about 500 MB/sec or more.
    

3、压缩方式选择

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

3.1、Gzip 压缩

优点:压缩率比较高; 

缺点:不支持Split;压缩/解压速度一般;

3.2、Bzip2 压缩

优点:压缩率高;支持Split; 

缺点:压缩/解压速度慢。

3.3、Lzo 压缩

优点:压缩/解压速度比较快;支持Split;

缺点:压缩率一般;想支持切片需要额外创建索引。

3.4、Snappy 压缩

优点:压缩和解压缩速度快; 

缺点:不支持Split;压缩率一般;

3.5、压缩位置选择

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

image.png

4、压缩参数设置

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

image.png

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

image.png

5、压缩实操案例

5.1、Map 输出端采用压缩

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

  • 给大家提供的Hadoop源码支持的压缩格式有:BZip2Codec、DefaultCodec

    public class WordCountDriver {
    
        public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
    
            // 1 获取job
            Configuration conf = new Configuration();
    
            // 开启map端输出压缩
            conf.setBoolean("mapreduce.map.output.compress", true);
    
            // 设置map端输出压缩方式
            conf.setClass("mapreduce.map.output.compress.codec", SnappyCodec.class, CompressionCodec.class);
    
            Job job = Job.getInstance(conf);
    
            // 2 设置jar包路径
            job.setJarByClass(WordCountDriver.class);
    
            // 3 关联mapper和reducer
            job.setMapperClass(WordCountMapper.class);
            job.setReducerClass(WordCountReducer.class);
    
            // 4 设置map输出的kv类型
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(IntWritable.class);
    
            // 5 设置最终输出的kV类型
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(IntWritable.class);
    
            // 6 设置输入路径和输出路径
            FileInputFormat.setInputPaths(job, new Path("D:\input\inputword"));
            FileOutputFormat.setOutputPath(job, new Path("D:\hadoop\output888"));
    
            // 7 提交job
            boolean result = job.waitForCompletion(true);
    
            System.exit(result ? 0 : 1);
        }
    }
    
  • Mapper保持不变

    public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
        private Text outK = new Text();
        private IntWritable outV = new IntWritable(1);
    
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            // 1 获取一行
            String line = value.toString();
            // 2 切割
            String[] words = line.split(" ");
            // 3 循环写出
            for (String word : words) {
                // 封装outk
                outK.set(word);
                // 写出
                context.write(outK, outV);
            }
        }
    }
    
  • Reducer保持不变

    public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        private IntWritable outV = new IntWritable();
    
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable value : values) {
                sum += value.get();
            }
            outV.set(sum);
            context.write(key, outV);
        }
    }
    

5.2、Reduce 输出端采用压缩

基于WordCount案例处理。

  • 修改驱动

    public class WordCountDriver {
    
        public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
    
            // 1 获取job
            Configuration conf = new Configuration();
    
            Job job = Job.getInstance(conf);
    
            // 2 设置jar包路径
            job.setJarByClass(WordCountDriver.class);
    
            // 3 关联mapper和reducer
            job.setMapperClass(WordCountMapper.class);
            job.setReducerClass(WordCountReducer.class);
    
            // 4 设置map输出的kv类型
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(IntWritable.class);
    
            // 5 设置最终输出的kV类型
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(IntWritable.class);
    
            // 6 设置输入路径和输出路径
            FileInputFormat.setInputPaths(job, new Path("D:\input\inputword"));
            FileOutputFormat.setOutputPath(job, new Path("D:\hadoop\output888"));
    
    
            // 设置reduce端输出压缩开启
            FileOutputFormat.setCompressOutput(job, true);
    
            // 设置压缩的方式
            // FileOutputFormat.setOutputCompressorClass(job, BZip2Codec.class);
            // FileOutputFormat.setOutputCompressorClass(job, GzipCodec.class);
           FileOutputFormat.setOutputCompressorClass(job, DefaultCodec.class);
    
    
            // 7 提交job
            boolean result = job.waitForCompletion(true);
    
            System.exit(result ? 0 : 1);
        }
    }
    
  • Mapper和Reducer保持不变

二、常见错误及解决方案

  • 导包容易出错。尤其Text和CombineTextInputFormat。

  • Mapper中第一个输入的参数必须是LongWritable或者NullWritable,不可以是IntWritable.  报的错误是类型转换异常。

  • java.lang.Exception: java.io.IOException: Illegal partition for 13926435656 (4),说明Partition和ReduceTask个数没对上,调整ReduceTask个数。

  • 如果分区数不是1,但是reducetask为1,是否执行分区过程。答案是:不执行分区过程。因为在MapTask的源码中,执行分区的前提是先判断ReduceNum个数是否大于1。不大于1肯定不执行。

  • Unsupported major.minor version 52.0原因是Windows环境用的jdk1.7,Linux环境用的jdk1.8。解决方案:统一jdk版本。

  • 报类型转换异常。通常都是在驱动函数中设置Map输出和最终输出时编写错误。Map输出的key如果没有排序,也会报类型转换异常。

  • 集群中运行wc.jar时出现了无法获得输入文件。原因:WordCount案例的输入文件不能放用HDFS集群的根目录。

  • 自定义Outputformat时,注意在RecordWirter中的close方法必须关闭流资源。否则输出的文件内容中数据为空。

三、友情链接

大数据Hadoop-MapReduce学习之旅第五篇

大数据Hadoop-MapReduce学习之旅第四篇

大数据Hadoop-MapReduce学习之旅第三篇

大数据Hadoop-MapReduce学习之旅第二篇

大数据Hadoop-MapReduce学习之旅第一篇