我刚刚开始学习Hadoop,并且仍在试验和尝试理解事情,我对OutputCollector类collect()方法的使用非常好奇,因为现在我发现的所有示例都只调用一次该方法。如果这种方法的调用成本非常高(因为它将输出写入文件)?同时考虑到我遇到的情况不同,我发现需要多次调用它。像明智下面是给定的代码段在Hadoop中使用context.write()或outputCollector.collect()编写输出的代价?
public static class Reduce extends MapReduceBase implements
Reducer<IntWritable, Text, Text, NullWritable> {
public void reduce(IntWritable key, Iterator<Text> values,
OutputCollector<Text, NullWritable> output, Reporter reporter)
throws IOException {
Text outData = null;
while (values.hasNext()) {
outData = new Text();
outData.set(values.next().toString());
output.collect(outData, NullWritable.get());
}
}
}
作为values
对象包含大量的其映射器根据一些过滤条件已经发出记录和我需要编写这些记录到输出file.OR周围的其他方法我也可以使用下面给出的方法。
public static class Reduce extends MapReduceBase implements
Reducer<IntWritable, Text, Text, NullWritable> {
public void reduce(IntWritable key, Iterator<Text> values,
OutputCollector<Text, NullWritable> output, Reporter reporter)
throws IOException {
StringBuilder sb = new StringBuilder();
while (values.hasNext()) {
sb.append(values.next().toString() + "\r\n ");
}
Text outData = new Text();
outData.set(sb.toString());
output.collect(outData, NullWritable.get());
}
}
但是这两种方法适用于我的singlenode设置为大的输入细数据集的高达40万条记录,并含有约70K的记录values
对象。我想问问哪种方法更好?以及上面的书面代码在多节点集群上的表现还会好吗?任何帮助赞赏。谢谢。
感谢@Thomas,所以根据collect方法的功能,在这种写入Mb或更多数据的场景中,它似乎更有效率。 – 2014-09-24 06:00:04
是的,但这确实是一个普遍的建议,并且hadoop会照顾不会泄漏一个小缓冲区。 – 2014-09-24 07:17:20
好的,非常感谢。 – 2014-09-24 07:21:28