天天看點

Hadoop 擷取Active Namenode的IP位址

       由于工作需要,需要拿到目前叢集的Active Namenode的Ip位址,是以寫以下小代碼,防止忘記,記錄一下:

import java.io.IOException;
import java.net.InetSocketAddress;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.hdfs.HAUtil;

public class ActiveNNAdd {

public void getNameNodeAdress() throws Exception {
Configuration conf = new Configuration();

FileSystem system = null;
try {
    system = FileSystem.get(conf);
    InetSocketAddress active = HAUtil.getAddressOfActive(system);
    System.out.println(active.getHostString()); 
    // System.out.println("hdfs port:" + active.getPort());
    // InetAddress address = active.getAddress();
    // System.out.println("hdfs://" + address.getHostAddress() + ":"+ active.getPort()); 
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (system != null) {
            system.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


public static void main(String[] args) {

ActiveNNAdd nn = new ActiveNNAdd() ;

try {
    nn.getNameNodeAdress();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
}
           

  以上代碼是使用Hadoop源碼提供的HAUtil工具類來做的,有興趣的小夥伴還可以嘗試連接配接Zookeeper擷取!

繼續閱讀