前言
代码语言: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) {
}
}