Hadoop减速器没有被调用

时间:2012-10-01 15:40:46

标签: hadoop mapreduce

所有

我有简单的map / reduce实现。调用Mapper并完成其工作,但永远不会调用reducer。

这是mapper:

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));
            }
        }
    }
}

这是我的减速机:

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?

2 个答案:

答案 0 :(得分:6)

好的,这是我的错,正如所料。工作配置不好。 这就是它应该是这样的:

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);

答案 1 :(得分:0)

  1. 我希望text方法中的Mapper包含一些数据。
  2. 您真的需要Reducer成为Combiner以及Reducer吗?
  3. 我总是有一个主要课程InteractionMapReduce,在其中我有InteractionMapInteractionReduce课程。

    因此,在作业中设置MapperReducer类时,我将其设置为InteractionMapReduce.InteractionMap.classInteractionMapReduce.InteractionReduce.class

    我不知道这对你有帮助,但你可以尝试一下。