Hadoop 3.x(HDFS)----【HDFS 的读写流程】
- 1. HDFS写数据流程
- 1. 剖析文件写入
- 2. 网络拓扑-结点距离计算
- 3. 机架感知(副本存储节点选择)
- 1. 机架感知说明
- 2. Hadoop3.1.3副本节点选择
- 2. HDFS读数据流程
1. HDFS写数据流程
1. 剖析文件写入
![在这里插入图片描述](https://img-blog.csdnimg.cn/84614b2e56f14a07b4559b806faaaee7.png)
- 客户端通过 DistributedFileSystem 模块向 NameNode 请求上传文件,NameNode 检查目标文件是否已经存在,父目录是否存在。
- NameNode 返回是否可以上传。
- 客户端请求第一个 Block 上传到哪几个 DataNode 服务器上。
- NameNode 返回 3 个 DataNode 节点,分别是 dn1、dn2、dn3。
- 客户端通过 FSD阿塔OutputStream 模块请求 dn1 上传数据,dn1 收到请求会继续调用 dn2,然后 dn2 调用 dn3,将这个通信管道建立完成。
- dn1、dn2、dn3 逐级应答客户端。
- 客户端开始往 dn1 上传第一个 Block(先从磁盘读取数据放到一个本地内存缓存),以 Packet 为单位,dn1 收到一个 Packet 就会传给 dn2,dn2 传给 dn3;dn1 每传一个 packet 会放入一个应答队列等待应答。
- 当一个 Block 传输完成之后,客户端再次请求 NameNode 上传第二个 Block 的服务器。(重复执行 3-7 步)。
2. 网络拓扑-结点距离计算
在 HDFS 写数据的过程中,NameNode 会选择距离待上传数据最近距离的 DataNode 接收数据。那么这个最近距离怎么计算呢?
节点距离:两个节点到达最近的共同祖先的距离总和。
![在这里插入图片描述](https://img-blog.csdnimg.cn/134c993722a241408e064d1d6c0a5f15.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/674dd0fd10c64de784228cec9053266e.png)
3. 机架感知(副本存储节点选择)
1. 机架感知说明
https://hadoop.apache.org/docs/r3.1.3/hadoop-project-dist/hadoop-hdfs/HdfsDesign.html#Data_Replication
For the common case, when the replication factor is three, HDFS’s placement policy is to put one replica on the local machine if the writer is on a datanode, otherwise on a random datanode, another replica on a node in a different (remote) rack, and the last on a different node in the same remote rack. This policy cuts the inter-rack write traffic which generally improves write performance. The chance of rack failure is far less than that of node failure; this policy does not impact data reliability and availability guarantees. However, it does reduce the aggregate network bandwidth used when reading data since a block is placed in only two unique racks rather than three. With this policy, the replicas of a file do not evenly distribute across the racks. One third of replicas are on one node, two thirds of replicas are on one rack, and the other third are evenly distributed across the remaining racks. This policy improves write performance without compromising data reliability or read performance.
2. Hadoop3.1.3副本节点选择
![在这里插入图片描述](https://img-blog.csdnimg.cn/5d848eb8ee9044eba02cc9603fdec6bb.png)
第一个副本在 Clinet 所处的节点上。如果客户端在集群外,随机选择一个。
第二个副本在另一个机架的随机一个节点。
第三个副本在第二个副本所在机架的随机节点。
2. HDFS读数据流程
![在这里插入图片描述](https://img-blog.csdnimg.cn/86938c56b12a444aa79824e4e18408e4.png)
- 客户端通过 DistributedFileSystem 向 NameNode 请求下载文件,NameNode 通过查询元数据,找到文件块所在的 DataNode 地址。
- 挑选一台 DataNode(就近原则,然后随机)服务器,请求读取数据。
- DataNode 开始传输数据给客户单(从磁盘里面读取数据输入流,以 Packet 为单位来做校验)。
- 客户端以 Packet 为单位接收,先在本地缓存,然后写入目标文件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)