Mapper类4个函数的解析

Mapper有setup(),map(),cleanup()和run()四个方法。其中setup()一般是用来进行一些map()前的准备工作,map()则一般承担主要的处理工作,cleanup()则是收尾工作如关闭文件或者执行map()后的K-V分发等。run()方法提供了setup->map->cleanup()的执行模板。

在MapReduce中,Mapper从一个输入分片中读取数据,然后经过Shuffle and Sort阶段,分发数据给Reducer,在Map端和Reduce端我们可能使用设置的Combiner进行合并,这在Reduce前进行。Partitioner控制每个K-V对应该被分发到哪个reducer[我们的Job可能有多个reducer],Hadoop默认使用HashPartitioner,HashPartitioner使用key的hashCode对reducer的数量取模得来。

protected void setup(Mapper.Context context) throws IOException,InterruptedException //Called once at the beginning of the task
protected void cleanup(Mapper.Context context)throws IOException,InterruptedException //Called once at the end of the task. 
protected void map(KEYIN key, VALUEIN value Mapper.Context context)throws IOException,InterruptedException
{
context.write((KEYOUT) key,(VALUEOUT) value);
} //Called once for each key/value pair in the input split. Most applications should override this, but the default is the identity function.
public void run(Mapper.Context context)throws IOException,InterruptedException
{
setup(context);
while(context.nextKeyValue())
{
map(context.getCurrentKey(),context.getCurrentValue(),context)
}
cleanup(context);
}
//Expert users can override this method for more complete control over the execution of the Mapper. 
执行顺序:setup --->   map/run   ----> cleanup

Mapper的三个子类,它们位于src\mapred\org\apache\hadoop\mapreduce\lib\map中(详解http://blog.csdn.net/posa88/article/details/7901304)
1、TokenCounterMapper
2、InverseMapper
3、MultithreadedMapper
同理在Reducer类中也存在4个函数
protected void setup(Mapper.Context context) throws IOException,InterruptedException //Called once at the beginning of the task
protected void cleanup(Mapper.Context context)throws IOException,InterruptedException //Called once at the end of the task. 
protected void reduce(KEYIN key, VALUEIN value Reducer.Context context)throws IOException,InterruptedException
{ for(VALUEIN value: values) {
      context.write((KEYOUT) key, (VALUEOUT) value);
    }
}
//This method is called once for each key. Most applications will define their reduce class by overriding this method. The default implementation is an identity function.

public void run(Reducer.Context context)throws IOException,InterruptedException
{
setup(context);
    while (context.nextKey()) {
     
reduce(context.getCurrentKey(), context.getValues(), context);
      // If a back up store is used, reset it
     
((ReduceContext.ValueIterator)
         
(context.getValues().iterator())).resetBackupStore();
    }
   
cleanup(context);
  }
}
//Advanced application writers can use the run(org.apache.hadoop.mapreduce.Reducer.Context) method to control how the reduce task works
执行顺序:setup --->   map/run   ----> cleanup

MapReduce之Mapper类,Reducer类中的函数(转载)的更多相关文章

  1. Hadoop(十七)之MapReduce作业配置与Mapper和Reducer类

    前言 前面一篇博文写的是Combiner优化MapReduce执行,也就是使用Combiner在map端执行减少reduce端的计算量. 一.作业的默认配置 MapReduce程序的默认配置 1)概述 ...

  2. Mapper类/Reducer类中的setup方法和cleanup方法以及run方法的介绍

    在hadoop的源码中,基类Mapper类和Reducer类中都是只包含四个方法:setup方法,cleanup方法,run方法,map方法.如下所示: 其方法的调用方式是在run方法中,如下所示: ...

  3. Adapter类 调用Activity中的函数

    在Adapter类中可以定义一个MainActivity变量,在初始化时,对其赋值,例如fragment的适配器中: private MainActivity context; private Lis ...

  4. EL表达式中fn函数 (转载)

    JSTL 使用表达式来简化页面的代码,这对一些标准的方法,例如bean的getter/setter方法,请求参数或者context以及 session中的数据的访问非常方便,但是我们在实际应用中经常需 ...

  5. Python中scatter()函数--转载

    原博地址:http://blog.csdn.net/anneqiqi/article/details/64125186 最近开始学习Python编程,遇到scatter函数,感觉里面的参数不知道什么意 ...

  6. 024_MapReduce中的基类Mapper和基类Reducer

    内容提纲 1) MapReduce中的基类Mapper类,自定义Mapper类的父类. 2) MapReduce中的基类Reducer类,自定义Reducer类的父类. 1.Mapper类 API文档 ...

  7. 027_编写MapReduce的模板类Mapper、Reducer和Driver

    模板类编写好后写MapReduce程序,的模板类编写好以后只需要改参数就行了,代码如下: package org.dragon.hadoop.mr.module; import java.io.IOE ...

  8. [Hadoop源码解读](四)MapReduce篇之Counter相关类

    当我们定义一个Counter时,我们首先要定义一枚举类型: public static enum MY_COUNTER{ CORRUPTED_DATA_COUNTER, NORMAL_DATA_COU ...

  9. 使用 Arrays 类操作 Java 中的数组

    Arrays 类是 Java 中提供的一个工具类,在 java.util 包中.该类中包含了一些方法用来直接操作数组,比如可直接实现数组的排序.搜索等(关于类和方法的相关内容在后面的章节中会详细讲解滴 ...

随机推荐

  1. imx6 android 进入文件系统闪屏

    imx6进入文件系统的时候都会闪屏,应该是framebuffer未初始化,就已经打开了背光.目前解决办法,在kenel阶段关闭背光,显示android的开机动画之后(此时framebuffer已经初始 ...

  2. NSUserDefaults 可以保存哪些类型

    NSData NSString NSNumber NSDate NSArray NSDictionary *如果你想保存其他类型,如UIImage,你应该进行编码(即archive),或者将它转换为N ...

  3. 给Windows机器创建软连接

    给Windows机器创建软连接 http://blog.csdn.net/w6611415/article/details/32084677

  4. Number类型

    这是计算基础,复杂的以后不充. 1.Number(); var box = { toString :function(){ return '123'; } }; alert(Number(box)); ...

  5. Linux命令行–基本的bash shell命令

    启动shell: /etc/passwd:包含系统用户账户列表以及每个用户的基本配置信息 每个条目有七个字段,每个字段用冒号隔开 用户名 用户密码 用户的系统UID 用户的系统GID 用户的全名 用户 ...

  6. linux dmesg命令参数及用法详解(linux显示开机信息命令)

    linux dmesg命令参数及用法详解(linux显示开机信息命令) http://blog.csdn.net/zhongyhc/article/details/8909905 功能说明:显示开机信 ...

  7. jQuery操作控件

    在项目中添加前台控件radio,操作两个div的显示和隐藏,其实是一个很简单的问题,但是费了老大劲才完成,也就是jQuery操作控件的一些基础知识.方法有三种,简单介绍: 1.给元素设置style属性 ...

  8. .NET反射(Reflection)机制

    C#编译后的文件主要由IL代码和元数据组成,元数据为.NET组件提供了丰富的自描述特性,它使得我们可以在代码运行时获知组件中的类型等重要的信息.C#中这是通过一种称作映射(Reflection)的机制 ...

  9. ios数字转emoj表情

    +(NSString *)convertSimpleUnicodeStr:(NSString *)inputStr{ ,); UTF32Char inputChar = ; // unicodeInt ...

  10. OC数组中文排序

    -(void)sortStudentInfo { if(studentInfoArray && studentInfoArray.count > 0) { for(TWDetai ...