gpt4 book ai didi

Hadoop reducer 未被调用

转载 作者:可可西里 更新时间:2023-11-01 14:16:35 36 4
gpt4 key购买 nike

全部

我有简单的 map/reduce 实现。 Mapper 被调用并完成其工作,但 reducer 从未被调用。

这是映射器:

static public class InteractionMap extends Mapper<LongWritable, Text, Text, InteractionWritable> {

@Override
protected void map(LongWritable offset, Text text, Context context) throws IOException, InterruptedException {
System.out.println("mapper");
String[] tokens = text.toString().split(",");
for (int idx = 0; idx < tokens.length; idx++) {
String sourceUser = tokens[1];
String targetUser = tokens[2];
int points = Integer.parseInt(tokens[4]);
context.write(new Text(sourceUser), new InteractionWritable(targetUser, points));
}
}
}
}

这是我的 reducer :

static public class InteractionReduce extends Reducer<Text, InteractionWritable, Text, Text> {

@Override
protected void reduce(Text token, Iterable<InteractionWritable> counts, Context context) throws IOException, InterruptedException {
System.out.println("REDUCER");
Iterator<InteractionWritable> i = counts.iterator();
while (i.hasNext()) {
InteractionWritable interaction = i.next();
context.write(token, new Text(token.toString() + " " + interaction.getTargetUser().toString() + " " + interaction.getPoints().get()));
}
}

}

还有,这里是配置部分:

@Override
public int run(String[] args) throws Exception {
Configuration configuration = getConf();
Job job = new Job(configuration, "Interaction Count");
job.setJarByClass(InteractionMapReduce.class);
job.setMapperClass(InteractionMap.class);
job.setCombinerClass(InteractionReduce.class);
job.setReducerClass(InteractionReduce.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
return job.waitForCompletion(true) ? 0 : -1;
}

有谁知道为什么没有调用 reducer?

最佳答案

好吧,果然是我的错。工作配置不好。它应该是这样的:

Configuration configuration = getConf();

Job job = new Job(configuration, "Interaction Count");
job.setJarByClass(InteractionMapReduce.class);
job.setMapperClass(InteractionMap.class);
job.setReducerClass(InteractionReduce.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(InteractionWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);

FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));

return job.waitForCompletion(true) ? 0 : -1;

问题的发生是因为 map 和 reduce 阶段的输出类型不同。作业在调用 context.write 方法后静默失败。所以,我必须添加的是这些行:

job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(InteractionWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);

关于Hadoop reducer 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12676505/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com