Hadoop2.7.3客户端之Java API查看当前文件详情
- 在Idea或eclipse中创建maven项目
- 在pom.xml中添加hadoop依赖(参考前面博文)
- 编写测试代码,代码如下:
public static void listFiles() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS","hdfs://nn-master:9000");
FileSystem fileSystem = FileSystem.get(configuration);
RemoteIterator<LocatedFileStatus> iterator =
fileSystem.listFiles(new Path("/input/"), true);
while (iterator.hasNext()){
LocatedFileStatus locatedFileStatus = iterator.next();
System.out.println("blockSize:" + locatedFileStatus.getBlockSize());
System.out.println("owner:" + locatedFileStatus.getOwner());
System.out.println("group:" + locatedFileStatus.getGroup());
System.out.println("更新时间:" + locatedFileStatus.getModificationTime());
System.out.println("副本数:" + locatedFileStatus.getReplication());
BlockLocation[] blockLocations = locatedFileStatus.getBlockLocations();
for (BlockLocation block: blockLocations) {
System.out.println("len : " + block.getLength());
String[] blockNames = block.getNames();
for (String name: blockNames) {
System.out.println("block name : " + name);
}
}
}
fileSystem.close();
}