我正在使用 Hadoop 编写 mapreduce
在reduce方法中,我想使用context.write()。但是输出是int类型。
我怎样才能做到这一点?当我使用context.write()时,显示错误:

第二个参数不能为int。

这是我的代码:

public void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {

            int count = 0;

            for (NullWritable nullWritable : values) {
                count++;
            }

             //context.write(key, count);
}

这样可以减少计数。然后,它应该写键和计数变量。

我怎样才能做到这一点?

回答

我找到了答案。我应该新建一个IntWritable类,并使用其方法(set(intValue))。

像下面的代码:
IntWritable c = new IntWritable();
    public void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {

                int count = 0;


                for (NullWritable nullWritable : values) {
                    count++;
                }

                c.set(count);
                context.write(key, c);

            }

最佳答案

您也可以像这样使用它:

context.write(key,new IntWritable(count));

关于hadoop - 如何在hadoop reducer中写入int值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21061406/

10-10 13:45
查看更多