Zookeeper系列(六)Zookeeper客户端(JAVA)API之创读取子节点列表
2017-05-11 14:53
501 查看
本篇博客介绍一下Zookeeper原生客户端API提供的获取子节点列表方法。
Zookeeper原生客户端API提供了以下8中获取子节点列表的方法,每个方法的使用说明参考注释内容:
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
2
3
4
1
2
3
4
2
3
4
5
1
2
3
4
5
2
3
4
5
1
2
3
4
5
2
3
4
5
6
1
2
3
4
5
6
2
3
4
1
2
3
4
2
3
4
5
1
2
3
4
5
2
3
4
5
1
2
3
4
5
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
49
50
51
52
53
54
55
56
57
58
59
60
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
49
50
51
52
53
54
55
56
57
58
59
60
输出结果:
2
3
4
1
2
3
4
上面的例子,先创建了一个父节点test,然后在其下面创建了a1子节点,并注册监听。当父节点test下面创建了新的a2子节点之后,监听事件触发。但此时需要重新主动获取该父节点下面的子节点,并继续注册监听事件。
上面只提供了新增子节点的案例,其实删除子节点或删除父节点同样会触发监听事件。
获取子节点列表方法
方法
Zookeeper原生客户端API提供了以下8中获取子节点列表的方法,每个方法的使用说明参考注释内容:/** * 返回指定路径下面的子节点列表。 * 如果watcher不为null,并且调用成功(没有异常),会将watcher注册在指定的path * 上。当path(父节点)被删除或者path下面创建/删除子节点,将触发通知watcher。 * * 返回结果列表不保证有序性。 */ public List<String> getChildren(final String path, Watcher watcher)1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
/** * 使用说明同上一个方法。 */ public List<String> getChildren(String path, boolean watch)1
2
3
4
1
2
3
4
/** * getChildren(String, Watcher)的异步版本 */ public void getChildren(final String path, Watcher watcher, ChildrenCallback cb, Object ctx)1
2
3
4
5
1
2
3
4
5
/** * getChildren(String, boolean)的异步版本 */ public void getChildren(String path, boolean watch, ChildrenCallback cb, Object ctx)1
2
3
4
5
1
2
3
4
5
/** * 为指定的路径返回stat和子节点列表。 * */ public List<String> getChildren(final String path, Watcher watcher, Stat stat)1
2
3
4
5
6
1
2
3
4
5
6
/** * 使用说明同上一个方法。 */ public List<String> getChildren(String path, boolean watch, Stat stat)1
2
3
4
1
2
3
4
/** * getChildren(String, Watcher, Stat)的异步版本 */ public void getChildren(final String path, Watcher watcher, Children2Callback cb, Object ctx)1
2
3
4
5
1
2
3
4
5
/** * getChildren(String, boolean, Stat)的异步版本 */ public void getChildren(String path, boolean watch, Children2Callback cb, Object ctx)1
2
3
4
5
1
2
3
4
5
参数说明
参数名 | 说明 |
---|---|
path | 指定数据节点的路径,获取该节点下面的子节点 |
watcher | 注册在path上的Watcher。path被删除或者path下面的创建/删除节点,向客户端发送通知。可为null |
watch | 表示是否需要注册一个watcher。true:注册默认watcher,false:不需要注册watcher |
cb | 注册一个异步的回调函数 |
ctx | 用户传递信息 |
stat | 指定数据节点状态信息。传入旧stat,方法执行过程中会将其替换为新stat对象。 |
具体案例
package com.secbro.learn; import org.apache.zookeeper.*; import java.util.List; import java.util.concurrent.CountDownLatch; /** * 获得子节点列表的方法 * Created by zhuzs on 2017/3/26. */ public class TestGetChildrenMethod implements Watcher { private static CountDownLatch countDownLatch = new CountDownLatch(1); private static ZooKeeper zooKeeper; public static void main(String[] args) throws Exception { zooKeeper = new ZooKeeper("127.0.0.1:2181", 5000, new TestGetChildrenMethod()); countDownLatch.await(); // 创建父节点/test zooKeeper.create("/test", "123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); // 在父节点/test下面创建a1节点 zooKeeper.create("/test/a1", "456".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); // 同步获得结果 List<String> childrenList = zooKeeper.getChildren("/test", true); System.out.println("同步getChildren获得数据结果:" + childrenList); // 异步获得结果 zooKeeper.getChildren("/test",true,new MyChildren2Callback(),null); // 在父节点/test下面创建a2节点 zooKeeper.create("/test/a2", "456".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); Thread.sleep(10000); } public void process(WatchedEvent event) { if (Event.KeeperState.SyncConnected == event.getState()) { if(Event.EventType.None == event.getType() && null == event.getPath()){ // 连接时的监听事件 countDownLatch.countDown(); } else if (event.getType() == Event.EventType.NodeChildrenChanged){ // 子节点变更时的监听 try { System.out.println("重新获得Children,并注册监听:" + zooKeeper.getChildren(event.getPath(),true)); } catch (Exception e) { e.printStackTrace(); } } } } } class MyChildren2Callback implements AsyncCallback.Children2Callback{ public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) { System.out.println("异步获得getChildren结果,rc=" + rc + ";path=" + path + ";ctx=" + ctx + ";children=" + children +";stat=" + stat); } }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
49
50
51
52
53
54
55
56
57
58
59
60
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
49
50
51
52
53
54
55
56
57
58
59
60
输出结果:
同步getChildren获得数据结果:[a1] 异步获得getChildren结果,rc=0;path=/test;ctx=null;children=[a1];stat=14277,14277,1490498559704,1490498559704,0,1,0,0,3,1,14278 重新获得Children,并注册监听:[a1, a2]1
2
3
4
1
2
3
4
上面的例子,先创建了一个父节点test,然后在其下面创建了a1子节点,并注册监听。当父节点test下面创建了新的a2子节点之后,监听事件触发。但此时需要重新主动获取该父节点下面的子节点,并继续注册监听事件。
上面只提供了新增子节点的案例,其实删除子节点或删除父节点同样会触发监听事件。
相关文章推荐
- Zookeeper系列(七)Zookeeper客户端(JAVA)API之创读取子节点内容
- Zookeeper系列(八)Zookeeper客户端(JAVA)API之修改、删除节点
- Zookeeper系列(八)Zookeeper客户端(JAVA)API之节点检查和权限控制
- Zookeeper系列(五)Zookeeper客户端(JAVA)API之创建节点
- ZooKeeper之Java客户端API使用—删除节点。
- ZooKeeper之Java客户端API使用—创建节点。
- Zookeeper客户端API之读取子节点列表(八)
- ZooKeeper之Java客户端API使用—检测节点是否存在
- Zookeeper客户端API之读取子节点列表(八)
- Zookeeper系列(四)Zookeeper客户端(JAVA)API之创建会话
- Zookeeper客户端API之节点检查和权限控制(十一)
- Zookeeper的java客户端API使用方法(五)
- java_api操作zookeeper节点
- Zookeeper客户端基本操作java实现——创建连接、创建节点、添加修改节点内容、获取子节点、获取节点数据、删除节点
- zookeeper的Java客户端API
- Zookeeper实例原生API--异步获取子节点列表
- zookeeper学习之java api查找并删除节点<五>
- hadoop系列二:HDFS文件系统的命令及JAVA客户端API
- zookeeper(二) java客户端api的使用
- 六:ZooKeeper的java客户端api的使用