作者为新手小白,只为记录学习&交流
如任何读者有任何正面建议,欢迎留言&私信,不胜感激
内容原创 侵删致歉
2020年4月12日20:02:42
文章目录
本文记录作者在Hadoop学习过程中对Combiner合并的理解
Combiner合并
(1) Combiner 是MR程序中Mapper和Reducer之外的一种组件。
(2) Combiner组件的父类就 是Reducer。
(3) Combiner和R educer的区别在于运行的位置
Combiner是在每一-个MapTask所在的节点运行;Reducer是接收全局所有Mapper的输出结果;
(4) Combiner的意义就是对每一个Map Task的输出进行局部汇总,以减小网络传输量。
(5) Combiner能够 应用的前提是不能影响最终的业务逻辑,而且,Combiner的输出kv应该跟R educer的输入kv类型要对应起来。
也就是说: Combiner是在map结束阶段对map的结果进行一次局部reduce,但是并不能改变map的输出结果。所以即使和真正的reduce阶段执行的任务相同也不能在Driver类中直接指定Combiner类为Reducer。除非map和reduce阶段的输入输出类型完全相同
Combiner的作用(个人理解)
由于是大数据,可能处理数以亿级的数据量,那么在map阶段每个map都会产生数量巨大的K-V值,对应会产生大量的IO操作,Combiner就是为了减少IO操作,可以理解为在map结束时先进行一次reduce
任务描述
统计全球每年的最高气温和最低气温。输出结果包含年份、最高气温、最低气温
开发环境
OS: Win10
Hadoop版本: Hadoop-3.1.2
IDE: Intelij IDEA
开始
新建五个类
Temp类
自定义序列化类型原理,不懂得可以看这边文章,这是我关于序列化的一些心得
package com.hadoop.Combiner;
import org.apache.hadoop.io.Writable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
public class Temp implements Writable {
private double maxtemp;
private double mintemp;
public Temp() {
}
@Override
public String toString() {
return "MaxTemperature:"+maxtemp+'\t'+"Mintemperature:"+mintemp;
}
public double getMaxtemp() {
return maxtemp;
}
public void setMaxtemp(double maxtemp) {
this.maxtemp = maxtemp;
}
public double getMintemp() {
return mintemp;
}
public void setMintemp(double mintemp) {
this.mintemp = mintemp;
}
/**
*
* @param dataOutput
* @throws IOException
*/
@Override
public void write(DataOutput dataOutput) throws IOException {
dataOutput.writeDouble(maxtemp);
dataOutput.writeDouble(mintemp);
}
/**
*
* @param dataInput
* @throws IOException
*/
@Override
public void readFields(DataInput dataInput) throws IOException {
maxtemp=dataInput.readDouble();
mintemp=dataInput.readDouble();
}
}
Combiner类
只有最高温度和最低温度才对我们有用,所以我们在Combiner阶段提取出每个map的最高温度和最低温度
package com.hadoop.Combiner;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class TempCombiner extends Reducer<Text, DoubleWritable,Text,DoubleWritable> {
@Override
protected void reduce(Text key, Iterable<DoubleWritable> values, Context context) throws IOException, InterruptedException {
double maxtemp=0;
double mintemp=999;
for (DoubleWritable value : values) {
if(value.get()>maxtemp){
maxtemp=value.get();
}
if (value.get()<mintemp){
mintemp=value.get();
}
}
context.write(key,new DoubleWritable(maxtemp));
context.write(key,new DoubleWritable(mintemp));
}
}
注意: 这里虽然和reducer的过程一样,但是由于输出类型必须和mapper一直所以需要单独写!
Mapper类
package com.hadoop.Combiner;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
public class TempMapper extends Mapper<LongWritable, Text,Text, DoubleWritable> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line=value.toString();
// 由于源数据的分割方式是不同个数的空格,用正则表达式可以直接切
String[] data=line.split("\\s+");
// 获取年份信息
String year=data[2].substring(0,4);
// 获取温度信息
Double tempdata= Double.parseDouble(data[3]);
context.write(new Text(year),new DoubleWritable(tempdata));
}
}
Reducer类
package com.hadoop.Combiner;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class TempReducer extends Reducer<Text, DoubleWritable,Text,Text> {
@Override
protected void reduce(Text key, Iterable<DoubleWritable> values, Context context) throws IOException, InterruptedException {
Temp temp = new Temp();
double maxtemp=0;
double mintemp=999;
for (DoubleWritable value : values) {
if(value.get()>maxtemp){
maxtemp=value.get();
}
if (value.get()<mintemp){
mintemp=value.get();
}
}
temp.setMaxtemp(maxtemp);
temp.setMintemp(mintemp);
// 我将Temp的toString内容作为结果输出
context.write(key,new Text(temp.toString()));
}
}
Driver类
package com.hadoop.Combiner;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
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;
import java.io.IOException;
public class TempDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Job job = Job.getInstance(new Configuration());
job.setJarByClass(TempDriver.class);
job.setMapperClass(TempMapper.class);
job.setReducerClass(TempReducer.class);
job.setCombinerClass(TempCombiner.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(DoubleWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.setInputPaths(job, new Path("E:\\athadoop\\Temperature\\input"));
FileOutputFormat.setOutputPath(job, new Path("E:\\athadoop\\Temperature\\output"));
boolean b = job.waitForCompletion(true);
System.exit(b ? 0 : 1);
}
}
输出结果
后记
至此为本人对Hadoop Combiner合并 全部理解内容,欢迎大家在留言处给予建议或指出不足,感谢!
内容原创 侵删致歉
欢迎访问作者主页查看更多相关文章,大家一起学习一起进步!