mapreduce高级特性及shuffle
第一节:shuffle机制
1.1 概述
mapreduce中,map阶段处理的数据如何传递给reduce阶段,是mapreduce框架中最关键的一个流程,这个流程就叫shuffle;shuffle:洗牌、发牌——(核心机制:数据分区,排序,缓存);具体来说:就是将maptask输出的处理结果数据,分发给reducetask,并在分发的过程中,对数据按key进行了分区和排序;
1.2 主要流程
shuffle是MR处理流程中的一个过程,它的每一个处理步骤是分散在各个map task和reduce task节点上完成的,整体来看,分为3个操作:
-
分区partition 属于map Task阶段
-
Sort根据key排序 属于reduce Task阶段
-
Combiner进行局部value的合并
1.3 流程细分
-
由wordcount案例执行开始分步演示mapreduce运行逻辑(较粗,但对初学者理解mr有帮助)
-
mr结合yarn运行逻辑
-
mr客户端程序业务流程
-
MrAppmaster控制map与reduce运行的工作流程
5.shuffle流程
第二节:结合案例讲解mr重要知识点
1.1 获取文件名
在map运行时获取被处理数据所在文件的文件名
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
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.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
/**
*
* @author lyd
*
* 数据:
* 如chinese.txt 其内容如下
* 小明 78
* 小红 80
* 小白 79
*
* math.txt:
* 小明 68
* 小红 70
* 小白 69
*
* enlish.txt:
* 小明 88
* 小红 90
* 小白 89
*
* 输出:
* chinese 79
* math 69
* english 89
关键代码
MyMapper类中的
InputSplit is = context.getInputSplit();
String fileName = ((FileSplit)is).getPath().getName();
*/
public class AvgDemo {
//自定义myMapper
public static class MyMapper extends Mapper<LongWritable, Text, Text, Text>{
//只在map方法运行之前执行一次。(仅执行一次)
@Override
protected void setup(Context context)
throws IOException, InterruptedException {
}
@Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException {
String line = value.toString();
String lines [] = line.split(" ");
//获取文件名字来作为key
InputSplit is = context.getInputSplit();
String fileName = ((FileSplit)is).getPath().getName();
context.write(new Text(fileName), new Text(lines[1]));
}
//map方法运行完后执行一次(仅执行一次)
@Override
protected void cleanup(Context context)
throws IOException, InterruptedException {
}
}
//自定义myReducer
public static class MyReducer extends Reducer<Text, Text, Text, Text>{
//在reduce方法执行之前执行一次。(仅一次)
@Override
protected void setup(Context context)
throws IOException, InterruptedException {
}
@Override
protected void reduce(Text key, Iterable<Text> value,Context context)
throws IOException, InterruptedException {
double counter = 0;
int sum = 0;
for (Text t : value) {
counter += Double.parseDouble(t.toString());
sum ++;
}
context.write(key, new Text((counter/sum)+""));
}
//在reduce方法执行之后执行一次。(仅一次)
@Override
protected void cleanup(Context context)
throws IOException, InterruptedException {
}
}
/**
* job的驱动方法
* @param args
*/
public static void main(String[] args) {
try {
//1、获取Conf
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://hadoop01:9000");
//2、创建job
Job job = Job.getInstance(conf, "model01");
//3、设置运行job的class
job.setJarByClass(AvgDemo.class);
//4、设置map相关属性
job.setMapperClass(MyMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
//5、设置reduce相关属性
job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
//判断输出目录是否存在,若存在则删除
FileSystem fs = FileSystem.get(conf);
if(fs.exists(new Path(args[1]))){
fs.delete(new Path(args[1]), true);
}
FileOutputFormat.setOutputPath(job, new Path(args[1]));
//6、提交运行job
int isok = job.waitForCompletion(true) ? 0 : 1;
//退出
System.exit(isok);
} catch (IOException | ClassNotFoundException | InterruptedException e) {
e.printStackTrace();
}
}
}
1.2 多文件输出
将mr处理后的结果数据输出到多个文件中
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
/**
*
* @author lyd
*
*输入数据
*hello world
*hi qianfeng
*Hi qianfeng
*Hello Qianfeng
*QQ
*163.com
*1603
*@qq.com
**123
**123
*(123)
*
*单词首字母为a-z的单词放到一个输出文件,并统计
*单词首字母为A-Z的单词放到一个输出文件,并统计
*单词首字母为0-9的单词放到一个输出文件,并统计
*单词首字母为其它的单词放到一个输出文件,并统计
*
关键代码:
MyReducer类中:
mos = new MultipleOutputs<Text, Text>(context);
mos.write("az", key, new Text(counter+""));
main函数中:
MultipleOutputs.addNamedOutput(job, "az", TextOutputFormat.class, Text.class, Text.class);
*/
public class MultipleDemo {
//自定义myMapper
public static class MyMapper extends Mapper<LongWritable, Text, Text, Text>{
//只在map方法运行之前执行一次。(仅执行一次)
@Override
protected void setup(Context context)
throws IOException, InterruptedException {
}
@Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens()) {
context.write(new Text(st.nextToken()), new Text("1"));
}
}
//map方法运行完后执行一次(仅执行一次)
@Override
protected void cleanup(Context context)
throws IOException, Interr