(*)什么是合并?在Map端先进行一次Reducer的操作,Combiner是一种特殊的Reducer
(*)好处:减少Map输出到Reducer中的数据量,从而提高性能
(*)举例:使用Combiner重写WordCount程序
(*)注意事项: ----> 编程:求平均值
谨慎使用!!!!
(1)并不是所有的问题都可以使用Combiner: 求平均值
(2)引入了Combiner后,不能改变原来的逻辑;如果改变了,想个办法,让他不改变。
Error: java.io.IOException: wrong value class: class org.apache.hadoop.io.DoubleWritable is not class org.apache.hadoop.io.LongWritable
==================================================================
package demo.wc.combiner;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
// k1 v1 k2 v2
public class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable>{
@Override
protected void map(LongWritable k1, Text v1, Context context)
throws IOException, InterruptedException {
/*
* context代表Map的上下文
* 上文:HDFS
* 下文是:Reducer
*/
//数据: I love Beijing
String data = v1.toString();
//分词
String[] words = data.split(" ");
//输出: k2 v2
for(String w:words){
context.write(new Text(w), new LongWritable(1));
}
}
}
-------------------------------------------------------------------------------------------------------
package demo.wc.combiner;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
// k3 v3 k4 v4
public class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
@Override
protected void reduce(Text k3, Iterable<LongWritable> v3,Context context) throws IOException, InterruptedException {
/*
* context 代表Reducer上下文
* 上文:mapper
* 下文:HDFS
*/
long total = 0;
for(LongWritable l:v3){
total = total + l.get();
}
//输出 k4 v4
context.write(k3, new LongWritable(total));
}
}
------------------------------------------------------------------------------------------------------
package demo.wc.combiner;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class WordCountMain {
public static void main(String[] args) throws Exception {
//创建一个job = mapper + reducer
Job job = Job.getInstance(new Configuration());
//指定job的入口
job.setJarByClass(WordCountMain.class);
//指定任务的mapper和输出数据类型
job.setMapperClass(WordCountMapper.class);
job.setMapOutputKeyClass(Text.class); //指定k2的类型
job.setMapOutputValueClass(LongWritable.class);//指定v2的数据类型
//设置任务的Combiner
job.setCombinerClass(WordCountReducer.class);
//指定任务的reducer和输出数据类型
job.setReducerClass(WordCountReducer.class);
job.setOutputKeyClass(Text.class);//指定k4的类型
job.setOutputValueClass(LongWritable.class);//指定v4的类型
//指定输入的路径和输出的路径
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
//执行任务
job.waitForCompletion(true);
}
}
=============================================================
MR的编程案例
1、去掉重复数据(distinct关键字):利用k2和k3的关系
SQL: select distinct deptno, job from emp;
distinct 作用于后面所有的列
package demo.distinct;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class DistinctMapper extends Mapper<LongWritable, Text, Text, NullWritable> {
@Override
protected void map(LongWritable key1, Text value1, Context context)
throws IOException, InterruptedException {
String str = value1.toString().trim();
context.write(new Text(str), NullWritable.get());
}
}
------------------------------------------------------------------------------------------
package demo.distinct;
import java.io.IOException;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class DistinctReducer extends Reducer<Text, NullWritable, Text, NullWritable> {
@Override
protected void reduce(Text k3, Iterable<NullWritable> v3,Context context) throws IOException, InterruptedException {
context.write(k3, NullWritable.get());
}
}
package demo.distinct;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class DistinctMain {
public static void main(String[] args) throws Exception {
//创建一个job = mapper + reducer
Job job = Job.getInstance(new Configuration());
//指定job的入口
job.setJarByClass(DistinctMain.class);
//指定任务的mapper和输出数据类型
job.setMapperClass(DistinctMapper.class);
job.setMapOutputKeyClass(Text.class); //指定k2的类型
job.setMapOutputValueClass(NullWritable.class);//指定v2的数据类型
//指定任务的reducer和输出数据类型
job.setReducerClass(DistinctReducer.class);
job.setOutputKeyClass(Text.class);//指定k4的类型
job.setOutputValueClass(NullWritable.class);//指定v4的类型
//指定输入的路径和输出的路径
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
//执行任务
job.waitForCompletion(true);
}
}