将会按照HDFS ,MapReduce, Yarn的顺序更新, 近期还会整理Zookeper, Hive, pig等相关
如果对您有帮助或者解决了您的问题, 就帮我点个赞或者评论关注支持吧, 您的鼓励是我写博客的最大支持, 感谢!
目前是大四研一实习, 单位比较人性化周末双休所以就断更两天了. 今天继续更新
0. MapReduce是什么?
MapReduce是一种编程模型,用于大规模数据集(大于1TB)的并行运算。MapReduce将分成两个部分"Map(映射)"和"Reduce(归约)"。
思想是运算跟着数据走, 数据在哪我就在哪运算. 因为移动数据成本太大
1. MapReduce工作过程拆解
Demo
下面通过一个景点案例(单词统计)看MapReduce是如何工作的。
有一个文本文件,被分成了4份,分别放到了4台服务器中存储
Text1:the weather is good
Text2:today is good
Text3:good weather is good
Text4:today has good weather
现在要统计出每个单词的出现次数。
1.1处理过程
1.1.1 Map拆分单词
- map节点1
- 输入:“the weather is good”
- 输出:(the,1),(weather,1),(is,1),(good,1)
- map节点2
- 输入:“today is good”
- 输出:(today,1),(is,1),(good,1)
map3,4,5.......
1.1.2 Map合并统计单词
- map节点1
- map节点2
map3,4,5.......
1.1.3 Reduce汇总统计
2. MapReduce 编程思路
map - 对于传入的单行数据处理
reduce - 对于传进来的map处理
3. MapReduce JAR包讲解
3.1 Map程序
import java.io.IOException;
import org.apache.hadoop.io.Intwritable;
import org.apache.hadoop.io.Longwritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class Wordcountmapper extends Mapper<LongWritabIe, Text,
Text, Intwritabte> {
@OverrIde
protected void map(LongWritabIe key, Text value, Context context )
throws IOException, InterruptedException {
//得到每一行数据
String line = value. tostring();
String[] words = line.split(" ");
for (String word : words) {
context.write(new Text(word), new Intwritable(l));
}
}
}
其中的4个类型分别是:输入key类型、输入value类型、输出key类型、输出value类型
Mapper<LongWritable, Text, Text, IntWritable>
输入key默认是mr所读到一行文本的起始偏移量(Long类型)
输入value默认是mr所读到的一行的数据内容(String类型)
输出key 和value则是用户自定义的
因为MapReduce程序的输出数据需要在不同机器间传输,所以必须是可序列化的,例如Long类型,Hadoop中定义了自己的可序列化类型LongWritable,String对应的是Text,int对应的是IntWritable。
3.2 Reduce程序
import java.io.IOException;
import org.apache.hadoop.io.Intwritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class WordCountReducer extends Reducer<Text, Intwritable,
Text, Intwritable> {
@Overr1de
protected void reduce(Text key, Interable<Intwritable>values,
Context context) throws IOException, InterruptedException {
Integer count = 0;
for (Intwritable value : values) {
count +=value.get();
context.write( key, new Intwritable(count)) ;
}
}
}
和之前一样分别指:输入key的类型、输入value的类型、输出key的类型、输出value的类型。
Reducer<Text, IntWritable, Text, IntWritable>
reduce方法接收的是:一个字符串类型的key、一个可迭代的数据集
比如
reduce任务读取到map任务处理结果是这样的:
(good,1)(good,1)(good,1)(good,1)
当传给reduce方法时,就变为:
key:good
value:(1,1,1,1)
四 MapReduce的执行过程
1. 客户端提交任务
查看文件规模, 形成任务规划分配
2. 启动 appmaster
appmaster负责maptask和reducetask的启动、监控、协调管理工作。
yarn找一个合适的服务器来启动appmaster,并把job.split、jar、xml交给它
3. 启动maptask
根据固化文件job.split中的分片信息启动maptask,一个分片对应一个maptask。
4. 执行maptask
读一行就调一次map方法.结果保存到本机的一个结果文件
5. 启动ReduceTask
maptask的结果中有几个分区就启动几个reducetask
6. 执行reduceTask
reducetask把读到的数据按key组织好,传给reduce方法进行处理,处理结果写到指定的输出路径。