网上搜索到的那个top K问题的解法,我觉得有些地方都没有讲明白。因为我们要找出top K, 那么就应该显式的指明the num of reduce tasks is one.
不然我还真不好理解为什么可以得到top K的结果。这里顺便提及一下,一个map task就是一个进程。有几个map task就有几个中间文件,有几个reduce task就有几个最终输出文件。好了,这就好理解了,我们要找的top K 是指的全局的前K条数据,那么不管中间有几个map, reduce最终只能有一个reduce来汇总数据,输出top K。
下面写出思路和代码:
1. Mappers
使用默认的mapper数据,一个input split(输入分片)由一个mapper来处理。
在每一个map task中,我们找到这个input split的前k个记录。这里我们用TreeMap这个数据结构来保存top K的数据,这样便于更新。下一步,我们来加入新记录到TreeMap中去(这里的TreeMap我感觉就是个大顶堆)。在map中,我们对每一条记录都尝试去更新TreeMap,最后我们得到的就是这个分片中的local top k的k个值。在这里要提醒一下,以往的mapper中,我们都是处理一条数据之后就context.write或者output.collector一次。而在这里不是,这里是把所有这个input split的数据处理完之后再进行写入。所以,我们可以把这个context.write放在cleanup里执行。cleanup就是整个mapper task执行完之后会执行的一个函数。
2.reducers
由于我前面讲了很清楚了,这里只有一个reducer,就是对mapper输出的数据进行再一次汇总,选出其中的top k,即可达到我们的目的。Note that we are using NullWritable here. The reason for this is we want all of the outputs from all of the mappers to be grouped into a single key in the reducer.
- package jtlyuan.csdn;
- import java.io.IOException;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.conf.Configured;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.LongWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Job;
- import org.apache.hadoop.mapreduce.Mapper;
- import org.apache.hadoop.mapreduce.Reducer;
- import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
- import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
- import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
- import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
- import org.apache.hadoop.util.Tool;
- import org.apache.hadoop.util.ToolRunner;
- //利用MapReduce求最大值海量数据中的K个数
- public class TopKNum extends Configured implements Tool {
- public static class MapClass extends Mapper<LongWritable, Text, IntWritable, IntWritable> {
- public static final int K = 100;
- private int[] top = new int[K];
- public void map(LongWritable key, Text value, Context context)
- throws IOException, InterruptedException {
- String[] str = value.toString().split(",", -2);
- try {// 对于非数字字符我们忽略掉
- int temp = Integer.parseInt(str[8]);
- add(temp);
- } catch (NumberFormatException e) {
- }
- }
- private void add(int temp) {//实现插入
- if(temp>top[0]){
- top[0]=temp;
- int i=0;
- for(;i<99&&temp>top[i+1];i++){
- top[i]=top[i+1];
- }
- top[i]=temp;
- }
- }
- @Override
- protected void cleanup(Context context) throws IOException,
- InterruptedException {
- for(int i=0;i<100;i++){
- context.write(new IntWritable(top[i]), new IntWritable(top[i]));
- }
- }
- }
- public static class Reduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {
- public static final int K = 100;
- private int[] top = new int[K];
- public void reduce(IntWritable key, Iterable<IntWritable> values, Context context)
- throws IOException, InterruptedException {
- for (IntWritable val : values) {
- add(val.get());
- }
- }
- private void add(int temp) {//实现插入if(temp>top[0]){
- top[0]=temp;
- int i=0;
- for(;i<99&&temp>top[i+1];i++){
- top[i]=top[i+1];
- }
- top[i]=temp;
- }
- }
- @Override
- protected void cleanup(Context context) throws IOException,
- InterruptedException {
- for(int i=0;i<100;i++){
- context.write(new IntWritable(top[i]), new IntWritable(top[i]));
- }
- }
- }
- public int run(String[] args) throws Exception {
- Configuration conf = getConf();
- Job job = new Job(conf, "TopKNum");
- job.setJarByClass(TopKNum.class);
- FileInputFormat.setInputPaths(job, new Path(args[0]));
- FileOutputFormat.setOutputPath(job, new Path(args[1]));
- job.setMapperClass(MapClass.class);
- job.setCombinerClass(Reduce.class);
- job.setReducerClass(Reduce.class);
- job.setInputFormatClass(TextInputFormat.class);
- job.setOutputFormatClass(TextOutputFormat.class);
- job.setOutputKeyClass(IntWritable.class);
- job.setOutputValueClass(IntWritable.class);
- System.exit(job.waitForCompletion(true) ? 0 : 1);
- return 0;
- }
- public static void main(String[] args) throws Exception {
- int res = ToolRunner.run(new Configuration(), new TopKNum(), args);
- System.exit(res);
- }
- }
- /*
- * 列举一部分出来:
- * 306 306
- 307 307
- 309 309
- 313 313
- 320 320
- 346 346
- 348 348
- 393 393
- 394 394
- 472 472
- 642 642
- 706 706
- 868 868
- */