大数据人|大数据第一社区

 找回密码
 注册会员

扫一扫,访问微社区

解析hadoop的文件读取过程

2016-1-2 21:35| 发布者: admin| 查看: 21895| 评论: 0|来自: 新浪博客

摘要: 搜索复制Step1Client生成一个HDFS类库中的DistributedFileSystem对象实例,并使用此实例的open接口打开一个文件Step2DistributedFileSystem 通过RPC向namenode发出请求,以获得文件相关的数据块位置信息。NameNode将 ...

Step1
Client生成一个HDFS类库中的DistributedFileSystem对象实例,并使用此实例的open接口打开一个文件
Step2
DistributedFileSystem 通过RPC向namenode发出请求,以获得文件相关的数据块位置信息。NameNode将包含此文件相关数据块所在的DataNode地址,经过与 Client相关距离进行排序后,返还给DistributedFileSystem。

Step3
DistributedFileSystem在获得数 据块相关信息后,生成一个FSDataInputStream对象实例返回给Client。此实例封装了一个DFSInputStream对象,负责存储 数据块信息以及DataNode地址信息,并负责后续的文件内容读取处理。

Step4
Client向FSDataInputStream发出读取数据的read调用。

Step5
在收到read调用后,FSDataInputStream封装的DFSInputStream选择第一个数据快的最近的DataNode,并读取相应的 数据信息,返回给CLient。在数据块读取完成后,DFSInputStream负责关闭到相应的DataNode的链接。

Step6
DFSInputStream将继续选择后续数据块最近的DataNode节点,并读取数据返回给Client,直到最后一个数据块读取完成。

Step7
当Client读取完所有的数据后,将调用FSInputStream的Close接口完成本次的文件读取操作。

public String catFile(String remoteFile) throws IOException {
Path path = new Path(remoteFile);
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
FSDataInputStream fsdis = null;
StringBuffer strBuffer=new StringBuffer();
try {
fsdis = fs.open(path);
byte buffer[]=new byte[1024];
int bytesRead=0;
while((bytesRead=fsdis.read(buffer))>0){
for(int i=0;i小于buffer.length;i++){
strBuffer.append(buffer[i]);
}
}
} finally {
IOUtils.closeStream(fsdis);
fs.close();
}
return strBuffer.toString();
}


鲜花

握手

雷人

路过

鸡蛋

最新评论

关闭

站长推荐上一条 /2 下一条


id="mn_portal" >首页Portalid="mn_P18" onmouseover="navShow('P18')">应用id="mn_P15" onmouseover="navShow('P15')">技术id="mn_P37" onmouseover="showMenu({'ctrlid':this.id,'ctrlclass':'hover','duration':2})">前沿id="mn_P36" onmouseover="navShow('P36')">宝箱id="mn_P61" onmouseover="showMenu({'ctrlid':this.id,'ctrlclass':'hover','duration':2})">专栏id="mn_P65" >企业id="mn_Nd633" >导航 折叠导航 关注微信 关注微博 关注我们

QQ|广告服务|关于我们|Archiver|手机版|小黑屋|大数据人 ( 鄂ICP备14012176号-2  

GMT+8, 2024-10-23 11:40 , Processed in 0.182963 second(s), 21 queries .

Powered by 小雄! X3.2

© 2014-2020 bigdataer Inc.

返回顶部