How can I sort in decreasing order the reducer output by value? I'm developing an application that must return top listened songs. Thus songs must be ordered by the number of listening. My application works in this way:
Input: songname@userid@boolean
MapOutput : songname userid
ReduceOutput : songname number_of_listening
Any idea how to do this?
Best way to do it is to use the output of your first MapReduce job as the input of another job, which I call Sort.java. Since the Hadoop Map function has a sorting algorithm in place, you don't even need a reduce class. Just do something like this:
public static class Map extends Mapper<LongWritable,Text,IntWritable,Text>{
private Text word = new Text();
public void map(LongWritable key, Text value, Context context) throws IO Exception, Interrupted Exception{
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
word.set(tokenizer.nextToken());
IntWritable number = new IntWritable(Integer.parseInt(tokenizer.nextToken()));
context.write(number,word);
}
}
That will sort your [LongWritable,text] output of your first MapReduce by the LongWritable value. Let me know how it works!
CL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With