java—如何确定减速机的值?

v1l68za4  于 2021-06-02  发布在  Hadoop
关注(0)|答案(2)|浏览(330)

我查了一张Map之后
键,值
1,白天,夜晚,白天
2,一天,一天
这些值将传递给减速器。我的减速机

import org.apache.hadoop.mapred.Reducer;

public class RTransactionPerPartOfDay implements Reducer<Text, Text, Text, IntWritable>{
    public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, IntWritable> outputCollector, Reporter reporter) throws IOException {
        IntWritable intWritable = new IntWritable();
        int transactionPerPartOfDayCount = 0;
        while(values.hasNext()){
            transactionPerPartOfDayCount += 1;
            values.next();
        }
        intWritable.set(transactionPerPartOfDayCount);
        outputCollector.collect(key, intWritable);
    }

提供以下输出
1,3
2,2
也就是说,我们遇到了13次键1和2次键2。我需要做什么使reducer计数键根据遇到值的次数分开?
这样地
1,1
1,2
2,2

5vf7fwbs

5vf7fwbs1#

reducer中的键值为1和2,当key为1时,值为{1,2},当key为2时,值为{2},请尝试以下操作:

while(values.hasNext()){
      transactionPerPartOfDayCount += Integer.parseInt(values.next());
}
u91tlkcl

u91tlkcl2#

解决方案#1

在Map输出中,使值成为键的一部分:

1 day, null
1 night, null
1 day, null
2 day, null
2 day, null

然后在reduce:

public class RTransactionPerPartOfDay implements Reducer<Text, NulLWritable, Text, IntWritable>{
    public void reduce(Text key, Iterator<NullWritable> values, OutputCollector<Text, IntWritable> outputCollector, Reporter reporter) throws IOException {
        String[] keyParts = key.toString().split(" ");
        int count = 0;
        for (NullWritable aValue : values) count++;
        outputCollector.collect(new Text(keyParts[0]), new IntWritable(count));
    }

解决方案#2

如果符合内存限制,则使用hashmap进行reduce计数。

相关问题