通过Zookeeper获取激活的HDFS节点

2023-01-13 13:50:01 浏览数 (1)

前言

代码语言:javascript复制
public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
  String hadoopZkNode = "/hadoop-ha/hacluster/ActiveStandbyElectorLock";
  ZooKeeper keeper = new ZooKeeper("hadoop01,hadoop02,hadoop03", 10000, new SimpleWatcher());
  Stat stat = new Stat();
  byte[] data = keeper.getData(hadoopZkNode, new SimpleWatcher(), stat);
  //由于HDFS在向Zookeeper上写数据之前对数据进行了序列化,所以在获取到字节数组时需要调用相应的反序列化方法进行反序列化
  HAZKInfoProtos.ActiveNodeInfo activeNodeInfo = HAZKInfoProtos.ActiveNodeInfo.parseFrom(data);
  System.out.println(activeNodeInfo.getHostname());
}


private static class SimpleWatcher implements Watcher {
  @Override
  public void process(WatchedEvent watchedEvent) {
  }
}

0 人点赞