在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,但我不明白其使用的关键或为什么我应该使用它。
我完全没有看到这与问题有关。 – justHelloWorld