MapReduce源码-Mapper和Reducer


1、Mapper类,有setup()、map()、run()、cleanup()这4个方法,如下:
    setup():在run方法执行前首先被调用,且只调用1次,通常用于初始化。
    map():需要重写的方法,此方法中实现业务逻辑。run方法会循环遍历,为每个key、value调用一次这个方法。
    run():此方法由框架控制执行。
    cleanup():map函数处理完所有的key、value会停止遍历,接下来调用此方法1次用于清理。
    
    Mapper类源码如下:    
        public class Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT> {

          public abstract class Context implements MapContext<KEYIN,VALUEIN,KEYOUT,VALUEOUT> {}
          
          protected void setup(Context context) throws IOException, InterruptedException {}

          @SuppressWarnings("unchecked")
          protected void map(KEYIN key, VALUEIN value, Context context) throws IOException, InterruptedException {
            context.write((KEYOUT) key, (VALUEOUT) value);
          }

          protected void cleanup(Context context) throws IOException, InterruptedException {}

          public void run(Context context) throws IOException, InterruptedException {
            setup(context);
            try {
              while (context.nextKeyValue()) {
                map(context.getCurrentKey(), context.getCurrentValue(), context);
              }
            } finally {
              cleanup(context);
            }
          }
          
        }

2、Reducer类,有setup()、reduce()、run()、cleanup()这4个方法,如下:
    setup():在run方法执行前首先被调用,且只调用1次,通常用于初始化。
    reduce():需要重写的方法,此方法中实现业务逻辑。run方法会循环遍历,为每个key、value调用一次这个方法。
    run():此方法由框架控制执行。
    cleanup():reduce函数处理完所有的key、value会停止遍历,接下来调用此方法1次用于清理。
    
    Reducer类源码如下:    
        public class Reducer<KEYIN,VALUEIN,KEYOUT,VALUEOUT> {

          public abstract class Context implements ReduceContext<KEYIN,VALUEIN,KEYOUT,VALUEOUT> {}

          protected void setup(Context context) throws IOException, InterruptedException {}

          @SuppressWarnings("unchecked")
          protected void reduce(KEYIN key, Iterable<VALUEIN> values, Context context) throws IOException, InterruptedException {
            for(VALUEIN value: values) {
              context.write((KEYOUT) key, (VALUEOUT) value);
            }
          }

          protected void cleanup(Context context) throws IOException, InterruptedException {}

          public void run(Context context) throws IOException, InterruptedException {
            setup(context);
            try {
              while (context.nextKey()) {
                reduce(context.getCurrentKey(), context.getValues(), context);
                // If a back up store is used, reset it
                Iterator<VALUEIN> iter = context.getValues().iterator();
                if(iter instanceof ReduceContext.ValueIterator) {
                  ((ReduceContext.ValueIterator<VALUEIN>)iter).resetBackupStore();        
                }
              }
            } finally {
              cleanup(context);
            }
          }
          
        }

转载于:https://www.cnblogs.com/mengyao/archive/2013/02/09/4869454.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值