我编写了一个小型地图程序来读取日志文件并查找名为“extract”的单词。只有找到单词时,才应将该行写入上下文对象。但不知何故,我看到输出文件中的所有行。这是我的代码
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
if(word.find("extract") >= -1) {
context.write(word, null);
}
}
}
你可以告诉我我做错了什么吗?
谢谢,aarthi
答案 0 :(得分:1)
如果您想将该行写入上下文;这是代码模板
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
if (line.contains("extract")) {
context.write(value,null);
}
}