Hadoop还原器中的多个面孔循环



我遇到了哈德普(Hadoop

我现在为reducer类有哪种代码:

public class R_PreprocessAllSMS extends Reducer<Text, Text, Text, Text>{
private final static Text KEY = new Text();
private final static Text VALUE = new Text();
    @Override
    public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        int sum = 0;
        for (Text value : values) {
            String[] splitString = value.toString().split("t");
            sum += Integer.parseInt(splitString[1]);
        }
        if (sum > 100) {
            for (Text value : values) {
                String[] splitString = value.toString().split("t");
                System.out.println(key.toString() + splitString[0] + " " + splitString[1]);
                KEY.set(key);
                VALUE.set(splitString[0] + "t" + splitString[1]);
                context.write(KEY, VALUE);
            }
        }
    }
}

,但我想第二次搜索给定值并散发我们需要的值。如果不可能,您会建议您建议这样做的建议是什么?谢谢。

而不是循环两次,您可以延迟写入值,直到您知道总和足够高,类似于:

    int sum = 0;
    List list = new ArrayList<String>();
    KEY.set(key);
    for (Text value : values) {
        String[] splitString = value.toString().split("t");
        String line = splitString[0] + "t" + splitString[1];
        sum += Integer.parseInt(splitString[1]);
        if (sum < 100) {
            list.add(line);
        } else {
            if (!list.isEmpty()) {
                for (String val: list) {
                   VALUE.set(val);
                   context.write(KEY, VALUE);
                }
                list.clear();
            }
            VALUE.set(line);
            context.write(KEY, VALUE);
        }
    }

也许使用两对映射和还原器?您可以一个接一个地称他们为一个。例如,在一个主中创建两个作业。第二获得第一。

JobConf jobConf1 = new JobConf();  
JobConf jobConf2 = new JobConf();  
Job job1 = new Job(jobConf1);  
Job job2 = new Job(jobConf2);

或tou可以看:http://hadoop.apache.org/docs/current/api/org/org/apache/hadoop/mapred/mapred/lib/chainreducer.html

最新更新