-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathZKGetChildren.java
48 lines (40 loc) · 1.39 KB
/
ZKGetChildren.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package demo;
import java.util.List;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
/**
* https://www.tutorialspoint.com/zookeeper/zookeeper_api.htm
*/
public class ZKGetChildren {
private static ZooKeeper zk;
private static ZooKeeperConnection conn;
public static Stat znodeExists(String path) throws KeeperException, InterruptedException {
return zk.exists(path, true);
}
/**
* >> before running
* [zk: localhost:2181(CONNECTED) 4] create /MyFirstZnode/myfirstsubnode Hi
* Created /MyFirstZnode/myfirstsubnode
* [zk: localhost:2181(CONNECTED) 5] create /MyFirstZnode/mysecondsubmode Hi
* Created /MyFirstZnode/mysecondsubmode
*/
public static void main(String[] args) {
String path = "/MyFirstZnode";
try {
conn = new ZooKeeperConnection();
zk = conn.connect("192.168.5.77");
Stat stat = znodeExists(path);
if (stat != null) {
List<String> children = zk.getChildren(path, false);
for (int i = 0; i < children.size(); i++) {
System.out.println(children.get(i));
}
} else {
System.out.println("Not exist :: " + path);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}