导读:
明白LocatedBlocks作用即可
此问题来自about云(371358502):
可实现:
DFSClient dfsClient = new DFSClient(new URI(defaultFS),this.fileSystem.getConf());
LocatedBlocks locatedBlocks = dfsClient.getLocatedBlocks(filePath, 0);
LocatedBlocks的作用:
getBlockLocations
函数原型:public LocatedBlocks getBlockLocations(String src,long offset, long length)
函数作用:用于确定文件内容的位置,它的输入参数为:文件名、偏移量、长度、返回值是一个LocatedBlocks 对象的列表:
getBlockLocations:确定数据的位置
下面可参考,来说明LocatedBlocks的作用:
- 从NameNode中获得将要被打开文件的元数据信息
- 1, 调用 callGetBlockLocations() 方法 取得用户请求的file的元数据信息,数据保存在 locatedBlocks 中
- //非常关键的一步
- LocatedBlocks newInfo = callGetBlockLocations(namenode, src, 0, prefetchSize);
-
- 2,若无法定位某个文件的数据Block,则表示文件不存在,则抛出对应异常
- if (newInfo == null) {
- throw new IOException("Cannot open filename " + src);
-
- }
- 3, 使用定位到locatedBlocks来更新当前的locatedBlocks
- if (locatedBlocks != null) {
- Iterator<LocatedBlock> oldIter = locatedBlocks.getLocatedBlocks().iterator();
-
- Iterator<LocatedBlock> newIter = newInfo.getLocatedBlocks().iterator();
-
- while (oldIter.hasNext() && newIter.hasNext()) {
-
- if (! oldIter.next().getBlock().equals(newIter.next().getBlock())) {
-
- throw new IOException("Blocklist for " + src + " has changed!");
-
- }
- }
- }
- this.locatedBlocks = newInfo;
复制代码
|