Rama
Rama

Reputation: 875

Reducer code for grouping the words by their lenght

In MapReduce Program, Reducer method is taking a inputs form Mapper as "Words" and it's length.

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

Now I need to write a Reducer in such a way that it gives a output by grouping "Word length" and all words comes under in one category on the basis of word' length as below

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

Here is my Mapper program :

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()));
        }

    }
}

How is a Reduce program ?

Upvotes: 0

Views: 253

Answers (1)

mtj
mtj

Reputation: 3554

If you want your reducer to group by the length, you'll have to have the mapper emit the length as keys and the words as values, thus instead of:

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

emit

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

Then, you have the ready result as input to the reducer already. Depending on the system you use, you can switch off the reducer alltogether or use a simply identity function.

Upvotes: 3

Related Questions