总访问量: PV

DataScience && DataMining && BigData

HDFS的Java客户端操作代码(查看HDFS下所有的文件存储位置信息)

1.查看HDFS下所有的文件存储位置信息

 1 package Hdfs;
 2 
 3 import java.net.URI;
 4 import org.apache.hadoop.conf.Configuration;
 5 import org.apache.hadoop.fs.BlockLocation;
 6 import org.apache.hadoop.fs.FileStatus;
 7 import org.apache.hadoop.fs.FileSystem;
 8 import org.apache.hadoop.fs.Path;
 9 
10 public class LocationFile {
11     public static void main(String[] args) throws Exception {
12         String uri = "hdfs://neusoft-master:9000/user/root/test/demo1";
13         Configuration conf = new Configuration();
14         try {
15             FileSystem fs = FileSystem.get(URI.create(uri), conf);
16             Path fpath = new Path(uri);
17             FileStatus fileStatus = fs.getFileStatus(fpath);
18             BlockLocation[] blockLocations = fs.getFileBlockLocations(
19                     fileStatus, 0, fileStatus.getLen());
20             int blocklen = blockLocations.length;
21             for (int i = 0; i < blocklen; i++) {
22                 String[] hosts = blockLocations[i].getHosts();
23                 System.out.println("block_" +i+ "_location:" + hosts[0]);
24             }
25         } catch (Exception e) {
26             e.printStackTrace();
27         }
28     }
29 }
HDFS下所有文件存储位置

2.提交jar包,并分析运行结果

3.这里的显示结果:

     block_0_location:neusoft-master

  分别为:块号:主机名,因为HDFS的文件是由Data块完成的,Hadoop中得block块一般64M

Hadoop2中block块为128M。(单独分析block块)

posted @ 2017-01-09 23:41  CJZhaoSimons  阅读(733)  评论(0编辑  收藏  举报