在this文章中,我发现了这个用于字数的映射器代码:
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
从mapper向reducer写了一些内容,我从未见过(或使用过)OutputCollector
。我已经阅读了documentation,但我不了解其使用的关键或我为什么要使用它。
答案 0 :(得分:2)
两个代码都包含不同的Map Reduce API。OutputCollector
位于MRV1中,Context
位于MRV2
Java Map Reduce API 1也称为MRV1,发布时带有初始hadoop版本,与这些初始版本相关的缺陷是map reduce框架执行处理任务和资源管理。
Map Reduce 2或下一代Map Reduce是一项期待已久且急需的升级,涉及与Hadoop中的调度,资源管理和执行相关的技术。从根本上说,这些改进将集群资源管理功能与Map Reduce特定逻辑分开,处理和资源管理的这种分离是通过在后续版本的HADOOP中启动YARN实现的。
MRV1使用OutputCollecter
和Reporter
与MapReduce系统进行通信。
MRV2使用API广泛使用允许用户代码与MapReduce系统通信的context
对象。 (来自旧API的JobConf,OutputCollector和Reporter的角色由MRV2中的上下文对象统一。)
使用应该使用mapreduce 2(MRV2)。我强调了hadoop 2相比hadoop最大的优势:
MRV2还有许多其他优点。 https://hadoop.apache.org/docs/r2.7.1/hadoop-yarn/hadoop-yarn-site/
答案 1 :(得分:-2)
这是一个很好的解决方案,但是,我只使用1行解决方案: int wordcount = string.split(“”)。length - 1;