import java.io.IOException;
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 GetNodes {
public static void main(String[] args) throws IOException {
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(configuration);
//强转成分布式文件对象
DistributedFileSystem hdfs = (DistributedFileSystem) fs;
//获取节点信息数组
DatanodeInfo[] dis = hdfs.getDataNodeStats();
for(DatanodeInfo info : dis){
String host = info.getHost();
String hostname = info.getHostName();
System.err.println("host->"+host);
System.err.println("name->"+hostname);
}
}
}
Console:
host->192.168.1.109
name->huangpeng-K53SD