hadoop----mapreduce的案例(四)(利用shuffle进行排序)

该博客介绍了如何使用Hadoop MapReduce进行数据排序,特别是利用Shuffle阶段对key进行排序。同时,文章还展示了如何在MapReduce程序中去除运行结果的重复数据,通过NullWritable实现此功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

案例四

使用MapReduce程序读取三个文件中的数据,进行排序(利用shuffle阶段的排序)
注意:shuffle阶段的排序是针对key进行排序的

在这里插入图片描述
在这里插入图片描述

map函数

public class MyMapper extends Mapper<LongWritable, Text, LongWritable, LongWritable>{

	@Override
	protected void map(LongWritable key, Text value, Context context)
			throws IOException, InterruptedException {
		LongWritable a = new LongWritable();
		long i = Long.parseLong(value.toString());
		a.set(i);
		context.write(a, new LongWritable(1));
	}
}

reduce函数

public class MyReducer extends Reducer<LongWritable, LongWritable, LongWritable, LongWritable>{

	@Override
	protected void reduce(LongWritable key, Iterable<LongWritable> vs,Context context) throws IOException, InterruptedException {
		for(LongWritable value:vs) {
			context.write(key, value);
		}
	}
}

驱动类

public class MyDriver {
	public static void main(String[] args) throws Exception, IOException {
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(conf);
		Path path = new Path("E:/data/sort/output");
		if(fs.exists(path)){
			fs.delete(path);
		}
		
		Job job = Job.getInstance();
		job.setJobName("sort");
		
		job.setJarByClass(MyDriver.class);
		job.setMapperClass(MyMapper.class);
		job.setReducerClass(MyReducer.class);
		job.setOutputKeyClass(LongWritable.class);
		job.setOutputValueClass(LongWritable.class);
		
		FileInputFormat.addInputPath(job, new Path("E:/data/sort/input/f*"));
		FileOutputFormat.setOutputPath(job, new Path("E:/data/sort/output"));
		
		System.exit(job.waitForCompletion(true)?0:1);
		
	}
}

运行结果
在这里插入图片描述

对案例中运行结果数据去重(使用了NullWritable)

map函数

public class MyMapper extends Mapper<LongWritable, Text, LongWritable, NullWritable>{

	@Override
	protected void map(LongWritable key, Text value, Context context)
			throws IOException, InterruptedException {
		LongWritable a = new LongWritable();
		long i = Long.parseLong(value.toString());
		a.set(i);
		context.write(a, NullWritable.get());
	}
}

reduce函数

public class MyReducer extends Reducer<LongWritable, NullWritable, LongWritable, NullWritable>{

	@Override
	protected void reduce(LongWritable key, Iterable<NullWritable> vs,Context context) throws IOException, InterruptedException {
		context.write(key, NullWritable.get());
	}
}

驱动类

public class MyDriver {
	public static void main(String[] args) throws Exception, IOException {
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(conf);
		Path path = new Path("E:/data/sort/output1");
		if(fs.exists(path)){
			fs.delete(path);
		}
		
		Job job = Job.getInstance();
		job.setJobName("sort");
		
		job.setJarByClass(MyDriver.class);
		
		job.setMapperClass(MyMapper.class);
		job.setReducerClass(MyReducer.class);
		
		job.setOutputKeyClass(LongWritable.class);
		job.setOutputValueClass(NullWritable.class);
		
		FileInputFormat.addInputPath(job, new Path("E:/data/sort/input/f*"));
		FileOutputFormat.setOutputPath(job, new Path("E:/data/sort/output1"));
		
		System.exit(job.waitForCompletion(true)?0:1);
		
	}
}

运行结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值