共計 1283 個字符,預計需要花費 4 分鐘才能閱讀完成。
ZooKeeper Java 客戶端 API 中如何獲取子節點,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
獲取節點的子節點
public class ZooKeeper_GetChildren_API_Sync_Usage implements Watcher {private static CountDownLatch connectedSemaphore = new CountDownLatch(1);
private static ZooKeeper zk = null;
public static void main (String[] args) throws Exception{
String path = /huangyi
zk = new ZooKeeper( 202.201.13.55:2100 ,
5000,
new ZooKeeper_GetChildren_API_Sync_Usage());
connectedSemaphore.await();
zk.create(path, .getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create(path+ /c1 , .getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
List String childrenList = zk.getChildren(path, true);
System.out.println(childrenList);
zk.create(path+ /c2 , .getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
Thread.sleep( Integer.MAX_VALUE);
public void process(WatchedEvent event) {if (KeeperState.SyncConnected == event.getState()) {if (EventType.None == event.getType() null == event.getPath()) {connectedSemaphore.countDown();
} else if (event.getType() == EventType.NodeChildrenChanged) {
try {System.out.println( ReGet Child: + zk.getChildren(event.getPath(), true));
} catch (Exception e) {}}
結果打印
[c1]
ReGet Child:[c1, c2]
關于 ZooKeeper Java 客戶端 API 中如何獲取子節點問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注丸趣 TV 行業資訊頻道了解更多相關知識。
正文完