zoukankan      html  css  js  c++  java
  • 【hadoop】——MapReduce解压缩实现

    转载请注明出处:http://www.cnblogs.com/zhengrunjian/p/4527269.html 

    1作为输入

    当压缩文件做为mapreduce的输入时,mapreduce将自动通过扩展名找到相应的codec对其解压。
    如果我们压缩的文件有相应压缩格式的扩展名(比如lzo,gz,bzip2等),hadoop就会根据扩展名去选择解码器解压。
    hadoop对每个压缩格式的支持,详细见下表:

    如果压缩的文件没有扩展名,则需 要在执行mapreduce任务的时候指定输入格式.

    [java] view plaincopy
     
    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 plaincopy
     
    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 plaincopy
     
    1. ~/hadoop/bin/hadoop com.sweetop.styhadoop.MaxTemperatureWithCompression   input/data.gz  output/  
    输出的每一个part都会被压缩,我们这里只有一个part,看下压缩了的输出
    [plain] view plaincopy
     
    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 plaincopy
     
    1. SequenceFileOutputFormat.setOutputCompressionType(job, SequenceFile.CompressionType.BLOCK);  
    如果你用Tool接口来跑mapreduce的话,可以在命令行设置这些参数,明显比硬编码好很多

    3压缩map输出

    即使你的mapreduce的输入输出都是未压缩的文件,你仍可以对map任务的中间输出作压缩,因为它要写在硬盘并且通过网络传输到reduce节点,对其压
    缩可以提高很多性能,这些工作也是只要设置两个属性即可,我们看下代码里怎么设置:
    [java] view plaincopy
     
    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
  • 相关阅读:
    C#:新邮件监听及搜索
    PHPexcel导入数据的时候出现object解决方法
    selectpage选择订单的时候,订单数量和金额会动态改变
    三、变量的简述
    TP框架where条件和whereOr条件同时使用
    一.OS运行机制
    二.进制简述
    1.go语言入门
    C# Redis学习系列二:Redis基本设置
    C# Redis学习系列一:Redis的认识、下载、安装、使用
  • 原文地址:https://www.cnblogs.com/zhengrunjian/p/4527269.html
Copyright © 2011-2022 走看看