Combiner组件

combiner
发生在map和reduce之间的,用于优化的一个组件,提升mapreduce性能
减少shuffle端的数据量,帮reduce分担压力,和reduce逻辑一样
适用场景:

求和
最大值
最小值

不适用的场景:

平均值

写法:

1.继承reducer类
2.重写reduce方法
3.在job中指定

使用之前的wordcount来进行简单的combiner组件的使用
编写combiner类

package pro1011;
//编写combiner类
import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
/*
 * 发生在map-reduce之间
 * 四个 泛型中,前两个泛型对应map的输出,后两个 泛型 对应reduce的输入
 */
public class CombinerTest extends Reducer<Text, IntWritable, Text, IntWritable> {
	IntWritable iw = new IntWritable();
	@Override
	protected void reduce(Text key, Iterable<IntWritable> values,
			Reducer<Text, IntWritable, Text, IntWritable>.Context context)
					throws IOException, InterruptedException {	
		//num用来计数
		int num =0;
		//values是一个迭代器,里面存着一个单词的出现的次数<1,1,....> ,我们计算出迭代器里元素个数就可以得出单词出现的频率了
		for(IntWritable i:values){
			num+=i.get();
		}
		iw.set(num);
		context.write(key, iw);
	
	}

}

使用之前的wordcount,只需要把job设置指定combiner类就可以了

package mrpro928;

import java.io.IOException;

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

import pro1011.CombinerTest;

public class WordCountMPManyJob {
	
	//写一个内部类,继承Mapper 类
	public static class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
		@Override
		protected void map(LongWritable key, Text value,
				Mapper<LongWritable, Text, Text, IntWritable>.Context context)
				throws IOException, InterruptedException {
			//逐行读取,内容在value中
			//Text对象转String用toString()
			//String对象转Text用new
			String line= value.toString();
			String[] split = line.split("\t");
			for(String s:split){
				Text t = new Text(s);
				//int转IntWritable用new
				//IntWritable 转int 用.get(),hadoop 转java基本类型都是.get()
				IntWritable i = new IntWritable(1);
				//map写出的k,v,一个是Text类型,一个是IntWritable类型
				context.write(t, i);
			}
		}
		
	}
	
	//写一个内部类,继承Reducer类,当然这些类都可以写出来
	//输入的k,v是map输出的k,v
	//相同的key会被框架分为一组
	public static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
		@Override
		protected void reduce(Text key, Iterable<IntWritable> values,
				Reducer<Text, IntWritable, Text, IntWritable>.Context context)
						throws IOException, InterruptedException {
			//num用来计数
			int num =0;
			//values是一个迭代器,里面存着一个单词的出现的次数<1,1,....> ,我们计算出迭代器里元素个数就可以得出单词出现的频率了
			for(IntWritable i:values){
				num+=i.get();
			}
			context.write(key, new IntWritable(num));
		}
	}
	
	//main方法写一下job的配置,当然也可以另外写一个driver类
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		//加载配置文件
		Configuration conf = new Configuration();
		//eclipse运行设置linux用户名
		System.setProperty("HADOOP_USER_NAME", "mading");
		//启动一个job
		Job job = Job.getInstance(conf);
		//指定当前任务的主类
		job.setJarByClass(WordCountMPManyJob.class);
		
		//指定combiner的类
		job.setCombinerClass(CombinerTest.class);
		
		
		
		//指定mapper和reducer类
		job.setMapperClass(MyMapper.class);
		job.setReducerClass(MyReducer.class);
		//指定map输出的key,value类型,如果和reduce的输出类型相同的情况下可以省略
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		//指定reduce输出的key,value类型
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		//指定文件输入的路径,这里是HA高可用集群的路径
		FileInputFormat.addInputPath(job, new Path("hdfs://master:9000/in"));
		//指定文件的输出路径
		FileOutputFormat.setOutputPath(job, new Path("hdfs://master:9000/combout01"));
		//提交job
		job.waitForCompletion(true);
		
		
		
	}
	
	
	
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值