我们正在开展一个Hadoop项目,我们目前的任务是根据食物的时间将食物清单映射到感受。映射阶段工作,reduce阶段生成正确的输出,但调用带有该输出的Context.write()不会向文件写入任何内容。
检查密钥是否有效以及'输出'实际上有我们想要它写的文本,我们将字符串打印到System.out,它应该是它应该是什么。什么可能导致Context.write()不写? "文件输出格式计数器"确认没有写任何东西,因为" Bytes Written = 8"。那8个字节是文件名,我猜测。
这是我们不会写的Reducer类。样品输出将是#34; 417004发痒:虾甜菜大豆"
public static class HourReducer
extends Reducer<LongWritable, Text, LongWritable, Text> {
private Text output = new Text();
public void reduce(LongWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
Set<String> foods = new HashSet<>();
Set<String> feels = new HashSet<>();
for(Text item : values) {
String[] splitLine = item.toString().split(" ");
if(splitLine[0].equals("food")) {
foods.add(splitLine[1]);
}
else if(splitLine[0].equals("feel")) {
feels.add(splitLine[1]);
}
}
String foodList = "";
for(String food : foods) {
foodList += " " + food;
}
for(String feel : feels) {
System.out.println("WRITING at " + key + ": \"" + feel + ":" + foodList + "\"");
this.output.set(feel + ":" + foodList);
context.write(key, this.output);
}
}
}