Hadoop mapper类的阅读

来源:互联网 发布:淘宝新店铺有扶持吗 编辑:程序博客网 时间:2024/05/21 10:55

文章转自:http://www.aboutyun.com/thread-5597-1-1.html

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