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();
			
		}
		
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值