package cn.jecn.hadoop.hdfs.com; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.BlockLocation; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hdfs.DistributedFileSystem; import org.apache.hadoop.hdfs.protocol.DatanodeInfo; public class FindFileOnHDFS { public static void main(String[] args) throws Exception{ getHDFSNodes(); getFileLocal(); } //查找某个文件在HDFS集群的位置 public static void getFileLocal() throws Exception { Configuration conf = new Configuration(); FileSystem hdfs = FileSystem.get(conf); Path fpath = new Path("/user/root/20120722/word.txt"); //获取文件系统里面的文件信息 FileStatus fileStatus = hdfs.getFileStatus(fpath); //获取文件的块信息 BlockLocation[] blkLocations = hdfs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen()); int blockLen = blkLocations.length; //循环打印 for(int i = 0 ; i < blockLen ; ++i ){ String[] hosts = blkLocations[i].getHosts(); System.out.println("block_"+i + "_location:" + hosts[i]); } } public static void getHDFSNodes() throws Exception{ //HDFS集群点数 Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); //获取分布式文件系统 DistributedFileSystem hdfs = (DistributedFileSystem)fs; //获取所有的节点数 DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats(); //循环打印 for( int i = 0 ; i < dataNodeStats.length ; ++i ){ System.out.println("DataNode_" + i + "_Node:" + dataNodeStats[i].getHostName()); } } }


pangzhihui LV14
2024年6月12日
ningningnn LV3
2024年4月9日
ewan007 LV30
2023年11月13日
sunlzh888888 LV29
2023年9月15日
飞行家真真 LV1
2023年7月2日
2716804680 LV9
2023年3月23日
1690356080 LV38
2023年2月13日
Trickster LV9
2022年8月1日
tianqi2020 LV5
2022年5月27日
cccccc12 LV8
2022年5月19日