首页>代码>hadoop入门教程及其实例代码>/hadoopTest/src/cn/jecn/hadoop/hdfs/com/FindFileOnHDFS.java
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());
		}
	}
}
最近下载更多
ewan007  LV29 2023年11月13日
cccccc12  LV8 2022年5月19日
AAAAAAnot  LV1 2021年7月13日
wsupsup  LV16 2021年5月21日
白天  LV1 2021年4月15日
lizhoutao  LV11 2020年12月11日
caine111  LV1 2020年12月9日
xiaoxIXXX  LV1 2020年7月3日
520110  LV1 2020年5月26日
zshb_gxj  LV8 2020年1月2日
最近浏览更多
ningningnn  LV3 4月9日
ewan007  LV29 2023年11月13日
sunlzh888888  LV28 2023年9月15日
飞行家真真  LV1 2023年7月2日
2716804680  LV9 2023年3月23日
1690356080  LV37 2023年2月13日
Trickster  LV9 2022年8月1日
tianqi2020  LV5 2022年5月27日
cccccc12  LV8 2022年5月19日
whfuai  LV14 2022年5月9日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友