本篇內(nèi)容主要講解“Hadoop壓縮技術(shù)的概念”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“Hadoop壓縮技術(shù)的概念”吧!
創(chuàng)新互聯(lián)公司主要從事網(wǎng)頁設(shè)計、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機版網(wǎng)站建設(shè))、響應(yīng)式網(wǎng)站建設(shè)、程序開發(fā)、網(wǎng)站優(yōu)化、微網(wǎng)站、重慶小程序開發(fā)公司等,憑借多年來在互聯(lián)網(wǎng)的打拼,我們在互聯(lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了豐富的成都網(wǎng)站設(shè)計、成都做網(wǎng)站、網(wǎng)站設(shè)計、網(wǎng)絡(luò)營銷經(jīng)驗,集策劃、開發(fā)、設(shè)計、營銷、管理等多方位專業(yè)化運作于一體。

壓縮策略和原則

| 壓縮格式 | hadoop自帶 | 算法 | 文件擴展名 | 是否可切分 | 換成壓縮格式后,原程序是否需要修改 |
|---|---|---|---|---|---|
| DEFLATE | 是,直接使用 | DEFLATE | .deflate | 否 | 和文本處理一樣,不需要修改 |
| Gzip | 是,直接使用 | DEFLATE | .gz | 否 | 和文本處理一樣,不需要修改 |
| bzip2 | 是,直接使用 | bzip2 | .bz2 | 是 | 和文本處理一樣,不需要修改 |
| LZO | 否,需要安裝 | LZO | .lzo | 是 | 需要建索引,還需要指定輸入格式 |
| Snappy | 否,需要安裝 | Snappy | .snappy | 否 | 和文本處理一樣,不需要修改 |
為了支持多種壓縮/解壓縮算法,Hadoop 引入了編碼/解碼器,如下表所示。
| 壓縮格式 | 對應(yīng)的編碼/解碼器 |
|---|---|
| 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 |
壓縮性能的比較
| 壓縮算法 | 原始文件大小 | 壓縮文件大小 | 壓縮速度 | 解壓速度 |
|---|---|---|---|---|
| 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 |





| 參數(shù) | 默認(rèn)值 | 階段 |
|---|---|---|
| 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] | false | mapper輸出 |
| mapreduce.map.output.compress.codec [mapred-site.xml] | org.apache.hadoop.io.compress.DefaultCodec | mapper輸出 |
| mapreduce.output.fileoutputformat.compress [mapred-site.xml] | false | reducer輸出 |
| mapreduce.output.fileoutputformat.compress.codec [mapred-site.xml] | org.apache.hadoop.io.compress DefaultCodec | reducer輸出 |
| mapreduce.output.fileoutputformat.compress.type [mapred-site.xml] | RECORD | reducer輸出 |
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();
}
}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);
// 設(shè)置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);
}
}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]));
// 設(shè)置reduce端輸出壓縮開啟
FileOutputFormat.setCompressOutput(job, true);
// 設(shè)置壓縮的方式
FileOutputFormat.setOutputCompressorClass(job, BZip2Codec.class);
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1);
}
}到此,相信大家對“Hadoop壓縮技術(shù)的概念”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
當(dāng)前名稱:Hadoop壓縮技術(shù)的概念
標(biāo)題URL:http://www.chinadenli.net/article14/ihocde.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、電子商務(wù)、虛擬主機、手機網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)