分享

Hadoop reducer类的阅读

lzw 发表于 2013-11-30 15:35:14 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 25 76604

在Hadoop的reducer类中,有3个主要的函数,分别是:setup,clearup,reduce。代码如下:
  1.   /**
  2.    * Called once at the start of the task.
  3.    */
  4.   protected void setup(Context context
  5.                        ) throws IOException, InterruptedException {
  6.     // NOTHING
  7.   }
复制代码

  1.   /**
  2.    * This method is called once for each key. Most applications will define
  3.    * their reduce class by overriding this method. The default implementation
  4.    * is an identity function.
  5.    */
  6.   @SuppressWarnings("unchecked")
  7.   protected void reduce(KEYIN key, Iterable<VALUEIN> values, Context context
  8.                         ) throws IOException, InterruptedException {
  9.     for(VALUEIN value: values) {
  10.       context.write((KEYOUT) key, (VALUEOUT) value);
  11.     }
  12.   }
复制代码


  1.   /**
  2.    * Called once at the end of the task.
  3.    */
  4.   protected void cleanup(Context context
  5.                          ) throws IOException, InterruptedException {
  6.     // NOTHING
  7.   }
复制代码


在用户的应用程序中调用到reducer时,会直接调用reducer里面的run函数,其代码如下:
  1. /*
  2.    * control how the reduce task works.
  3.    */
  4.   @SuppressWarnings("unchecked")
  5.   public void run(Context context) throws IOException, InterruptedException {
  6.     setup(context);
  7.     while (context.nextKey()) {
  8.       reduce(context.getCurrentKey(), context.getValues(), context);
  9.       // If a back up store is used, reset it
  10.       ((ReduceContext.ValueIterator)
  11.           (context.getValues().iterator())).resetBackupStore();
  12.     }
  13.     cleanup(context);
  14.   }
  15. }
复制代码


由上面的代码,我们可以了解到,当调用到reduce时,通常会先执行一个setup函数,最后会执行一个cleanup函数。而默认情况下,这两个函数的内容都是nothing。因此,当reduce不符合应用要求时,可以试着通过增加setup和cleanup的内容来满足应用的需求。

加入qq群(号码:39327136),讨论云技术,获取最新资讯资源等

欢迎加入about云群9037177932227315139327136 ,云计算爱好者群,亦可关注about云腾讯认证空间||关注本站微信

已有(26)人评论

跳转到指定楼层
legend404 发表于 2014-5-19 10:01:20
不太懂  谁能解释一下这些是干什么用的
回复

使用道具 举报

admin 发表于 2014-5-19 12:28:04
legend404 发表于 2014-5-19 10:01
不太懂  谁能解释一下这些是干什么用的

没有setup和cleanup是属于reduce的一般情况
带有setup和cleanup属于需求更多的情况
如同一般情况和特殊情况
setup和cleanup就是用来处理reduce的特殊情况和更高需求的

回复

使用道具 举报

冥月幻影 发表于 2014-5-24 23:49:02
新人学习中
回复

使用道具 举报

ascentzhen 发表于 2014-7-18 13:23:22
cleanup是不是可以用来比如说释放系统的资源啊?
回复

使用道具 举报

跃阳紫 发表于 2014-7-28 20:26:18
回复

使用道具 举报

pig2 发表于 2014-7-28 22:19:17
可以看看这个:

mapreduce学习指导及疑难解惑汇总

回复

使用道具 举报

跃阳紫 发表于 2014-7-29 08:55:04
pig2 发表于 2014-7-28 22:19
可以看看这个:

mapreduce学习指导及疑难解惑汇总

好的,谢谢,我刚接触Hadoop一周的时间,学习起来有点迷茫,还希望多多指导!
回复

使用道具 举报

pig2 发表于 2014-7-29 10:44:01
跃阳紫 发表于 2014-7-29 08:55
好的,谢谢,我刚接触Hadoop一周的时间,学习起来有点迷茫,还希望多多指导!
初学可以参考这个:
云技术、大数据(hadoop)入门常见问题回答



零基础学习hadoop到上手工作线路指导(初级篇)


点评

好贴心!新人有问题都热心回答,为你们的态度点赞!!  发表于 2015-1-3 00:34
回复

使用道具 举报

跃阳紫 发表于 2014-7-29 13:47:14
pig2 发表于 2014-7-29 10:44
初学可以参考这个:
云技术、大数据(hadoop)入门常见问题回答

非常感谢,我会好好研读
回复

使用道具 举报

123下一页
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条