MapReduce之topN

停不下的腳步發表於2015-01-29
適用場景:
1.這個模式需要一個比較兩條記錄的比較函式。也就是說,我們必須得通過比較確認兩條記錄中哪一個更大一些。
2.輸出記錄數相對於輸入記錄數將會是異常的小,否則獲得整個資料集的全排序將會更有意義。

結構:
這個模式同時使用了mapper和reducer。mapper任務找出其本地的top K,然後所有獨立的top K集合在reducer中做最後的top K運算。因為在mapper中輸出的資料記錄最多是K條,而K通常相對較小,所以我們只需要一個reducer來處理最後的運算。

效能分析:
top 10模式的效能通常是很好的,但是有一些重要的侷限性需要考慮。大多數侷限性都來源於:不管這個模式需要處理的記錄數有多少,它都只能使用一個reducer。需要特別注意的是:單一reducer需要處理的記錄數目。每個map任務會輸出K條記錄,當這個作業由M個map任務組成時,這個reducer需要處理K*M條記錄,這個值可能很大,

下面是map reduce寫的top 10示例
輸入檔案如下:
10 9 8 7 6 5 1 2 3 4
11 12 13 14 15 20 19 18 17 16
最終得到如下結果:


我們來看程式碼:
package com.mr.top10;

import java.io.IOException;
import java.net.URI;
import java.util.StringTokenizer;
import java.util.TreeMap;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
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.output.FileOutputFormat;

/**
 * @author luobao
 *
 */
public class Top10 {
	public static class TopTenMapper extends
			Mapper<Object, Text, NullWritable, IntWritable> {
		private TreeMap<Integer, String> repToRecordMap = new TreeMap<Integer, String>();

		public void map(Object key, Text value, Context context) {
			StringTokenizer itr = new StringTokenizer(value.toString());
			while (itr.hasMoreTokens()) {
				repToRecordMap.put(Integer.parseInt(itr.nextToken()), " ");
				if (repToRecordMap.size() > 10) {
					repToRecordMap.remove(repToRecordMap.firstKey());
				}
			}
		}

		protected void cleanup(Context context) {
			for (Integer i : repToRecordMap.keySet()) {
				try {
					context.write(NullWritable.get(), new IntWritable(i));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static class TopTenReducer extends
			Reducer<NullWritable, IntWritable, NullWritable, IntWritable> {
		private TreeMap<Integer, String> repToRecordMap = new TreeMap<Integer, String>();

		public void reduce(NullWritable key, Iterable<IntWritable> values,
				Context context) throws IOException, InterruptedException {
			for (IntWritable value : values) {
				repToRecordMap.put(value.get(), " ");
				if (repToRecordMap.size() > 10) {
					repToRecordMap.remove(repToRecordMap.firstKey());
				}
			}
			for (Integer i : repToRecordMap.keySet()) {
				context.write(NullWritable.get(), new IntWritable(i));
			}
		}
	}

	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		String ipPre = "hdfs://192.168.40.191:9000/";
		removeOutput(conf, ipPre);
		Job job = Job.getInstance(conf);
		job.setJarByClass(Top10.class);
		job.setMapperClass(TopTenMapper.class);
		job.setReducerClass(TopTenReducer.class);
		job.setNumReduceTasks(1);

		job.setMapOutputKeyClass(NullWritable.class);// map階段的輸出的key
		job.setMapOutputValueClass(IntWritable.class);// map階段的輸出的value

		job.setOutputKeyClass(Text.class);// reduce階段的輸出的key
		job.setOutputValueClass(IntWritable.class);// reduce階段的輸出的value

		FileInputFormat.addInputPath(job, new Path(ipPre + "input/top10"));
		FileOutputFormat.setOutputPath(job, new Path(ipPre + "output"));
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}

	private static void removeOutput(Configuration conf, String ipPre)
			throws IOException {
		String outputPath = ipPre + "output";
		FileSystem fs = FileSystem.get(URI.create(outputPath), conf);
		Path path = new Path(outputPath);
		if (fs.exists(path)) {
			fs.deleteOnExit(path);
		}
		fs.close();
	}
}


相關文章