获取NameNode的方法
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(configuration);
DistributedFileSystem dfs = (DistributedFileSystem) fileSystem;
DFSClient client = dfs.getClient();
InetSocketAddress serverAddress = RPC.getServerAddress(client.getNamenode());
System.out.println(serverAddress.getHostName());
如上方法我们可以将fileSystem
强转为DistributedFileSystem
,在通过hadoop自身提供的RPC方法获取到当前的NameNode,但是此方法获取到的Namenode可能不是Active NameNode
,那么如果我们想要获取到Active NameNode怎么做了,只需要用fileSystem检测一下某个一定存在的路径即可
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(configuration);
fileSystem.exists(new Path("/"));
DistributedFileSystem dfs = (DistributedFileSystem) fileSystem;
DFSClient client = dfs.getClient();
InetSocketAddress serverAddress = RPC.getServerAddress(client.getNamenode());
System.out.println(serverAddress.getHostName());
就只多了一句,fileSystem.exists(new Path("/"));
,做这么一个操作就一定能拿到active NameNode