在Hadoop的mapper类中,有4个主要的函数,分别是:setup,clearup,map,run。代码如下:

  1. protected void setup(Context context) throws IOException, InterruptedException {
  2. // NOTHING
  3. }
  4. protected void map(KEYIN key, VALUEIN value,
  5. Context context) throws IOException, InterruptedException {
  6. context.write((KEYOUT) key, (VALUEOUT) value);
  7. }
  8. protected void cleanup(Context context) throws IOException, InterruptedException {
  9. // NOTHING
  10. }
  11. public void run(Context context) throws IOException, InterruptedException {
  12. setup(context);
  13. while (context.nextKeyValue()) {
  14. map(context.getCurrentKey(), context.getCurrentValue(), context);
  15. }
  16. cleanup(context);
  17. }
  18. }

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

在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的内容来满足应用的需求。

map/reduce类简单介绍的更多相关文章

  1. oc-12-NSString 类简单介绍及用法

    // 11-[掌握]NSString 类简单介绍及用法 #import <Foundation/Foundation.h> int main(int argc, const char * ...

  2. Tstrings类简单介绍及实例

    用TStrings保存文件;var  S: TStrings;begin  S := TStringList.Create();  { ... }  S.SaveToFile('config.txt' ...

  3. C++map类型 之 简单介绍

    一:map的前世今生 (1)从关联容器与顺序容器说起.关联容器通过键(key)存储和读取元素.而顺序容器则通过元素在容器中的位置顺序存储和訪问元素(vector,queue,stack,list等). ...

  4. 12.常用类简单介绍.md

    目录 1.Scanner类 2.System类 4.Object类和工具类 5.StringBuffer类和StringBuilder类 6.Math类 7.Random类和ThreadLocalRa ...

  5. Java学习笔记43(打印流、IO流工具类简单介绍)

    打印流: 有两个类:PrintStream,PrintWriter类,两个类的方法一致,区别在于构造器 PrintStream:构造方法:接收File类型,接收字符串文件名,接收字节输出流(Outpu ...

  6. Java并发之Semaphore和Exchanger工具类简单介绍

    一.Semaphore介绍 Semaphore意思为信号量,是用来控制同时访问特定资源的线程数数量.它的本质上其实也是一个共享锁.Semaphore可以用于做流量控制,特别是公用资源有限的应用场景.例 ...

  7. Java中的Number和Math类简单介绍

    Java Number类 一般地,当需要使用数字的时候,我们通常使用内置数据类型,如:byte.int.long.double 等. 实例: int a = 5000; float b = 13.65 ...

  8. java IO类简单介绍

    一.流的概念 流是字节序列的抽象概念.流和文件的差别:文件是数据的静态存储形式,而流是指数据传输时的形态.文件只是流的操作对象之一.流按其操作的对象不同可以分为文件流.网络流.内存流.磁带流等.Jav ...

  9. android application类简单介绍(一)

    每次应用程序执行时.应用程序的application类保持实例化的状态. 通过扩展applicaiton类,能够完毕下面3项工作: 1.对android执行时广播的应用程序级事件如低低内做出响应. 2 ...

随机推荐

  1. Dubbo之RPC架构

    为什么会有dubbo的出现: 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 当 ...

  2. PM,RD,FE,UE,UI,QA,OP,DBA,BRD,MRD,PRD,FSD全称解析

    PM    项目经理( Project Manager )    从职业角度,是指企业建立以项目经理责任制为核心,对项目实行质量.安全.进度.成本管理的责任保证体系和全面提高项目管理水平设立的重要管理 ...

  3. ubuntu循环登录问题的解决

    之前试过几个方法都不行,包括改/etc/profile,startx,删Xauthority文件等,这些都是因为,形象地来说是药不对症,ubuntu循环登录是有很多个问题造成的,前面的这些例子只是针对 ...

  4. cookie 与 session 的区别详解

    [转]cookie 与session 的区别详解 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择,都纪录下来.当 ...

  5. DPDK l3fwd

    l3fwd负责三层转发,比l2fwd要复杂点. /*- * BSD LICENSE * * Copyright(c) 2010-2014 Intel Corporation. All rights r ...

  6. 枚举处理工具类 .net

    将枚举转化成List<T>的方法如下: /// <summary> /// 枚举处理工具类 /// </summary> public class EnumHelp ...

  7. 配置可对外链接的Redis

    链接服务器的Redis telnet 192.168.1.200 6379 Trying 192.168.1.200... telnet: Unable to connect to remote ho ...

  8. scala学习手记30 - 闭包

    首先要弄白闭包的概念. 教材中的说法是:闭包是一种特殊的函数值,闭包中封闭或绑定了在另一个作用域或上下文中定义的变量.这里说闭包是一种特殊的函数值. 维基百科中的说法是:在计算机科学中,闭包(英语:C ...

  9. Sqlserver 查询 临时字段

    临时字段格式   字段名=N'字段值' 例子如下: select cEmp_C, cEmp_N, oper_id=N'001', log_pw=N'123', sSex, cDept_C, cDept ...

  10. 使用springmvc时报错org.springframework.beans.NullValueInNestedPathException: Invalid property 'department' of bean class [com.atguigu.springmvc.crud.entities.Employee]:

    使用springmvc时报错 org.springframework.beans.NullValueInNestedPathException: Invalid property 'departmen ...