zoukankan      html  css  js  c++  java
  • Hadoop学习笔记(三):Combiner funcitons

      很多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


    作者:beanmoon
    出处:http://www.cnblogs.com/beanmoon/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
    该文章也同时发布在我的独立博客中-豆月博客

  • 相关阅读:
    centos7 关闭firewall安装iptables并配置
    Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
    URL地址下载图片到本地
    IDEA常用快捷键
    电商的支付前、中、后这3个流程都是怎么设计的?
    jenkins的部署
    mysql 授权用户 主从和备份
    windows下利用iis建立网站网站并实现局域共享
    nginx反向代理 和部分优化
    LNMP的搭建 及地址转换
  • 原文地址:https://www.cnblogs.com/beanmoon/p/2805684.html
Copyright © 2011-2022 走看看