Hadoop学习笔记(三):Combiner funcitons

来源:互联网 发布:域名提供商查询 编辑:程序博客网 时间:2024/05/16 03:25

 很多MapReduce程序受限于集群上可用的带宽,所以它会尽力最小化需要在map和reduce任务之间传输的中间数据。Hadoop允许用户声明一个combiner function来处理map的输出,同时把自己对map的处理结果作为reduce的输入。因为combiner function本身只是一种优化,hadoop并不保证对于某个map输出,这个方法会被调用多少次。换句话说,不管combiner function被调用多少次,对应的reduce输出结果都应该是一样的。

  下面我们以学习笔记(一)中的例子来加以说明,假设1950年的天气数据读取是由两个map完成的,其中第一个map的输出如下: 
  (1950, 0)
  (1950, 20)
  (1950, 10)

第二个map的输出为:
(1950, 25)
(1950, 15)

而reduce得到的输入为:(1950, [0, 20, 10, 25, 15]), 输出为:(1950, 25)

  由于25是集合中的最大值,我们可以使用一个类似于reduce function的combiner function来找出每个map输出中的最大值,这样的话,reduce的输入就变成了:
  (1950, [20, 25])

  各个funciton 对温度值的处理过程可以表示如下:max(0, 20, 10, 25, 15) = max(max(0, 20, 10), max(25, 15)) = max(20, 25) = 25

  注意:并不是所有的函数都拥有这个属性的(有这个属性的函数我们称之为commutative和associative),例如,如果我们要计算平均温度,就不能这样使用combiner function,因为mean(0, 20, 10, 25, 15) = 14,而mean(mean(0, 20, 10), mean(25, 15)) = mean(10, 20) = 15

  combiner function并不能取代reduce function(因为仍然需要reduce function处理来自不同map的带有相同key的记录)。但是他可以帮助减少需要在map和reduce之间传输的数据,就为这一点combiner function就值得考虑使用。

  声明combiner function

  现在我们回到学习笔记(一)中的mapreduce程序,对于这个程序来说,combiner的实现同reducer 的实现是一样的,唯一的改变就是在Job中设置一下Combiner的具体实现类(也就是reducer类,代码见下面下划线部分)。  

复制代码
 1 public class MaxTemperatureWithCombiner { 2   public static void main(String[] args) throws Exception { 3     if (args.length != 2) { 4        System.err.println("Usage: MaxTemperatureWithCombiner <input path> " + 5              "<output path>"); 6        System.exit(-1); 7     } 8     Job job = new Job(); 9     job.setJarByClass(MaxTemperatureWithCombiner.class);10     job.setJobName("Max temperature");11 12     FileInputFormat.addInputPath(job, new Path(args[0]));13     FileOutputFormat.setOutputPath(job, new Path(args[1]));14 15     job.setMapperClass(MaxTemperatureMapper.class);16     job.setCombinerClass(MaxTemperatureReducer.class);17     job.setReducerClass(MaxTemperatureReducer.class);18 19     job.setOutputKeyClass(Text.class);20     job.setOutputValueClass(IntWritable.class);21 22     System.exit(job.waitForCompletion(true) ? 0 : 1);23   }24 }
复制代码

 转载请注明出处:http://www.cnblogs.com/beanmoon/archive/2012/12/09/2805684.html

原创粉丝点击