1
下面是从的Hadoop字计数示例代码示例:Hadoop的文本类设置方法
class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
private Text outputKey;
private IntWritable outputVal;
@Override
public void setup(Context context) {
outputKey = new Text();
outputVal = new IntWritable(1);
}
@Override
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer stk = new StringTokenizer(value.toString());
while(stk.hasMoreTokens()) {
outputKey.set(stk.nextToken());
context.write(outputKey, outputVal);
}
}
}
只有一个outputKey
实例。在while
循环中,outputKey
设置不同的单词并被写为context
的密钥。在整个<key, value>
对内是否共享outputKey
实例?
为什么不使用context.write(new Text(stk.nextToken()), new IntWritable(1))
?