【hadoop】——MapReduce解壓縮實現


轉載請注明出處:http://www.cnblogs.com/zhengrunjian/p/4527269.html 

1作為輸入

當壓縮文件做為mapreduce的輸入時,mapreduce將自動通過擴展名找到相應的codec對其解壓。
如果我們壓縮的文件有相應壓縮格式的擴展名(比如lzo,gz,bzip2等),hadoop就會根據擴展名去選擇解碼器解壓。
hadoop對每個壓縮格式的支持,詳細見下表:

如果壓縮的文件沒有擴展名,則需 要在執行mapreduce任務的時候指定輸入格式.

[java]  view plain copy
 
  1. hadoop jar /usr/home/hadoop/hadoop-0.20.2/contrib/streaming/hadoop-streaming-0.20.2-CDH3B4.jar 
  2. -file /usr/home/hadoop/hello/mapper.py -mapper /usr/home/hadoop/hello/mapper.py 
  3. -file /usr/home/hadoop/hello/reducer.py -reducer /usr/home/hadoop/hello/reducer.py 
  4. -input lzotest -output result4 
  5. -jobconf mapred.reduce.tasks=1 
  6. -inputformat org.apache.hadoop.mapred.LzoTextInputFormat

2作為輸出

當mapreduce的輸出文件需要壓縮時,可以更改mapred.output.compress為true,mapped.output.compression.codec為想要使用的codec的類名就
可以了,當然你可以在代碼中指定,通過調用FileOutputFormat的靜態方法去設置這兩個屬性,我們來看代碼:
[java]  view plain copy
 
  1. package com.sweetop.styhadoop;  
  2.   
  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.GzipCodec;  
  7. import org.apache.hadoop.mapreduce.Job;  
  8. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  9. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  10.   
  11. import java.io.IOException;  
  12.   
  13. /** 
  14.  * Created with IntelliJ IDEA. 
  15.  * User: lastsweetop 
  16.  * Date: 13-6-27 
  17.  * Time: 下午7:48 
  18.  * To change this template use File | Settings | File Templates. 
  19.  */  
  20. public class MaxTemperatureWithCompression {  
  21.     public static void main(String[] args) throws Exception {  
  22.         if (args.length!=2){  
  23.             System.out.println("Usage: MaxTemperature <input path> <out path>");  
  24.             System.exit(-1);  
  25.         }  
  26.         Job job=new Job();  
  27.         job.setJarByClass(MaxTemperature.class);  
  28.         job.setJobName("Max Temperature");  
  29.   
  30.         FileInputFormat.addInputPath(job, new Path(args[0]));  
  31.         FileOutputFormat.setOutputPath(job, new Path(args[1]));  
  32.   
  33.         job.setMapperClass(MaxTemperatrueMapper.class);  
  34.         job.setCombinerClass(MaxTemperatureReducer.class);  
  35.         job.setReducerClass(MaxTemperatureReducer.class);  
  36.   
  37.         job.setOutputKeyClass(Text.class);  
  38.         job.setOutputValueClass(IntWritable.class);  
  39.   
  40.         FileOutputFormat.setCompressOutput(job, true);  
  41.         FileOutputFormat.setOutputCompressorClass(job, GzipCodec.class);  
  42.   
  43.         System.exit(job.waitForCompletion(true)?0:1);  
  44.   
  45.     }  
  46. }  
輸入也是一個壓縮文件
[plain]  view plain copy
 
  1. ~/hadoop/bin/hadoop com.sweetop.styhadoop.MaxTemperatureWithCompression   input/data.gz  output/  
輸出的每一個part都會被壓縮,我們這里只有一個part,看下壓縮了的輸出
[plain]  view plain copy
 
  1. [hadoop@namenode test]$hadoop fs -get output/part-r-00000.gz .  
  2. [hadoop@namenode test]$ls  
  3. 1901  1902  ch2  ch3  ch4  data.gz  news.gz  news.txt  part-r-00000.gz  
  4. [hadoop@namenode test]$gunzip -c part-r-00000.gz   
  5. 1901<span style="white-space:pre">  </span>317  
  6. 1902<span style="white-space:pre">  </span>244  
如果你要將序列文件做為輸出,你需要設置mapred.output.compression.type屬性來指定壓縮類型,默認是RECORD類型,它會按單個的record壓縮,如果指定為BLOCK類型,它將一組record壓縮,壓縮效果自然是BLOCK好。
當然代碼里也可以設置,你只需調用SequenceFileOutputFormat的setOutputCompressionType方法進行設置。
[plain]  view plain copy
 
  1. SequenceFileOutputFormat.setOutputCompressionType(job, SequenceFile.CompressionType.BLOCK);  
如果你用Tool接口來跑mapreduce的話,可以在命令行設置這些參數,明顯比硬編碼好很多

3壓縮map輸出

即使你的mapreduce的輸入輸出都是未壓縮的文件,你仍可以對map任務的中間輸出作壓縮,因為它要寫在硬盤並且通過網絡傳輸到reduce節點,對其壓
縮可以提高很多性能,這些工作也是只要設置兩個屬性即可,我們看下代碼里怎么設置:
[java]  view plain copy
 
  1. Configuration conf = new Configuration();  
  2.     conf.setBoolean("mapred.compress.map.output", true);  
  3.     conf.setClass("mapred.map.output.compression.codec",GzipCodec.class, CompressionCodec.class);  
  4.     Job job=new Job(conf);  
  5. 轉至:http://blog.csdn.net/lastsweetop/article/details/9187721


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM