听了超哥的一席课后逐渐明白了Combiner,记录一下自己的理解!(thanks 超哥)

首先贴上两段代码:

code1:

package combine;import java.io.IOException;
import java.net.URI;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
/*** @ClassName: WordCount2 * @Description: TODO(这里用一句话描述这个类的作用) * @author zhangweixiang* @date 2014年3月6日 下午1:34:50*/public class WordCount2 {static final String INPUT_PATH="hdfs://192.168.0.9:9000/hello.txt";static final String OUT_PATH="hdfs://192.168.0.9:9000/word";public static void main(String[] args) throws Exception {Configuration conf=new Configuration();Job job=new Job(conf, WordCount2.class.getSimpleName());//1.1指定读取的文件位置FileInputFormat.addInputPaths(job, INPUT_PATH);//指定如何对输入文件进行格式化,把输入文件每一行解析成键值对job.setInputFormatClass(TextInputFormat.class);//1.2指定自定义的map类job.setMapperClass(MyMapper.class);//map输出的<k,v>类型。如果<k3,v3>的类型与<k2,v2>的类型一致,则可以省略job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(LongWritable.class);//1.3分区
//      job.setPartitionerClass(HashPartitioner.class);//有一个reudce任务运行
//      job.setNumReduceTasks(1);//1.4 排序、分组//1.5规约
//      job.setCombinerClass(MyReduce.class);//2.2指定自定义的reduce类job.setReducerClass(MyReduce.class);//指定reduce的输出类型job.setOutputKeyClass(Text.class);job.setOutputValueClass(LongWritable.class);//删除已存在的文件FileSystem fileSystem = FileSystem.get(new URI(OUT_PATH), new Configuration());Path path = new Path(OUT_PATH);if(fileSystem.exists(path)){fileSystem.delete(path, true);}//2.3指定写出到哪里FileOutputFormat.setOutputPath(job,new Path(OUT_PATH) );//指定输出文件的格式化类job.setOutputFormatClass(TextOutputFormat.class);//把Job提交给JobTracker执行job.waitForCompletion(true);}/*** @ClassName: MyMapper * @Description: map任务处理* @param  KEYIN 即k1  表示行的偏移量* @param  VALUEIN 即v1  表示行的文本内容* @param  KEYOUT 即k2  表示行中出现的单词* @param  VALUEOUT 即v2  表示行中出现的单词数,固定值为1* @author zhangweixiang* @date 2014年3月4日 下午4:16:00*/static class  MyMapper extends Mapper<LongWritable, Text, Text, LongWritable>{@Overrideprotected void map(LongWritable key, Text value,Context context)throws IOException, InterruptedException {String string = value.toString();//自定义计数器(查询hello的出现,以及出现次数)Counter counter = context.getCounter("查找hello", "hello");if(string.contains("hello")){counter.increment(1l);//出现一次+1}//分割字符串String[] split = string.split(" ");for(String word:split){context.write(new Text(word), new LongWritable(1));//map任务输出}}}/*** @ClassName: MyReduce * @Description: reduce任务处理* @param KEYIN 即k2 表示行中出现的单词* @param VALUEIN 即v2 表示行中出现的单词个数* @param KEYOUT 即k3 表示文本中出现的不同单词* @param VALUEOUT 即v3 表示文本中出现不同单词的总次数* @author zhangweixiang* @date 2014年3月4日 下午4:23:20*/static class MyReduce extends Reducer<Text, LongWritable, Text, LongWritable>{protected void reduce(Text key, Iterable<LongWritable> values,Context context)throws IOException, InterruptedException {long sum=0;for (LongWritable longWritable : values) {sum+=longWritable.get();}context.write(key, new LongWritable(sum));//reduce输出}}
}

code2

package combine;import java.io.IOException;
import java.net.URI;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
/*** @ClassName: WordCount2 * @Description: TODO(这里用一句话描述这个类的作用) * @author zhangweixiang* @date 2014年3月6日 下午1:34:50*/public class WordCount2 {static final String INPUT_PATH="hdfs://192.168.0.9:9000/hello.txt";static final String OUT_PATH="hdfs://192.168.0.9:9000/word";public static void main(String[] args) throws Exception {Configuration conf=new Configuration();Job job=new Job(conf, WordCount2.class.getSimpleName());//1.1指定读取的文件位置FileInputFormat.addInputPaths(job, INPUT_PATH);//指定如何对输入文件进行格式化,把输入文件每一行解析成键值对job.setInputFormatClass(TextInputFormat.class);//1.2指定自定义的map类job.setMapperClass(MyMapper.class);//map输出的<k,v>类型。如果<k3,v3>的类型与<k2,v2>的类型一致,则可以省略job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(LongWritable.class);//1.3分区
//      job.setPartitionerClass(HashPartitioner.class);//有一个reudce任务运行
//      job.setNumReduceTasks(1);//1.4 排序、分组//1.5规约job.setCombinerClass(MyReduce.class);//2.2指定自定义的reduce类job.setReducerClass(MyReduce.class);//指定reduce的输出类型job.setOutputKeyClass(Text.class);job.setOutputValueClass(LongWritable.class);//删除已存在的文件FileSystem fileSystem = FileSystem.get(new URI(OUT_PATH), new Configuration());Path path = new Path(OUT_PATH);if(fileSystem.exists(path)){fileSystem.delete(path, true);}//2.3指定写出到哪里FileOutputFormat.setOutputPath(job,new Path(OUT_PATH) );//指定输出文件的格式化类job.setOutputFormatClass(TextOutputFormat.class);//把Job提交给JobTracker执行job.waitForCompletion(true);}/*** @ClassName: MyMapper * @Description: map任务处理* @param  KEYIN 即k1  表示行的偏移量* @param  VALUEIN 即v1  表示行的文本内容* @param  KEYOUT 即k2  表示行中出现的单词* @param  VALUEOUT 即v2  表示行中出现的单词数,固定值为1* @author zhangweixiang* @date 2014年3月4日 下午4:16:00*/static class  MyMapper extends Mapper<LongWritable, Text, Text, LongWritable>{@Overrideprotected void map(LongWritable key, Text value,Context context)throws IOException, InterruptedException {String string = value.toString();//自定义计数器(查询hello的出现,以及出现次数)Counter counter = context.getCounter("查找hello", "hello");if(string.contains("hello")){counter.increment(1l);//出现一次+1}//分割字符串String[] split = string.split(" ");for(String word:split){context.write(new Text(word), new LongWritable(1));//map任务输出}}}/*** @ClassName: MyReduce * @Description: reduce任务处理* @param KEYIN 即k2 表示行中出现的单词* @param VALUEIN 即v2 表示行中出现的单词个数* @param KEYOUT 即k3 表示文本中出现的不同单词* @param VALUEOUT 即v3 表示文本中出现不同单词的总次数* @author zhangweixiang* @date 2014年3月4日 下午4:23:20*/static class MyReduce extends Reducer<Text, LongWritable, Text, LongWritable>{protected void reduce(Text key, Iterable<LongWritable> values,Context context)throws IOException, InterruptedException {long sum=0;for (LongWritable longWritable : values) {sum+=longWritable.get();}context.write(key, new LongWritable(sum));//reduce输出}}
}

code1和code2的唯一区别就是
//1.5规约
job.setCombinerClass(MyReduce.class);
code1中加了注释,code2启用了规约!
接下来看看控制台的输出(我win8下的eclipse)
code1:
/06 13:52:17 INFO mapred.JobClient: Counters: 20
14/03/06 13:52:17 INFO mapred.JobClient:   File Output Format Counters 
14/03/06 13:52:17 INFO mapred.JobClient:     Bytes Written=28
14/03/06 13:52:17 INFO mapred.JobClient:   FileSystemCounters
14/03/06 13:52:17 INFO mapred.JobClient:     FILE_BYTES_READ=424
14/03/06 13:52:17 INFO mapred.JobClient:     HDFS_BYTES_READ=84
14/03/06 13:52:17 INFO mapred.JobClient:     FILE_BYTES_WRITTEN=128676
14/03/06 13:52:17 INFO mapred.JobClient:     HDFS_BYTES_WRITTEN=28
14/03/06 13:52:17 INFO mapred.JobClient:   File Input Format Counters 
14/03/06 13:52:17 INFO mapred.JobClient:     Bytes Read=42
14/03/06 13:52:17 INFO mapred.JobClient:   查找hello
14/03/06 13:52:17 INFO mapred.JobClient:     hello=2
14/03/06 13:52:17 INFO mapred.JobClient:   Map-Reduce Framework
14/03/06 13:52:17 INFO mapred.JobClient:     Map output materialized bytes=126
14/03/06 13:52:17 INFO mapred.JobClient:     Map input records=4
14/03/06 13:52:17 INFO mapred.JobClient:     Reduce shuffle bytes=0
14/03/06 13:52:17 INFO mapred.JobClient:     Spilled Records=16
14/03/06 13:52:17 INFO mapred.JobClient:     Map output bytes=104
14/03/06 13:52:17 INFO mapred.JobClient:     Total committed heap usage (bytes)=266469376
14/03/06 13:52:17 INFO mapred.JobClient:     SPLIT_RAW_BYTES=98
14/03/06 13:52:17 INFO mapred.JobClient:     Combine input records=0
14/03/06 13:52:17 INFO mapred.JobClient:     Reduce input records=8
14/03/06 13:52:17 INFO mapred.JobClient:     Reduce input groups=4
14/03/06 13:52:17 INFO mapred.JobClient:     Combine output records=0
14/03/06 13:52:17 INFO mapred.JobClient:     Reduce output records=4
14/03/06 13:52:17 INFO mapred.JobClient:     Map output records=8

code2:
14/03/06 13:55:11 INFO mapred.JobClient: Counters: 20
14/03/06 13:55:11 INFO mapred.JobClient:   File Output Format Counters 
14/03/06 13:55:11 INFO mapred.JobClient:     Bytes Written=28
14/03/06 13:55:11 INFO mapred.JobClient:   FileSystemCounters
14/03/06 13:55:11 INFO mapred.JobClient:     FILE_BYTES_READ=364
14/03/06 13:55:11 INFO mapred.JobClient:     HDFS_BYTES_READ=84
14/03/06 13:55:11 INFO mapred.JobClient:     FILE_BYTES_WRITTEN=129072
14/03/06 13:55:11 INFO mapred.JobClient:     HDFS_BYTES_WRITTEN=28
14/03/06 13:55:11 INFO mapred.JobClient:   File Input Format Counters 
14/03/06 13:55:11 INFO mapred.JobClient:     Bytes Read=42
14/03/06 13:55:11 INFO mapred.JobClient:   查找hello
14/03/06 13:55:11 INFO mapred.JobClient:     hello=2
14/03/06 13:55:11 INFO mapred.JobClient:   Map-Reduce Framework
14/03/06 13:55:11 INFO mapred.JobClient:     Map output materialized bytes=66
14/03/06 13:55:11 INFO mapred.JobClient:     Map input records=4
14/03/06 13:55:11 INFO mapred.JobClient:     Reduce shuffle bytes=0
14/03/06 13:55:11 INFO mapred.JobClient:     Spilled Records=8
14/03/06 13:55:11 INFO mapred.JobClient:     Map output bytes=104
14/03/06 13:55:11 INFO mapred.JobClient:     Total committed heap usage (bytes)=266469376
14/03/06 13:55:11 INFO mapred.JobClient:     SPLIT_RAW_BYTES=98
14/03/06 13:55:11 INFO mapred.JobClient:     Combine input records=8
14/03/06 13:55:11 INFO mapred.JobClient:     Reduce input records=4
14/03/06 13:55:11 INFO mapred.JobClient:     Reduce input groups=4
14/03/06 13:55:11 INFO mapred.JobClient:     Combine output records=4
14/03/06 13:55:11 INFO mapred.JobClient:     Reduce output records=4
14/03/06 13:55:11 INFO mapred.JobClient:     Map output records=8

通过code1和code2的输出比较(红色部分),相信大家已经看出了区别!
code1中没有使用规约,所以:
Combine input records=0
Reduce input records=8
 Reduce input groups=4
Combine output records=0
code2中使用了规约,所以:
 Combine input records=8
 Reduce input records=4
Reduce input groups=4
Combine output records=4

很明显使用了规约后 Reduce input records=4减少map到reduce过程中数据的传输!

为什么要使用规约呢?(总结超哥的!)
/**
 * 问:为什么使用Combiner?
 * 答:Combiner发生在Map端,对数据进行规约处理,数据量变小了,传送到reduce端的数据量变小了,传输时间变短,作业的整体时间变短。
 * 
 * 问:为什么Combiner不作为MR运行的标配,而是可选步骤哪?
 * 答:因为不是所有的算法都适合使用Combiner处理,例如求平均数。
 *
 * 问:Combiner本身已经执行了reduce操作,为什么在Reducer阶段还要执行reduce操作哪?
 * 答:combiner操作发生在map端的,处理一个任务所接收的文件中的数据,不能跨map任务执行;只有reduce可以接收多个map任务处理的数据。
 *
 */











												

mapreduce的规约(Combiner)相关推荐

  1. Hadoop大数据——mapreduce中的Combiner/序列化/排序初步

    mapreduce中的Combiner (1)combiner是MR程序中Mapper和Reducer之外的一种组件 (2)combiner组件的父类就是Reducer (3)Combiner和red ...

  2. 《Hadoop MapReduce实战手册》一1.4 给WordCount MapReduce程序增加combiner步骤

    本节书摘来异步社区<Hadoop MapReduce实战手册>一书中的第1章,第1.4节,作者: [美]Srinath Perera , Thilina Gunarathne 译者: 杨卓 ...

  3. MapReduce中的combiner类详解及自定义combiner类(转)

    转自:https://www.cnblogs.com/edisonchou/p/4297786.html 一.Combiner的出现背景 1.1 回顾Map阶段五大步骤 在第四篇博文<初识Map ...

  4. MapReduce中的Combiner

    1.combiner运行机制: Combiner是MR程序中Mapper和Reducer之外的一种组件 Combiner组件的父类就是Reducer Combiner和reducer的区别在于运行的位 ...

  5. Hadoop入门(九)Mapreduce高级shuffle之Combiner

    一.Combiner的出现 (1)为什么需要进行Map规约操 作 在上述过程中,我们看到至少两个性能瓶颈: (1)如果我们有10亿个数据,Mapper会生成10亿个键值对在网络间进行传输,但如果我们只 ...

  6. 使用Mapreduce案例编写用于统计文本中单词出现的次数的案例、mapreduce本地运行等,Combiner使用及其相关的知识,流量统计案例和流量总和以及流量排序案例,自定义Partitioner

    工程结构: 在整个案例过程中,代码如下: WordCountMapper的代码如下: package cn.toto.bigdata.mr.wc; import java.io.IOException ...

  7. MapReduce代码编写--求性别人数、求总分、关联、map端的过滤、combiner预聚合

    主要基于数据 #students.txt /* 1500100001,施笑槐,22,女,文科六班 1500100002,吕金鹏,24,男,文科六班 1500100003,单乐蕊,22,女,理科六班 1 ...

  8. MapReduce中Combiner的作用

    问题提出: 众所周知,Hadoop框架使用Mapper将数据处理成一个<key,value>键值对,再网络节点间对其进行整理(shuffle),然后使用Reducer处理数据并进行最终输出 ...

  9. mapreduce的combiner

    优化组件combiner 1. shuffle必经:分区 .排序.分组 combiner:优化过程 combiner的作用;对每一个maptask的输出做一个聚合 达到的效果:减少了shuffle过程 ...

最新文章

  1. python迭代计算_如何在Python中迭代坐标列表并计算它们之间的距离
  2. Java split(“\\s+“) 和 split(“+“) 有什么区别
  3. 关于health的原理
  4. css3动画事件—webkitAnimationEnd
  5. 使用 Github Actions artifact 在 workflow job 之间共享数据
  6. java分享第十七天-01(封装操作xml类)
  7. go test遇到的一些问题-command-line-arguments undefined: xxxxx
  8. 我不是来约架,我只是请他们说几句实话——QCon上海2015编程语言专题前瞻
  9. jsp的mysql数据库分页查询_Jsp如何实现分页功能(使用MySQL数据库)
  10. 小样本条件下工业无损检测X光图像
  11. 网页游戏服务器端开发心得
  12. 大数据商业化应用的价值和应用场景是什么?
  13. 解决opencart配置Gmail邮箱收不到来信
  14. 从限定词开始 - 词性识别在人工智能自然语言处理中的不足与改进
  15. LOL服务器人数最新,英雄联盟大区人数排名
  16. RAR Extractor - The Unarchiver Pro for mac(解压缩软件)
  17. GPU_GEMS_自然态_渲染水焦散
  18. 周末两天入门 PCB 设计
  19. Ubuntu16.04更改新加卷名称
  20. 支持向量机(SVM)的数学原理

热门文章

  1. qpaint 在graphicsview上的qimage画一条线_solidworks2016画一个塑料外壳:用开放的草图进行切除,你会吗?...
  2. 6月19日 NSFileHandle文件类的常用方法
  3. 树形结构与关系数据库之闭包表
  4. Windows 2003 Server 用户隔离
  5. P1244 青蛙过河
  6. angular.js前端和后台的数据交换,后台取不到值对应方案
  7. ASP.NET获取文件名,后缀名
  8. Mvc过滤器的使用【转载】
  9. matlab谐波仿真代码,matlab的谐波仿真程序基于ip-iq法???怎么出不来图像啊???...
  10. linux如何设置账号全民,linux基本练习:用户和组管理的相关练习