add hadoop mapreduce

This commit is contained in:
罗祥 2019-03-14 14:05:25 +08:00
parent a580dfdefa
commit cf17652c35
17 changed files with 289 additions and 119 deletions

View File

@ -6,7 +6,7 @@
> 2019大数据常用技术栈学习之路 — — 持续更新中
> 大数据常用技术栈学习之路 — — 持续更新中
@ -40,8 +40,12 @@
</table>
## 一、Hadoop
1. [分布式文件存储系统——HDFS](https://github.com/heibaiying/BigData-Notes/blob/master/notes/Hadoop-HDFS.md)
2. [分布式计算框架——MapReduce](https://github.com/heibaiying/BigData-Notes/blob/master/notes/Hadoop-MapReduce.md)
3. [集群资源管理器——YARN](https://github.com/heibaiying/BigData-Notes/blob/master/notes/Hadoop-YARN.md)
## 二、Hive
## 三、Spark
## 四、Flume

View File

@ -1,5 +1,6 @@
# Hadoop-HDFS
<nav>
# 分布式文件存储系统——HDFS
<nav>
<a href="#一介绍">一、介绍</a><br/>
<a href="#二HDFS-设计原理">二、HDFS 设计原理</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#21-HDFS-架构">2.1 HDFS 架构</a><br/>
@ -20,9 +21,14 @@
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#34-跨平台移植性">3.4 跨平台移植性</a><br/>
<a href="#四HDFS-shell">四、HDFS shell</a><br/>
<a href="#五HDFS-API">五、HDFS API</a><br/>
<a href="#附图解HDFS存储原理">图解HDFS存储原理</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#1-HDFS写数据原理">1. HDFS写数据原理</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#2-HDFS读数据原理">2. HDFS读数据原理</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#3-HDFS故障类型和其检测方法">3. HDFS故障类型和其检测方法</a><br/>
</nav>
## 一、介绍
**HDFS** **Hadoop Distributed File System**)是一种分布式文件系统,具有**高容错**、**高吞吐量**等特性,可以部署在**低成本**的硬件上。
@ -132,8 +138,55 @@ HDFS的设计便于从一个平台移植到另一个平台。这有助于HDFS作
## 附图解HDFS存储原理
说明:本小结图片引用自博客[翻译经典 HDFS 原理讲解漫画](https://blog.csdn.net/hudiefenmu/article/details/37655491)
### 1. HDFS写数据原理
<div align="center"> <img src="https://github.com/heibaiying/BigData-Notes/blob/master/pictures/hdfs-write-1.jpg"/> </div>
<div align="center"> <img src="https://github.com/heibaiying/BigData-Notes/blob/master/pictures/hdfs-write-2.jpg"/> </div>
<div align="center"> <img src="https://github.com/heibaiying/BigData-Notes/blob/master/pictures/hdfs-write-3.jpg"/> </div>
### 2. HDFS读数据原理
<div align="center"> <img src="https://github.com/heibaiying/BigData-Notes/blob/master/pictures/hdfs-read-1.jpg"/> </div>
### 3. HDFS故障类型和其检测方法
<div align="center"> <img src="https://github.com/heibaiying/BigData-Notes/blob/master/pictures/hdfs-tolerance-1.jpg"/> </div>
<div align="center"> <img src="https://github.com/heibaiying/BigData-Notes/blob/master/pictures/hdfs-tolerance-2.jpg"/> </div>
**第二部分:读写故障的处理**
<div align="center"> <img src="https://github.com/heibaiying/BigData-Notes/blob/master/pictures/hdfs-tolerance-3.jpg"/> </div>
**第三部分DataNode故障处理**
<div align="center"> <img src="https://github.com/heibaiying/BigData-Notes/blob/master/pictures/hdfs-tolerance-4.jpg"/> </div>
**副本布局策略**
<div align="center"> <img src="https://github.com/heibaiying/BigData-Notes/blob/master/pictures/hdfs-tolerance-5.jpg"/> </div>
## 参考资料
1. [Apache Hadoop 2.9.2 > HDFS Architecture](http://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-hdfs/HdfsDesign.html)
2. Tom White. hadoop权威指南 [M]. 清华大学出版社, 2017.
3. [翻译经典 HDFS 原理讲解漫画](https://blog.csdn.net/hudiefenmu/article/details/37655491)

113
notes/Hadoop-MapReduce.md Normal file
View File

@ -0,0 +1,113 @@
# 分布式计算框架——MapReduce
<nav>
<a href="#一MapReduce-概述">一、MapReduce 概述</a><br/>
<a href="#二MapReduce-编程模型简述">二、MapReduce 编程模型简述</a><br/>
<a href="#三MapReduce-编程模型详述">三、MapReduce 编程模型详述</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#31-InputFormat-&-RecordReaders">3.1 InputFormat & RecordReaders </a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#32-combiner">3.2 combiner</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#33-partitioner">3.3 partitioner</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#34-sort-&-combiner">3.4 sort & combiner</a><br/>
<a href="#四MapReduce-词频统计案例">四、MapReduce 词频统计案例</a><br/>
</nav>
## 一、MapReduce 概述
Hadoop MapReduce是一个分布式计算框架用于编写应用程序以可靠容错的方式在大型集群上并行处理大量数据多为TB级别数据集
MapReduce 作业通常将输入数据集拆分为独立的块,这些块由**map任务**以完全并行的方式处理。框架对**map任务**的输出进行排序,然后输入到**reduce任务**。通常,作业的输入和输出都存储在文件系统中。该框架负责调度任务,监视任务并重新执行失败的任务。
MapReduce框架专门用于`<keyvalue>`对,也就是说,框架将作业的输入视为一组`<keyvalue>`对,并生成一组`<keyvalue>`对作为输出。输出和输出的`key``value`都必须实现[Writable](http://hadoop.apache.org/docs/stable/api/org/apache/hadoop/io/Writable.html) 接口。
```
(input) <k1, v1> -> map -> <k2, v2> -> combine -> <k2, v2> -> reduce -> <k3, v3> (output)
```
## 二、MapReduce 编程模型简述
这里以词频统计为例说明MapReduce的编程模型下图为词频统计的流程图
<div align="center"> <img width="600px" src="https://github.com/heibaiying/BigData-Notes/blob/master/pictures/mapreduceProcess.png"/> </div>
1. **input** : 读取文本文件;
2. **splitting** : 将文件按照行进行拆分此时得到的K1为行数V1表示对应行的文本内容
3. **mapping** : 并行将每一行按照空格进行拆分拆分得到的List(K2,V2),其中K2代表每一个单词由于是做词频统计所以其V2为1代表出现1次
4. **shuffling**由于Mapping操作可能是在不同的机器上并行处理的所以需要通过shuffling将相同的数据分到同一个节点上去合并这样才能统计出最终的结果此时得到K2为每一个单词List(V2)为可迭代集合V2就是Mapping中的V2
5. **Reducing** : 这里的案例是统计单词出现的总次数所以Reducing迭代List(V2),并计算其和值,最终输出。
MapReduce 编程模型中`splitting`` shuffing`操作都是由框架实现的,实际上,主要需要我们实现的是`mapping``reducing`
中的编程逻辑这也就是为何该框架叫做MapReduce的原因。
## 三、MapReduce 编程模型详述
<div align="center"> <img width="600px" src="https://github.com/heibaiying/BigData-Notes/blob/master/pictures/Detailed-Hadoop-MapReduce-Data-Flow-14.png"/> </div>
### 3.1 InputFormat & RecordReaders
、通过InputFormat 将输出文件拆分为多个InputSplit并由RecordReaders 将InputSplit 转换为标准的<key,value>键值对作为map的输出。这一步的意义在于只有先进行逻辑拆分并转为标准的格式后才能为多个map提供输入进行并行处理
`InputFormat` 为一个抽象类,其中定义了两个抽象方法,而实际的操作则由其实现类来进行。其源码如下:
**getSplits**将输入文件拆分为多个InputSplit
**createRecordReader**: 定义RecordReader的创建方法
```java
public abstract class InputFormat<K, V> {
public abstract
List<InputSplit> getSplits(JobContext context) throws IOException, InterruptedException;
public abstract
RecordReader<K,V> createRecordReader(InputSplit split,
TaskAttemptContext context
) throws IOException,
InterruptedException;
}
```
### 3.2 combiner
combiner是map运算后的可选操作其实际上是一个本地化的reduce操作它主要是在map计算出中间文件后做一个简单的合并重复key值的操作。例如我们对文件里的单词频率做统计map计算时候如果碰到一个hadoop的单词就会记录为1但是这篇文章里hadoop可能会出现n多次那么map输出文件冗余就会很多因此在reduce计算前对相同的key做一个合并操作那么文件会变小。这样就提高了宽带的传输效率因为hadoop计算的宽带资源往往是计算的瓶颈也是最为宝贵的资源。
但并非所有场景都适合使用combiner使用它的原则是combiner的输入不会影响到reduce计算的最终输入例如如果计算只是求总数最大值最小值可以使用combiner但是做平均值计算使用combiner的话最终的reduce计算结果就会出错。
<div align="center"> <img width="600px" src="https://github.com/heibaiying/BigData-Notes/blob/master/pictures/mapreduce-combiner.png"/> </div>
### 3.3 partitioner
partitioner可以理解成分类器按照key的不同分别将map的输出分给不同的reduce可以自定义实现。
### 3.4 sort & combiner
<div align="center"> <img width="600px" src="https://github.com/heibaiying/BigData-Notes/blob/master/pictures/mapreduce-sort.png"/> </div>
经过partitioner处理后每个key-value对都得到分配到的reduecer信息然后把记录先写入内存(In-memory buffer)。当写入内存的数据越来越多时当buffer达到一定阀值默认80M就开始执行spill溢写步骤即分成小文件写入磁盘。在写之前先对memory中每个partition进行排序in-memory sort。如果数据量大的话这个步骤会产生很多个spilled文件如果我们定义了combine那么在排序之前还会进行combine最后一个步骤就是merge把 spill 步骤产生的所有spilled filesmerge成一个大的已排序文件。merge是相同的partition之间进行。
Merge是怎样的如“aaa”从某个map task读取过来时值是5从另外一个map 读取值是8因为它们有相同的key所以得merge成group。什么是group。对于“aaa”就是像这样的{“aaa”, [5, 8, 10, …]}数组中的值就是从不同溢写文件中读取出来的然后再把这些值加起来。请注意因为merge是将多个溢写文件合并到一个文件所以可能也有相同的key存在在这个过程中如果client设置过Combiner也会使用Combiner来合并相同的key。
## 四、MapReduce 词频统计案例
## 参考资料
1. [分布式计算框架MapReduce](https://zhuanlan.zhihu.com/p/28682581)
2. [Apache Hadoop 2.9.2 > MapReduce Tutorial](http://hadoop.apache.org/docs/stable/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html)

View File

@ -1,4 +1,4 @@
# Hadoop-YARN
# 集群资源管理器——YARN
<nav>
<a href="#一hadoop-yarn-简介">一、hadoop yarn 简介</a><br/>

Binary file not shown.

After

Width:  |  Height:  |  Size: 226 KiB

BIN
pictures/hdfs-read-1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 291 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 310 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 327 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 346 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 340 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 314 KiB

BIN
pictures/hdfs-write-1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 307 KiB

BIN
pictures/hdfs-write-2.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 297 KiB

BIN
pictures/hdfs-write-3.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 308 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
pictures/mapreduce-sort.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 150 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 269 KiB