如何按值降序对减速器输出进行排序?
我正在开发一个必须返回最常听的歌曲的应用程序。因此,歌曲必须按照收听次数排序。
我的应用程序以这种方式工作:
Input: songname@userid@boolean
MapOutput : songname userid
ReduceOutput : songname number_of_listening
知道如何做到这一点吗?
最好的方法是使用第一个 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);
}
}
这将按 LongWritable 值对第一个 MapReduce 的 [LongWritable,text] 输出进行排序。让我知道它是如何工作的!
CL
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)