MapReduce: combiner

本文详细介绍了Hadoop MapReduce框架中Combiner的作用及其使用场景,通过具体实例展示了如何利用Combiner来减少Reduce任务的数据传输量,进而提高整体处理效率。

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

1、什么是combiner?

combiner就是规约操作,通过对map输出的数量进行规约,可以减少reduce的数量,提高执行效率combiner的输入输出类型必须和mapper的输出以及reducer的输入类型一致

2、什么情况要使用 combiner,什么情况不使用?

求平均数的时候就不需要用combiner,因为不会减少reduce执行数量。在其他的时候,可以依据情况,使用combiner,来减少map的输出数量,减少拷贝到reduce的文件,从而减轻reduce的压力,节省网络开销,提升执行效率

3、combine出现在哪个过程

map阶段的最后一个过程。

4、combine代码实现

/***
 * 
 * <p>Description: 减少Reduce的压力,设置在job.setCombinerClass(WordCountReducer.class);中</p>
 * @author	余辉
 * @date	2016年3月14日下午4:31:10
 * @version 1.0
 */
public class WordCountCombiner extends Reducer<Text, IntWritable, Text, IntWritable>{
	
	@Override
	protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
		//定义一个计数器
		int count = 0;
		//遍历这一组kv的所有v,累加到count中
		for(IntWritable value:values){
			count += value.get();
		}
		context.write(key, new IntWritable(count));
		
	}
	

}
public class WordCountRunner {
	
	static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
		
		
		protected void map(LongWritable key, Text value, Context context ) throws IOException, InterruptedException{
			
			String line = value.toString();
			
	        String[] words = StringUtils.split(line, " ");
	        
	        for(String word : words){
	        	context.write(new Text(word), new IntWritable(1));
	        }
			
		}
	}


	static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
		
		protected void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException{
			
			int counter = 0;
			
			for(IntWritable value:values){
				
				//累加每一个value
				counter += value.get();
				
			}
			
			context.write(key, new IntWritable(counter));
		}	
		
	}
	
	
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

		//封装任务信息的对象为Job对象,所以要先构造一个Job对象
				Configuration conf = new Configuration();
				Job job = Job.getInstance(conf);
				
				//设置本次job作业所在的jar包
				job.setJarByClass(WordCountRunner.class);
				
				//本次job作业使用的mapper类是哪个?
				job.setMapperClass(WordCountMapper.class);
				//本次job作业使用的reducer类是哪个?
				job.setReducerClass(WordCountReducer.class);
				
				//指定自定义的combiner类
				job.setCombinerClass(WordCountReducer.class);
				
						
				//本次job作业mapper类的输出数据key类型
				job.setMapOutputKeyClass(Text.class);
				//本次job作业mapper类的输出数据value类型
				job.setMapOutputValueClass(IntWritable.class);
				
				//本次job作业reducer类的输出数据key类型
				job.setOutputKeyClass(Text.class);
				//本次job作业reducer类的输出数据value类型
				job.setOutputValueClass(IntWritable.class);
				
				//本次job作业要处理的原始数据所在的路径
				FileInputFormat.setInputPaths(job,  new Path("/home/hadoop/Desktop/input"));
				//本次job作业产生的结果输出路径
				FileOutputFormat.setOutputPath(job, new Path("/home/hadoop/Desktop/output"));		
				//提交本次作业
				job.waitForCompletion(true);

		
	}

}
北京小辉微信公众号

在这里插入图片描述

大数据资料分享请关注
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

辉哥大数据

你的鼓舞将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值