【发布时间】:2020-03-03 09:35:42
【问题描述】:
这是一个从 Apache 教程发布的字数统计作业实现的 sn-p
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);
}
}
}
重用Text word 字段有什么好处吗?
我已经在许多 Hadoop 程序中看到了这种做法,这个类的实例化如此繁重以至于重用会导致性能提升。如果不是,人们为什么要这样做,而不是像context.write(new Text(itr.nextToken()), one);
【问题讨论】:
-
相对于什么?
-
@PM77-1 重复使用 word 字段,而不是根据需要实例化 Text 对象
-
明确DRY原则。
-
@PM77-1 怎么样?没有重复。
-
也许这个问题在某种程度上也能帮助到你,stackoverflow.com/questions/26208454/…
标签: java performance hadoop