注:转载自http://blog.csdn.net/ipolaris/article/details/8723782

在MapReduce中,当map生成的数据过大时,带宽就成了瓶颈,怎样精简压缩传给Reduce的数据,有不影响最终的结果呢。有一种方法就是使用Combiner,Combiner号称本地的Reduce,Reduce最终的输入,是Combiner的输出。下面以《Hadoop in action》中的专利数据为例。我们打算统计每个国家的专利数目。代码如下(使用Combiner的代码注释掉):

package net.csdn.blog.ipolaris.hadoopdemo;  

import java.io.IOException;  

import net.scdn.blog.ipolaris.util.ArgsTool;  

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
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.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; public class Demo1 extends Configured implements Tool{ /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
System.exit(ToolRunner.run(new Demo1(), args)); } public static class DemoMap extends Mapper<LongWritable, Text, Text, IntWritable>{ @Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException { String line = value.toString();
String[] splitdata = line.split("\\,");
String contry = splitdata[4];
System.out.println("country:"+contry);
if (contry.trim().equals("\"COUNTRY\"")) {
return;
}else{
context.write(new Text(contry), new IntWritable(1));
}
} } public static class DemoReduce extends Reducer<Text, IntWritable, Text, IntWritable>{ @Override
protected void reduce(Text arg0, Iterable<IntWritable> arg1,Context context)
throws IOException, InterruptedException {
System.out.println("reduce");
int sum = 0;
for (IntWritable num : arg1) {
sum += num.get();
}
context.write(arg0, new IntWritable(sum));
} }
@Override
public int run(String[] arg0) throws Exception {
Configuration conf = getConf(); Job job = new Job(conf, "demo1");
String inputPath = ArgsTool.getArg(arg0, "input");
String outputPath = ArgsTool.getArg(arg0, "output"); FileInputFormat.addInputPath(job, new Path(inputPath));
FileOutputFormat.setOutputPath(job, new Path(outputPath)); job.setJarByClass(Demo1.class);
job.setMapperClass(DemoMap.class);
job.setReducerClass(DemoReduce.class);
//job.setCombinerClass(DemoReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
return job.waitForCompletion(true)?0:1;
} }

可以看出,reduce的输入每个key所对应的value将是一大串1,但处理的文本很多时,这一串1已将占用很大的带宽,如果我们在map的输出给于reduce之前做一下合并或计算,那么传给reduce的数据就会少很多,减轻了网络压力。此时Combiner就排上用场了。我们现在本地把Map的输出做一个合并计算,把具有相同key的1做一个计算,然后再把此输出作为reduce的输入,这样传给reduce的数据就少了很多。Combiner是用reducer来定义的,多数的情况下Combiner和reduce处理的是同一种逻辑,所以job.setCombinerClass()的参数可以直接使用定义的reduce,当然也可以单独去定义一个有别于reduce的Combiner,继承Reducer,写法基本上定义reduce一样。让我们看一下,加入Combiner之前的处理结果

我们看到Reduce input records的值为2923922(在map中删掉了一条数据),而Map input records值为2923923,也就是说每个map input record,对应了一个reduce input record。代表着我们要通过网络传输大量的值。最终的统计结果如下(只截取了一段)

我们在看看加上Combiner运行情况

Reduce input records只有565,大量的map输出已经在Combiner中进行了合并,最终的统计结果和上图相同,就不贴图了。

有关Combiner组件的总结如下:

1.是在每一个map task的本地运行,能收到map输出的每一个key的valuelist,所以可以做局部汇总处理

2.因为在map task的本地进行了局部汇总,就会让map端的输出数据量大幅精简,减小shuffle过程的网络IO

3.combiner其实就是一个reducer组件,跟真是的reducer的区别就在于,combiner运行maptask的本地

4.combiner在使用时需要注意,输入输出KV数据类型要跟map和reduce的相应数据类型匹配

5.要注意业务逻辑不能因为combiner的加入而受影响

Hadoop中Combiner的使用的更多相关文章

  1. hadoop中Combiner使用中需要注意的地方

    今天在写一个MR的时候,用到了combiner.在使用过程中,遇到了一些问题,特此记录一下. Combiner分为两种,一种是可插拔的,一种是不可插拔的. 可插拔的:Combiner和Reduce的处 ...

  2. Hadoop中Combiner的作用

    1.Partition 把 Map任务输出的中间结果按 key的范围划分成 R份( R是预先定义的 Reduce任务的个数),划分时通常使用hash函数如: hash(key) mod R,这样可以保 ...

  3. hadoop中联结不同来源数据

    装载自http://www.cnblogs.com/dandingyy/archive/2013/03/01/2938462.html 有时可能需要对来自不同源的数据进行综合分析: 如下例子: 有Cu ...

  4. 浅析 Hadoop 中的数据倾斜

    转自:http://my.oschina.net/leejun2005/blog/100922 最近几次被问到关于数据倾斜的问题,这里找了些资料也结合一些自己的理解. 在并行计算中我们总希望分配的每一 ...

  5. Hadoop中的Partitioner浅析

    转自:http://blog.csdn.net/b1198103958/article/details/47169105 Hadoop里面的MapReduce编程模型,非常灵活,大部分环节我们都可以重 ...

  6. Hadoop 中利用 mapreduce 读写 mysql 数据

    Hadoop 中利用 mapreduce 读写 mysql 数据   有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...

  7. Hadoop中客户端和服务器端的方法调用过程

    1.Java动态代理实例 Java 动态代理一个简单的demo:(用以对比Hadoop中的动态代理) Hello接口: public interface Hello { void sayHello(S ...

  8. [转] - hadoop中使用lzo的压缩

    在hadoop中使用lzo的压缩算法可以减小数据的大小和数据的磁盘读写时间,不仅如此,lzo是基于block分块的,这样他就允许数据被分解成chunk,并行的被hadoop处理.这样的特点,就可以让l ...

  9. Hadoop中WritableComparable 和 comparator

    1.WritableComparable 查看HadoopAPI,如图所示: WritableComparable继承自Writable和java.lang.Comparable接口,是一个Writa ...

随机推荐

  1. [leetcode] 题型整理之排列组合

    一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...

  2. eclipse运行速度优化(解决狂读盘、发布慢、CPU100%等问题)

    转自:http://blog.csdn.net/wyp_810618/article/details/8953921 我的ECLIPSE运行时速度奇慢,具体表现为: 1.只要ECLIPSE启动后,硬盘 ...

  3. 借鉴dubbo实现自定义缓存

    自定义缓存一般基于ConcurrentMap实现,实现缓存需要注意的点是缓存容器对象 本身依赖于 static final去存储对象,样例: ConcurrentMap<String, Gene ...

  4. 计算机视觉中的词袋模型(Bow,Bag-of-words)

    计算机视觉中的词袋模型(Bow,Bag-of-words) Bag-of-words 读 'xw20084898的专栏'的blogBag-of-words model in computer visi ...

  5. NIO的一些相关链接

    Architecture of a Highly Scalable NIO-Based Server Scalable IO in Java Tricks and Tips with NIO part ...

  6. 【转】安装第三方库出现 Python version 2.7 required, which was not found in the registry

    安装第三方库出现 Python version 2.7 required, which was not found in the registry 建立一个文件 register.py 内容如下. 然 ...

  7. 不能用con作为类名

    A class file was not written. The project may be inconsistent, if so try refreshing this project and ...

  8. Switch语句的case穿透

    Switch语句的case穿透 一 switch语句几点说明: 1. case后面只能是常量,不能是变量,而且,多个case后面的值不能出现相同的. 2.case后面表达式可以接受: 基本数据类型,b ...

  9. Go语言配置与开发环境配置

    1.首先下载go的运行时 http://golang.org/dl/  下载windows 的zip版本,解压到硬盘上的一个位置 2.设置环境变量如下 GOBIN %GOROOT%\bin //go的 ...

  10. 百度地图API的IP定位城市和浏览器定位(转)

    百度地图API提供了Geolocation 和 LocalCity两个服务类. 这俩API可以分别供用户在JavaScript中进行定位和城市确认. 1 本质上,Geolocation这个类是使用了支 ...