前面《Hadoop系列(九)HDFS的数据文件是如何被写入DataNode?》我们介绍了文件数据写入到DataNode的过程,这篇文章我们介绍下从DataNode上读取文件数据的过程,如图:
整个读取文件过程如下:
1. Client向NameNode发起RPC请求,来确定请求⽂件block所在的位置; 2. NameNode会视情况返回⽂件的部分或者全部block列表,对于每个block,NameNode 都会返回含有该 block 副本的 DataNode 地址; 这些返回的 DN 地址,会按照集群拓扑结构得出DataNode 与客户端的距离,然后进⾏排序, 排序两个规则:⽹络拓扑结构中距离 Client 近的排靠前;⼼跳机制中超时汇报的 DN 状态为 STALE,这样的排靠后; 3. Client 选取排序靠前的 DataNode 来读取 block,如果客户端本身就是DataNode,那么将从本地直接获取数据(短路读取特性); 4. 底层上本质是建⽴ Socket Stream(FSDataInputStream),重复的调⽤⽗类DataInputStream 的 read ⽅法,直到这个块上的数据读取完毕; 5. 当读完列表的 block 后,若⽂件读取还没有结束,客户端会继续向NameNode 获取下⼀批的block 列表; 6. 读取完⼀个 block 都会进⾏ checksum 验证,如果读取 DataNode 时出现错误,客户端会通知 NameNode,然后再从下⼀个拥有该 block 副本的DataNode 继续读。 7. read ⽅法是并⾏的读取 block 信息,不是⼀块⼀块的读取;NameNode 只是返回Client请求包含块的DataNode地址,并不是返回请求块的数据; 8. 最终读取来所有的 block 会合并成⼀个完整的最终⽂件
备注:
1、hdfs写入数据是串行写入的,也就是先写A,再写B,再写C。
2、hdfs读取数据是并行读取的。
还没有评论,来说两句吧...