【发布时间】:2010-08-14 03:44:42
【问题描述】:
我正在尝试使用 hadoop 查找任何给定点的总和,我遇到的问题是从单个减速器中的给定键获取所有值。看起来像这样。
减速机:
public static class Reduce extends MapReduceBase implements
Reducer<Text, IntWritable, Text, DoubleWritable> {
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, DoubleWritable> output, Reporter reporter)
throws IOException {
Text word = new Text();
Iterator<IntWritable> tr = values;
IntWritable v;
while (tr.hasNext()) {
v = tr.next();
Iterator<IntWritable> td = values;
while (td.hasNext()) {
IntWritable u = td.next();
double sum = u+v;
word.set( u + " + " + v);
output.collect(word, new DoubleWritable(sum));
}
}
}
}
我正在尝试创建 Iterator 变量的两个副本,以便我可以遍历第二个迭代器的所有值,同时从前一个 Iterator 获得单个值(上面的两个 while 循环)但是两个迭代器持有始终相同的值。
我不确定这是否是正确的做法。
【问题讨论】:
-
我也在试图找出类似的问题。我需要在 reduce 函数中检查记录两次。我正在使用带有 python 的 hadoop 流,并且不知道如何为 reducer 中的记录倒带迭代器。
-
根据定义,迭代器只向一个方向移动。所以一旦你做了 .next() 你推进了它,你持有的迭代器的任何其他实例现在也将指向下一个值。这不是 Hadoop 特有的。当您说“尝试创建 Iterator 变量的两个副本”时,您实际上并没有创建任何副本,正如@casper 所说,它们都是相同的实例。也就是说,map-reduce 可能不适合解决这个问题,一种方法是在某处编写 reducer 输出后,在 M/R 之外运行嵌套的 while 循环。
标签: hadoop mapreduce parallel-processing