Reducer代码,用于按字段分组单词

时间:2016-03-20 05:43:15

标签: java hadoop mapreduce iterator reducers

在MapReduce程序中,Reducer方法将Mapper的输入作为“Words”和它的长度。

 ex.input :-
         Hi -2
         how - 3
         are  -3 
         you - 3 
         ?   - 1

现在我需要以这样一种方式编写一个Reducer,它通过对“Word length”进行分组来提供输出,并且所有单词都在一个类别的基础上,基于单词'length如下

ex. Output :-
       1  - [?]
       2  - [hi]
       3  - [how, are, you]

这是我的Mapper程序:

public void map(LongWritable key, Text values, OutputCollector<Text, IntWritable> Output, Reporter arg3) throws IOException {
    String s = values.toString();

    for (String word : s.split(" ")) {
        if (word.length() > 0 ) {
            Output.collect(new Text(word), new IntWritable(word.length()));
        }

    }
}

Reduce计划如何?

1 个答案:

答案 0 :(得分:3)

如果你想让你的减速器按长度分组,你必须让映射器将长度作为键,将单词作为值发出,而不是:

     Hi -2
     how - 3
     are  -3 
     you - 3 
     ?   - 1

发射

     2 - Hi
     3 - how
     3 - are
     3 - you
     1 - ?

然后,您已将准备好的结果作为减速器的输入。根据您使用的系统,您可以完全关闭减速器或使用简单的标识功能。

相关问题