gpt4 book ai didi

java - hadoop java : how to know that end of reducer input is reached?

转载 作者:可可西里 更新时间:2023-11-01 16:34:38 26 4
gpt4 key购买 nike

我的reducer是这样的

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

List<Text> allRecords = new ArrayList<Text>();

public void reduce(IntWritable key, Iterator<Text> values, OutputCollector<IntWritable, Text> output, Reporter reporter) throws IOException {

allRecords.add(values.next());
Text[] outputValues = new Text[7];
for (int i=1; i>=7; i++) {
outputValues[i-1] = allRecords.get(allRecords.size() - i);
}
}
}
  • 我只有一个 reducer 。
  • 当 reducer 完成工作时,我需要收集前 7 条记录。

问题

  • 我如何知道收到 reducer 输入的结尾
    谢谢

最佳答案

我认为您误解了为映射的每个值写出的键的用途。 key 的目的是将元素分组到对 reducer 的特定调用中。由于您希望同时考虑代码中的所有值,因此您只需使用一个键,如下所示:

public class MyMapper<K extends WritableComparable, V extends Writable> 
extends MapReduceBase implements Mapper<IntWriteable, WhateverTheInputTypeWas,
IntWriteable, Text> {
public void map(IntWriteable key, WhateverTheInputTypeWas val,
OutputCollector<IntWriteable, Text> output, Reporter reporter)

// do some processing
output.collect(new IntWriteable(1), ...);
}
}

基础架构会自动收集特定键的所有值,并在对 reduce 的一次调用中显示它们。这就是为什么 reduce 采用 Iterator 值,而不仅仅是单个值。您需要做的就是遍历整个迭代器,当 hasNext() 返回 false 时,这就是您到达 reduce 函数输入的末尾的时候键。

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

public void reduce(IntWritable key, Iterator<Text> values,
OutputCollector<IntWritable, Text> output,
Reporter reporter) throws IOException {

int i=0
Text[] outputValues = new Text[7];
while (values.hasNext() && i < 7) {
outputValues[i++] = values.next();
}
// now output the contents of outputValues to the OutputCollector
}
}

如果您在 reducer 中进行的某些其他计算需要不同的键,也只需从映射器输出这些键,并有一个特殊的哨兵值(可能是 -1,取决于您的键的含义)为映射的每个数据元素输出,然后仅当键等于标记值时才运行此特殊逻辑。

关于java - hadoop java : how to know that end of reducer input is reached?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9203740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com