MapReduce源碼分析:Mapper和Reducer類

來源:互聯網
上載者:User

標籤:源碼   hadoop   mapreduce   

一:Mapper類

在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的內容來滿足應用的需求。

二:Reducer類

在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的內容來滿足應用的需求。

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

MapReduce源碼分析:Mapper和Reducer類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.