Hadoop3.x 之 Hadoop 数据压缩 (第十章)

一、概述

1、压缩的好处和坏处

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

2、压缩原则

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

二、MR 支持的压缩编码

1、压缩算法对比介绍

在这里插入图片描述

2、压缩性能的比较

在这里插入图片描述

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.

三、压缩方式选择

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

四、Gzip 压缩

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

五、Bzip2 压缩

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

六、Lzo 压缩

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

七、Snappy 压缩

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

八、压缩位置选择

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

在这里插入图片描述

九、压缩参数配置

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

在这里插入图片描述

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

在这里插入图片描述

十、压缩实操案例

1、Map 输出端采用压缩

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

WordCountDriver

package org.example.yasuo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.BZip2Codec;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.DefaultCodec;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * 数据压缩
 */
public class WordCountDriver {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //1、获取JOB
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);


        // 开启 map 端输出压缩
        configuration.setBoolean("mapreduce.map.output.compress", true);
      // 设置 map 端输出压缩方式
        configuration.setClass("mapreduce.map.output.compress.codec",
                BZip2Codec.class, CompressionCodec.class);

        //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);



        // 设置reduce端输出压缩开启
        FileOutputFormat.setCompressOutput(job, true);
        // 设置压缩的方式
//        FileOutputFormat.setOutputCompressorClass(job, GzipCodec.class);
//


        FileOutputFormat.setOutputCompressorClass(job, DefaultCodec.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\\output13311114"));

        //7、提交job
        boolean result = job.waitForCompletion(true);

        System.exit(result ? 0 : 1);
    }
}

WordCountMapper

package org.example.yasuo;


import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * KEYIN, map阶段输入的key的类型:LongWritable
 * VALUEIN, map阶段输入value类型 :text
 * KEYOUT, map阶段输出的key类型 text
 * VALUEOUT map阶段输出的value类型 IntWritable
 */
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(" ");
        for (String word : words) {
            outK.set(word);
            context.write(outK, outV);
        }


    }
}

WordCountReducer

package org.example.yasuo;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * reducer
 * KEYIN, reducer 阶段输入的key的类型:text
 * VALUEIN, reducer 阶段输入value类型 :IntWritable
 * KEYOUT, reducer 阶段输出的key类型 text
 * VALUEOUT reducer 阶段输出的value类型 IntWritable
 */
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 num = 0;
        for (IntWritable value : values) {
            num += value.get();
        }
        outV.set(num);
        //写出
        context.write(key, outV);

    }
}

开启 gz的压缩 输出路径
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值