MapReduce - 如何按值减少排序

Pie*_*ani 5 sorting hadoop mapreduce

如何按减少顺序对减速器输出进行排序?我正在开发一个必须返回顶级收听歌曲的应用程序.因此歌曲必须按收听次数排序.我的应用程序以这种方式工作:

Input: songname@userid@boolean
MapOutput : songname userid
ReduceOutput : songname number_of_listening
Run Code Online (Sandbox Code Playgroud)

知道怎么做吗?

小智 5

最好的方法是使用第一个MapReduce作业的输出作为另一个作业的输入,我称之为Sort.java.由于Hadoop Map函数具有适当的排序算法,因此您甚至不需要reduce类.做这样的事情:

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);
   }     
}
Run Code Online (Sandbox Code Playgroud)

这将使您的第一个MapReduce的[LongWritable,text]输出按LongWritable值排序.让我知道它是如何工作的!

CL


hig*_*ted 3

根据文档,Reducer 输出不会重新排序。通过为JobConf.setOutputValueGroupingComparator(Class)设置适当的值,对减速器的输入进行排序(如果这适用于您的应用程序),或者仅在单独的步骤中对减速器的最终输出进行排序。