手机版
你好,游客 登录 注册
背景:
阅读新闻

Hadoop2.0 Mapreduce实例WordCount体验

[日期:2012-10-22] 来源:Linux社区  作者:xubcing [字体:大 中 小]

在Hadoop2.0中MapReduce程序的都需要继承org.apache.hadoop.mapreduce.Mapper 和 org.apache.hadoop.mapreduce.Reducer这两个基础类,来定制自己的mapreduce功能,源码中主要的函数如下

Mapper.java

  1. public void run(Context context) throws IOException, InterruptedException { 
  2.     setup(context);     // Called once at the beginning of the task.  
  3.     while (context.nextKeyValue()) { 
  4.       map(context.getCurrentKey(), context.getCurrentValue(), context); 
  5.     } 
  6.     cleanup(context);   // Called once at the end of the task.  
  7.   } 
  8.  } 
  9.  
  10.   /** 
  11.    * Called once for each key/value pair in the input split. Most applications 
  12.    * should override this, but the default is the identity function. 
  13.    */ 
  14.   protected void map(KEYIN key, VALUEIN value,   
  15.                      Context context) throws IOException, InterruptedException { 
  16.     context.write((KEYOUT) key, (VALUEOUT) value); 
  17.   } 

Reducer.java

  1.   public void run(Context context) throws IOException, InterruptedException { 
  2.     setup(context);     // Called once at the beginning of the task.  
  3.     while (context.nextKey()) { 
  4.       reduce(context.getCurrentKey(), context.getValues(), context); 
  5.     } 
  6.     cleanup(context);   // Called once at the end of the task.  
  7.   } 
  8.  
  9.   /** 
  10.    * This method is called once for each key. Most applications will define 
  11.    * their reduce class by overriding this method. The default implementation 
  12.    * is an identity function. 
  13.    */ 
  14.   protected void reduce(KEYIN key, Iterable<VALUEIN> values, Context context 
  15.                         ) throws IOException, InterruptedException { 
  16.     for(VALUEIN value: values) { 
  17.       context.write((KEYOUT) key, (VALUEOUT) value); 
  18.     } 
  19.   } 
linux
相关资讯       Hadoop  MapReduce  Mapreduce实例  WordCount  WordCount源码 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款