我尝试使用Java API从Zookeeper的Znode获取数据。紧随下面的链接
https://www.tutorialspoint.com/zookeeper/zookeeper_api.htm程序:zkgetdata.java
我得到这样的结果
"(clustersecondarynn1node4.test.com>(>)
如何单独获取" node4.test.com"。
它是强烈的建议使用某些层来针对ZK,例如Apache Curator或ZkClient。这是一个示例
CuratorFramework zkClient = CuratorFrameworkFactory.builder().connectString("localhost:2181").build();
zkClient.start();
boolean isExist = zkClient.checkExists().forPath("/myNode") != null;
if (isExist) {
byte[] myNodeBytes = zkClient.getData().forPath("/myNode");
if (myNodeBytes != null && myNodeBytes.length > 0) {
System.out.println(new String(myNodeBytes));
}
}
else {
zkClient.create().withMode(CreateMode.PERSISTENT).forPath("/myNode", "foo".getBytes());
}