通过"
DatanodeInfo.getHostName()"可获取HDFS集群上的所有节点名称。具体实现如下:
package
com.hebut.file;
import
org.apache.hadoop.conf.Configuration; import
org.apache.hadoop.fs.FileSystem; import
org.apache.hadoop.hdfs.DistributedFileSystem; import
org.apache.hadoop.hdfs.protocol.DatanodeInfo;
public
class GetList {
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
FileSystem fs=FileSystem.get(conf);
DistributedFileSystem hdfs = (DistributedFileSystem)fs;
DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
for(int i=0;ilength;i++){
System.out.println("DataNode_"+i+"_Name:"+dataNodeStats[i].getHostName());
}
} }