千家信息网

Hadoop压缩技术的概念

发表于:2025-01-31 作者:千家信息网编辑
千家信息网最后更新 2025年01月31日,本篇内容主要讲解"Hadoop压缩技术的概念",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Hadoop压缩技术的概念"吧!1 概述压缩策略和原则2 MR
千家信息网最后更新 2025年01月31日Hadoop压缩技术的概念

本篇内容主要讲解"Hadoop压缩技术的概念",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Hadoop压缩技术的概念"吧!

1 概述

压缩策略和原则

2 MR 支持的压缩编码

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

为了支持多种压缩/解压缩算法,Hadoop 引入了编码/解码器,如下表所示。

压缩格式对应的编码/解码器
DEFLATEorg.apache.hadoop.io.compress.DefaultCodec
gziporg.apache.hadoop.io.compress.GzipCodec
bzip2org.apache.hadoop.io.compress.BZip2Codec
LZOcom.hadoop.compression.lzo.LzopCodec
Snappyorg.apache.hadoop.io.compress.SnappyCodec

压缩性能的比较

压缩算法原始文件大小压缩文件大小压缩速度解压速度
gzip8.3GB1.8GB17.5MB/s58MB/s
bzip28.3GB1.1GB2.4MB/s9.5MB/s
LZO8.3GB2.9GB49.3MB/s74.6MB/s

3 压缩方式选择

3.1 Gzip 压缩

3.2 Bzip2 压缩

3.3 Lzo 压缩

3.4 Snappy 压缩

4 压缩位置选择

5 压缩参数配置

参数默认值阶段
io.compression.codecs [在core-site.xml]org.apache.hadoop.io.compress.DefaultCodecorg apache.hadoop.io.compress.GzipCodec org.apache.hadoop.io.compress.BZip2Codec输入压缩
mapreduce.map.output.compress [mapred-site.xml]falsemapper输出
mapreduce.map.output.compress.codec [mapred-site.xml]org.apache.hadoop.io.compress.DefaultCodecmapper输出
mapreduce.output.fileoutputformat.compress [mapred-site.xml]falsereducer输出
mapreduce.output.fileoutputformat.compress.codec [mapred-site.xml]org.apache.hadoop.io.compress DefaultCodecreducer输出
mapreduce.output.fileoutputformat.compress.type [mapred-site.xml]RECORDreducer输出

6 压缩实操案例

6.1 数据流的压缩和解压缩

package com.djm.mapreduce.zip;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IOUtils;import org.apache.hadoop.io.compress.CompressionCodec;import org.apache.hadoop.io.compress.CompressionCodecFactory;import org.apache.hadoop.io.compress.CompressionInputStream;import org.apache.hadoop.io.compress.CompressionOutputStream;import org.apache.hadoop.util.ReflectionUtils;import java.io.*;public class CompressUtils {    public static void main(String[] args) throws IOException, ClassNotFoundException {        compress(args[0], args[1]);        decompress(args[0]);    }    private static void decompress(String path) throws IOException {        CompressionCodecFactory factory = new CompressionCodecFactory(new Configuration());        CompressionCodec codec = (CompressionCodec) factory.getCodec(new Path(path));        if (codec == null) {            System.out.println("cannot find codec for file " + path);            return;        }        CompressionInputStream cis = codec.createInputStream(new FileInputStream(new File(path)));        FileOutputStream fos = new FileOutputStream(new File(path + ".decoded"));        IOUtils.copyBytes(cis, fos, 1024);        cis.close();        fos.close();    }    private static void compress(String path, String method) throws IOException, ClassNotFoundException {        FileInputStream fis = new FileInputStream(new File(path));        Class codecClass  = Class.forName(method);        CompressionCodec codec  = (CompressionCodec) ReflectionUtils.newInstance(codecClass, new Configuration());        FileOutputStream fos = new FileOutputStream(new File(path + codec.getDefaultExtension()));        CompressionOutputStream cos = codec.createOutputStream(fos);        IOUtils.copyBytes(fis, cos, 1024);        cos.close();        fos.close();        fis.close();    }}

6.2 Map 输出端采用压缩

package com.djm.mapreduce.wordcount;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.mapreduce.Job;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import java.io.IOException;public class WcDriver {    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {        Configuration configuration = new Configuration();        configuration.setBoolean("mapreduce.map.output.compress", true);        // 设置map端输出压缩方式        configuration.setClass("mapreduce.map.output.compress.codec", BZip2Codec.class, CompressionCodec.class);        Job job = Job.getInstance(configuration);        job.setJarByClass(WcDriver.class);        job.setMapperClass(WcMapper.class);        job.setReducerClass(WcReduce.class);        job.setMapOutputKeyClass(Text.class);        job.setMapOutputValueClass(IntWritable.class);        job.setOutputKeyClass(Text.class);        job.setOutputValueClass(IntWritable.class);        FileInputFormat.setInputPaths(job, new Path(args[0]));        FileOutputFormat.setOutputPath(job, new Path(args[1]));        boolean result = job.waitForCompletion(true);        System.exit(result ? 0 : 1);    }}

6.3 Reduce 输出端采用压缩

package com.djm.mapreduce.wordcount;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.mapreduce.Job;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import java.io.IOException;public class WcDriver {    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {        Configuration configuration = new Configuration();        Job job = Job.getInstance(configuration);        job.setJarByClass(WcDriver.class);        job.setMapperClass(WcMapper.class);        job.setReducerClass(WcReduce.class);        job.setMapOutputKeyClass(Text.class);        job.setMapOutputValueClass(IntWritable.class);        job.setOutputKeyClass(Text.class);        job.setOutputValueClass(IntWritable.class);        FileInputFormat.setInputPaths(job, new Path(args[0]));        FileOutputFormat.setOutputPath(job, new Path(args[1]));        // 设置reduce端输出压缩开启        FileOutputFormat.setCompressOutput(job, true);        // 设置压缩的方式        FileOutputFormat.setOutputCompressorClass(job, BZip2Codec.class);        boolean result = job.waitForCompletion(true);        System.exit(result ? 0 : 1);    }}

到此,相信大家对"Hadoop压缩技术的概念"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0