2017-10-07 17 views
0

this文章中,我發現這個詞映射碼數:何時應該在Hadoop中使用OutputCollector和Context?

public static class MapClass extends MapReduceBase 
    implements Mapper<LongWritable, Text, Text, IntWritable> { 

    private final static IntWritable one = new IntWritable(1); 
    private Text word = new Text(); 

    public void map(LongWritable key, Text value, 
        OutputCollector<Text, IntWritable> output, 
        Reporter reporter) throws IOException { 
     String line = value.toString(); 
     StringTokenizer itr = new StringTokenizer(line); 
     while (itr.hasMoreTokens()) { 
     word.set(itr.nextToken()); 
     output.collect(word, one); 
     } 
    } 
    } 

相反,在official tutorial這是所提供的映射:

public static class TokenizerMapper 
     extends Mapper<Object, Text, Text, IntWritable>{ 

    private final static IntWritable one = new IntWritable(1); 
    private Text word = new Text(); 

    public void map(Object key, Text value, Context context 
        ) throws IOException, InterruptedException { 
     StringTokenizer itr = new StringTokenizer(value.toString()); 
     while (itr.hasMoreTokens()) { 
     word.set(itr.nextToken()); 
     context.write(word, one); 
     } 
    } 
    } 

到現在爲止,我只Context來看到寫一些從映射器到還原器,我從來沒有見過(或使用過)OutputCollector。我已閱讀documentation,但我不明白其使用的關鍵或爲什麼我應該使用它。

回答

2

這兩個代碼都包含不同的Map Reduce API。 OutputCollector是MRV1和Context是MRV2

Java的的Map Reduce API 1又稱MRV1初始的Hadoop版本,並與這些初始版本相關的漏洞發佈了地圖縮小框架進行處理的兩個任務,資源管理。

映射Reduce 2或下一代Map Reduce是一項期待已久且非常需要的與Hadoop中調度,資源管理和執行有關的技術升級。從根本上說,這些改進將集羣資源管理功能與Map Reduce專用邏輯分開,並且這種處理和資源管理的分離是通過在更高版本的HADOOP中啓動YARN實現的。

MRV1使用OutputCollecterReporter與MapReduce系統進行通信。

MRV2使用API​​廣泛使用允許用戶代碼與MapReduce系統通信的對象context。 (來自舊API的JobConf,OutputCollector和Reporter的角色由MRV2中的上下文對象統一)。

使用應該使用的MapReduce 2(MRV2)。我已經在Hadoop的突出的Hadoop 2的最大優勢:

  1. 一個主要優點是,有在 hadoop2架構沒有jobtrackers和的TaskTracker。相反,我們有YARN資源管理器和節點 。這有助於hadoop2支持除 mapreduce框架之外的其他模型來執行代碼並克服與mapreduce相關的高延遲 問題。
  2. Hadoop2支持非批處理以及傳統批處理 操作。
  3. Hdfs聯盟是在hadoop2中引入的。這使得多個 名稱節點可以控制試圖處理hadoop的單個 點故障問題的hadoop羣集。

MRV2還有很多優點。 https://hadoop.apache.org/docs/r2.7.1/hadoop-yarn/hadoop-yarn-site/

-2

這是一個很好的解決方案,但是,我只是使用1行解決方案: int wordcount = string.split(「」).length - 1;

+0

我完全沒有看到這與問題有關。 – justHelloWorld

相關問題