如何按值对减速器输出进行降序排序?我正在开发一个必须返回热门歌曲的应用程序。因此歌曲必须按收听次数排序。我的应用程序以这种方式工作:
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
根据文档,Reducer 输出不会重新排序。通过为JobConf.setOutputValueGroupingComparator(Class)设置适当的值对减速器的输入进行排序(如果这适用于您的应用程序),或者仅在单独的步骤中对减速器的最终输出进行排序。