Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MapReduce - How sort reduce output by value

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?

like image 688
Pietro Luciani Avatar asked Sep 09 '12 22:09

Pietro Luciani


1 Answers

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

like image 59
CL_Operator Avatar answered Nov 03 '22 02:11

CL_Operator