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

1作为输入

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

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

  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的静态方法去设置这两个属性,我们来看代码:
  1. package com.sweetop.styhadoop;
  2. import org.apache.hadoop.fs.Path;
  3. import org.apache.hadoop.io.IntWritable;
  4. import org.apache.hadoop.io.Text;
  5. import org.apache.hadoop.io.compress.GzipCodec;
  6. import org.apache.hadoop.mapreduce.Job;
  7. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  8. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  9. import java.io.IOException;
  10. /**
  11. * Created with IntelliJ IDEA.
  12. * User: lastsweetop
  13. * Date: 13-6-27
  14. * Time: 下午7:48
  15. * To change this template use File | Settings | File Templates.
  16. */
  17. public class MaxTemperatureWithCompression {
  18. public static void main(String[] args) throws Exception {
  19. if (args.length!=2){
  20. System.out.println("Usage: MaxTemperature <input path> <out path>");
  21. System.exit(-1);
  22. }
  23. Job job=new Job();
  24. job.setJarByClass(MaxTemperature.class);
  25. job.setJobName("Max Temperature");
  26. FileInputFormat.addInputPath(job, new Path(args[0]));
  27. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  28. job.setMapperClass(MaxTemperatrueMapper.class);
  29. job.setCombinerClass(MaxTemperatureReducer.class);
  30. job.setReducerClass(MaxTemperatureReducer.class);
  31. job.setOutputKeyClass(Text.class);
  32. job.setOutputValueClass(IntWritable.class);
  33. FileOutputFormat.setCompressOutput(job, true);
  34. FileOutputFormat.setOutputCompressorClass(job, GzipCodec.class);
  35. System.exit(job.waitForCompletion(true)?0:1);
  36. }
  37. }

输入也是一个压缩文件

  1. ~/hadoop/bin/hadoop com.sweetop.styhadoop.MaxTemperatureWithCompression   input/data.gz  output/
输出的每一个part都会被压缩,我们这里只有一个part,看下压缩了的输出
  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方法进行设置。
  1. SequenceFileOutputFormat.setOutputCompressionType(job, SequenceFile.CompressionType.BLOCK);

如果你用Tool接口来跑mapreduce的话,可以在命令行设置这些参数,明显比硬编码好很多

3压缩map输出

即使你的mapreduce的输入输出都是未压缩的文件,你仍可以对map任务的中间输出作压缩,因为它要写在硬盘并且通过网络传输到reduce节点,对其压
缩可以提高很多性能,这些工作也是只要设置两个属性即可,我们看下代码里怎么设置:
  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

【hadoop】——MapReduce解压缩实现的更多相关文章

  1. Hadoop MapReduce开发最佳实践(上篇)

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  2. Hadoop文件解压缩

    Class org.apache.hadoop.io.compress .CompressionCodecFactory A factory that will find the correct co ...

  3. Hadoop MapReduce执行过程详解(带hadoop例子)

    https://my.oschina.net/itblog/blog/275294 摘要: 本文通过一个例子,详细介绍Hadoop 的 MapReduce过程. 分析MapReduce执行过程 Map ...

  4. hadoop MapReduce Yarn运行机制

    原 Hadoop MapReduce 框架的问题 原hadoop的MapReduce框架图 从上图中可以清楚的看出原 MapReduce 程序的流程及设计思路: 首先用户程序 (JobClient) ...

  5. Hadoop Mapreduce分区、分组、二次排序过程详解[转]

    原文地址:Hadoop Mapreduce分区.分组.二次排序过程详解[转]作者: 徐海蛟 教学用途 1.MapReduce中数据流动   (1)最简单的过程:  map - reduce   (2) ...

  6. Hadoop MapReduce编程 API入门系列之薪水统计(三十一)

    不多说,直接上代码. 代码 package zhouls.bigdata.myMapReduce.SalaryCount; import java.io.IOException; import jav ...

  7. Hadoop MapReduce编程 API入门系列之压缩和计数器(三十)

    不多说,直接上代码. Hadoop MapReduce编程 API入门系列之小文件合并(二十九) 生成的结果,作为输入源. 代码 package zhouls.bigdata.myMapReduce. ...

  8. Hadoop MapReduce例子-新版API多表连接Join之模仿订单配货

    文章为作者原创,未经许可,禁止转载.    -Sun Yat-sen University 冯兴伟 一.    项目简介: 电子商务的发展以及电商平台的多样化,类似于京东和天猫这种拥有过亿用户的在线购 ...

  9. Writing an Hadoop MapReduce Program in Python

    In this tutorial I will describe how to write a simpleMapReduce program for Hadoop in thePython prog ...

随机推荐

  1. dmesg 的时间戳处理

    dmesg_with_human_timestamps () { $(type -P dmesg) "$@" | perl -w -e 'use strict; my ($upti ...

  2. Scalaz(17)- Monad:泛函状态类型-State Monad

    我们经常提到函数式编程就是F[T].这个F可以被视为一种运算模式.我们是在F运算模式的壳子内对T进行计算.理论上来讲,函数式程序的运行状态也应该是在这个运算模式壳子内的,也是在F[]内更新的.那么我们 ...

  3. malloc和new的区别

    (1)malloc在C和C++中都可以使用,用来申请一段内存:申请的内存一定要用free释放,然后把指针置为null: new只能在C++中使用,用于动态内存分配:new的对象要delete掉: (2 ...

  4. 接口测试之webservice

    什么是Webservice Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配 ...

  5. Git远程和分支管理

    一.远程       Git是分布式版本控制系统,最重要的优点就是远程仓库托管代码.不用自己搭建一个服务器,在github上面注册一个账户就可免费获取远程仓库.      首先需要先在github上面 ...

  6. struts2输入验证

    1.方法     ① 基于Annotations的验证       ②基于XML配置的验证 http://blog.csdn.net/furongkang/article/details/692204 ...

  7. prototype和__proto__

    一.prototype和__proto__的概念 prototype是函数的一个属性(每个函数都有一个prototype属性),这个属性是一个指针,指向一个对象.它是显示修改对象的原型的属性. __p ...

  8. Subway Icon Set – 306个像素完美的特制图标

    这个图标集是306个优化的像素完美,精雕细琢的图标.为这些设备进行了优化:iOS.Windows Phone.Windows 8 and BlackBerry 10,提供 PNG, SVG, XALM ...

  9. go语言 类型:布尔类型

    Go语言中的布尔类型与其他语言基本一致,关键字也为bool,可赋值为预定义的true和false示例代码如下: var v1 bool v1 = true v2 := (1 == 2) // v2也会 ...

  10. react native与现有的应用程序集成

    (1)通过cocopods 集成 ,以下内容 参考 http://wiki.jikexueyuan.com/project/react-native/integration-existing.html ...