2017-10-07 93 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

相关问题