转载请注明出处:http://blog.csdn.net/lastsweetop/article/details/9187721

作为输入

当压缩文件做为mapreduce的输入时,mapreduce将自动通过扩展名找到相应的codec对其解压。

作为输出

当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的话,可以在命令行设置这些参数,明显比硬编码好很多

压缩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);

mr中间结果优化的更多相关文章

  1. 【Hadoop】Hadoop MR 性能优化 Combiner机制

    1.概念 2.参考资料 提高hadoop的mapreduce job效率笔记之二(尽量的用Combiner) :http://sishuo(k).com/forum/blogPost/list/582 ...

  2. Hive整体优化策略

    一 整体架构优化 现在hive的整体框架如下,计算引擎不仅仅支持Map/Reduce,并且还支持Tez.Spark等.根据不同的计算引擎又可以使用不同的资源调度和存储系统. 整体架构优化点: 1 根据 ...

  3. MR中的combiner和partitioner

    1.combiner combiner是MR编程模型中的一个组件: 有些任务中map可能会产生大量的本地输出,combiner的作用就是在map端对输出先做一次合并,以减少map和reduce节点之间 ...

  4. Hadoop优化

    一.影响MR程序效率的因素 1.计算机性能: CPU.内存.磁盘.网络, 计算机的性能会影响MR程序的速度与效率 2.I/O方面 1)数据倾斜(代码优化) 2)map和reduce数量设置不合理(通过 ...

  5. Hadoop3.x 三大组件详解

    Hadoop Hadoop适合海量数据分布式存储和分布式计算 运行用户使用简单的编程模型实现跨机器集群对海量数据进行分布式计算处理 1. 概述 1.1 简介 Hadoop核心组件 HDFS (分布式文 ...

  6. 浅析Hadoop文件格式

    Hadoop 作为MR 的开源实现,一直以动态运行解析文件格式并获得比MPP数据库快上几倍的装载速度为优势.不过,MPP数据库社区也一直批评Hadoop由于文件格式并非为特定目的而建,因此序列化和反序 ...

  7. hadoop 原理: 浅析Hadoop文件格式

    Hadoop 作为MR 的开源实现,一直以动态运行解析文件格式并获得比MPP数据库快上几倍的装载速度为优势.不过,MPP数据库社区也一直批评Hadoop由于文件格式并非 为特定目的而建,因此序列化和反 ...

  8. 【原创】大数据基础之Hive(5)性能调优Performance Tuning

    1 compress & mr hive默认的execution engine是mr hive> set hive.execution.engine;hive.execution.eng ...

  9. Shark简介、部署及编译小结

    http://blog.csdn.net/pelick/article/details/11964291 Shark简介 Shark即Hive on Spark,本质上是通过Hive的HQL解析,把H ...

随机推荐

  1. C语言 域名通配符实现

    本例实现通配符 * 的功能,不支持*在字符串的末尾, 仅提供思路,函数仅做简单单元测试. 如有使用,还请自己进行修改 // str1: 待匹配字符串 // str2: 带通配符字串 int wildc ...

  2. MySQL 中now()时间戳用法

    MySQL 中now()时间戳用法 UPDATE news set addtime = unix_timestamp(now()); #结果:1452001082  

  3. Linux 监测内存常用的工具sar free vmstat

    Linux 检测内存常用的工具sar free vmstat free 内存统计信息解释 total 内存总量used 内存使用的大小free 内存剩余大小shared 共享内存大小buffers 块 ...

  4. iOS开发之UITableView的使用

    这一篇记录的是iOS开发中UITableView的使用,iOS中的UITableView跟Android中的ListView特别相似,以下用一个Demo来说明: 1.Xcode中新建projectTe ...

  5. mindmanager2018优化

      mindmanager2018优化 CreationTime--2018年6月6日09:35:02 Author:Marydon 1.点击“文件”-“选项”进入配置界面,在“常规”选项中,建议勾选 ...

  6. ffmpeg command

    1. 列出当前系统的设备列表 ffmpeg -list_devices true -f dshow -i dummy 2. 列出设备Integrated Camera的信息 ffmpeg -list_ ...

  7. Android的学习之路(四)项目中清单文件的学习和android中经常使用的显示单位

    1.所谓的清单文件就是项目中的AndroidManifest.xml文件.这个文件但是有大用处的.比方:app的名字,图标.app支持的版本号app的包名等等.以下我就介绍下这个清单文件的各个參数的作 ...

  8. 峰值因子,峰均比,Reference Level

    峰值因子(CREST Factor,CF)与 峰均比( Peak-to-Average Ratio,PAR) 对于一个波形信号,在一段时间内信号幅度峰值比上信号幅度的有效值即为信号的峰值因子,它表征了 ...

  9. 温故而知新 forEach 无法中断(break)的问题

    forEach无法使用break和return来中断,只能使用throw catch来达到中断的效果了. var id = (function(){ // forEach 是无法中断的.除非用这种ha ...

  10. Spring Boot 概念知识

    转 http://rapharino.com/coder/Spring-Boot-Induction/ Spring Boot Induction 发表于 2016-10-29   |   分类于 c ...