MapReduce编程之排重

利用reducer的输入key是已经排重过的先天特性可以进行数据的排重
想对什么排重,就把它放到map的输出key上。
对单词进行排重
样例数据

王小花
孙建国 王小花 王小花 孙建国 王小花 王小花 赵文明 赵文明 赵文明 李建军 赵文明 孙建国
王小花 王小花 李建军 李建军 王小花 赵文明 赵文明 孙建国 孙建国 李建军 李建军 王小花 王小花 孙建国 赵文明 孙建国 王小花 孙建国 孙建国 王小花 王小花 孙建国 赵文明 李建军 李建军 孙建国 王小花 赵文明 赵文明 李建军 孙建国 李建军 赵文明 王小花 王小花 李建军 孙建国 李建军 孙建国 孙建国 赵文明 李建军 李建军
孙建国 王小花 李建军 王小花 王小花 李建军 王小花 孙建国 赵文明 王小花 王小花 王小花 赵文明 李建军 李建军 王小花 王小花
赵文明 王小花 王小花
李建军 孙建国 赵文明 赵文明 王小花 孙建国 李建军 赵文明 孙建国 李建军 李建军 赵文明 王小花 孙建国 王小花 王小花 孙建国 赵文明 孙建国 王小花
赵文明 王小花 王小花
赵文明

public class MyDemo1 extends Configured implements Tool {
	
	public static class MyMapper extends Mapper<LongWritable, Text, Text, NullWritable>{
		// strs存储文件中每行分割过的所有数据为数组
		private String[] strs = null;
		// 存储单个数据
		private Text outkey = new Text();
		private NullWritable outval = NullWritable.get();
		
		@Override
		protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, NullWritable>.Context context)
				throws IOException, InterruptedException {
			
			strs = value.toString().split(MyConstant.SPLIT_STR1);
			for (String string : strs) {
				outkey.set(string);
				context.write(outkey, outval);
			}
			
		}
		
	}
	
	public static class MyReducer extends Reducer<Text, NullWritable, Text, NullWritable>{
		@Override
		protected void reduce(Text outkey, Iterable<NullWritable> values,
				Reducer<Text, NullWritable, Text, NullWritable>.Context context) throws IOException, InterruptedException {
			// 写入map阶段已经排重过的单词
			context.write(outkey, NullWritable.get());
			
		}
	}
	

	@Override
	public int run(String[] args) throws Exception {
		// 获取配置文件
		Configuration conf = this.getConf();
		// 创建本次job
		try {
			Job job = Job.getInstance(conf, "MyJob");
			
			// 类设置
			job.setJarByClass(MyDemo1.class);
			job.setInputFormatClass(TextInputFormat.class);
			// 设置Mapper
			job.setMapperClass(MyMapper.class);
			// 设置Reducer
			job.setReducerClass(MyReducer.class);
			// Mapper和Reducer的输出相同
			job.setOutputKeyClass(Text.class);
			job.setOutputValueClass(NullWritable.class);
			
			job.setOutputFormatClass(TextOutputFormat.class);
			
			FileSystem fs = FileSystem.get(conf);
			
			Path in = new Path(args[0]);
			Path out = new Path(args[1]);
			
			if (fs.exists(out)) {
				fs.delete(out, true);
				System.out.println(job.getJobName() + "'s dir is deleted!");
			}
			
			// 设置输入输出目录
			
			FileInputFormat.addInputPath(job, in);
			FileOutputFormat.setOutputPath(job, out);
			
			Long start = System.currentTimeMillis();
			boolean con = job.waitForCompletion(true);
			Long end = System.currentTimeMillis();
			
			String msg = con?"success!":"error!";
			System.out.println(msg);
			System.out.println("time consuming:" + ((end - start)/1000) + "s");
			
		} catch (IOException e) {
			
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		
		return 0;
		
	}

	public static void main(String[] args) {
		try {
			System.exit(ToolRunner.run(new MyDemo1(), args));
		} catch (Exception e) {
			
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		
	}
}
posted @ 2020-05-24 10:39  我不是忘尘  阅读(135)  评论(0编辑  收藏  举报