13

我正在尝试通过更改 hadoop 给出的 wordcount 示例来创建一个简单的 map reduce 作业。

我正在尝试列出一个列表而不是单词数。wordcount 示例给出以下输出

hello 2
world 2

我正在尝试将其作为列表输出,这将成为未来工作的基础

hello 1 1
world 1 1

我认为我在正确的轨道上,但我在编写列表时遇到了麻烦。而不是上面的,我得到

Hello   foo.MyArrayWritable@61250ff2
World   foo.MyArrayWritable@483a0ab1

这是我的 MyArrayWritable。我在其中放了一个系统,write(DataOuptut arg0)但它从不输出任何东西,所以我认为该方法可能不会被调用,我不知道为什么。

class MyArrayWritable extends ArrayWritable{

public MyArrayWritable(Class<? extends Writable> valueClass, Writable[] values) {
    super(valueClass, values);
}
public MyArrayWritable(Class<? extends Writable> valueClass) {
    super(valueClass);
}

@Override
public IntWritable[] get() {
    return (IntWritable[]) super.get();
}

@Override
public void write(DataOutput arg0) throws IOException {
    for(IntWritable i : get()){
        i.write(arg0);
    }
}
}

编辑 - 添加更多源代码

public class WordCount {

public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    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());
            context.write(word, one);
        }
    }
} 

public static class Reduce extends Reducer<Text, IntWritable, Text, MyArrayWritable> {

    public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        ArrayList<IntWritable> list = new ArrayList<IntWritable>();    
        for (IntWritable val : values) {
            list.add(val);
        }
        context.write(key, new MyArrayWritable(IntWritable.class, list.toArray(new IntWritable[list.size()])));
    }
}

public static void main(String[] args) throws Exception {
    if(args == null || args.length == 0)
        args = new String[]{"./wordcount/input","./wordcount/output"};
    Path p = new Path(args[1]);
    FileSystem fs = FileSystem.get(new Configuration());
    fs.exists(p);
    fs.delete(p, true);

    Configuration conf = new Configuration();

    Job job = new Job(conf, "wordcount");
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    job.setMapperClass(Map.class);
    job.setReducerClass(Reduce.class);
    job.setJarByClass(WordCount.class);
    job.setInputFormatClass(TextInputFormat.class);
    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));

    job.waitForCompletion(true);
}

}

4

1 回答 1

21

您的减速器中有一个“错误” - 值迭代器在整个循环中重复使用相同的 IntWritable,因此您应该将添加到列表中的值包装如下:

public void reduce(Text key, Iterable<IntWritable> values, Context context)
                                      throws IOException, InterruptedException {
    ArrayList<IntWritable> list = new ArrayList<IntWritable>();    
    for (IntWritable val : values) {
        list.add(new IntWritable(val));
    }
    context.write(key, new MyArrayWritable(IntWritable.class, list.toArray(new IntWritable[list.size()])));
}

这实际上不是问题,因为您使用的是数组列表,而您的映射器只输出一个值(一个),但如果您扩展此代码,这可能会让您感到困惑。

你还需要在你的工作中定义你的 map 和 reducer 输出类型是不同的:

// map output types
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
// reducer output types

job.setOutputValueClass(Text.class);
job.setOutputValueClass(MyArrayWritable.class);

您可能想要明确定义减速器的数量(这可能是您永远不会看到系统输出被写入任务日志的原因,特别是如果您的集群管理员已将默认数量定义为 0):

job.setNumReduceTasks(1);

您使用默认的文本输出格式,它在输出键和值对上调用 toString() - MyArrayWritable 没有覆盖的 toString 方法,因此您应该在 MyArrayWritable 中放置一个:

@Override
public String toString() {
  return Arrays.toString(get());
}

最后write从 MyArrayWritable 中删除被覆盖的方法 - 这不是与免费的 readFields 方法兼容的有效实现。您不需要重写此方法,但如果您这样做(假设您想查看 sysout 以验证它是否被调用),那么请执行以下操作:

@Override
public void write(DataOutput arg0) throws IOException {
  System.out.println("write method called");
  super.write(arg0);
}
于 2013-04-05T12:21:50.823 回答