本文ZooKeeper版本:3.8.0
通过终端在zookeeper的bin打开新的session会话
ajisun@ajisun-2 bin> sh zkCli.sh
随便输入一些命令之后就会出现ZooKeeper的命令列表,如下
下面详细介绍下主要API的使用
config
查看仲裁成员的配置
- [zk: 1] config
- server.1=127.0.0.1:2888:3888:participant
- server.2=127.0.0.1:2898:3898:participant
- server.3=127.0.0.1:2988:3988:participant
- version=0
-
ls [-s] [-w] [-R] path
查看某个节点下的节点列表
- # 获取根节点列表
- [zk: 4] ls /
- [ajisun, zookeeper]
-
- # 显示根节点数据
- [zk: 5] ls -s /
- [ajisun, zookeeper]
- cZxid = 0x0
- ctime = Thu Jan 01 08:00:00 CST 1970
- mZxid = 0x0
- mtime = Thu Jan 01 08:00:00 CST 1970
- pZxid = 0x40000000a
- cversion = 36
- dataVersion = 0
- aclVersion = 0
- ephemeralOwner = 0x0
- dataLength = 0
- numChildren = 2
-
- # 递归展示根节点下的所有子节点
- [zk: 6] ls -R /
- /
- /ajisun
- /zookeeper
- /zookeeper/config
- /zookeeper/quota
-
- # 添加监听,当子节点发生变更的时候输出信息
- [zk: 7] ls /
- [ajisun, zookeeper]
- [zk: 8] create /ajisun/01
- Created /ajisun/01
- [zk: 9] ls -w /ajisun
- [01]
- [zk: 10] delete /ajisun/01
- WATCHER::
- WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/ajisun
-
如下命令代表查询的根节点下的所有节点
create [-s] [-e] [-c] [-t ttl] path [data] [acl]
创建一个节点
- # 创建持久化节点 /ajisun
- [zk: 6] create /ajisun
- Created /ajisun
-
- # 创建持久化顺序节点 /ajisun(相同的节点名是自动加上了递增的序号来保证顺序的)
- [zk: 7] create -s /ajisun data
- Created /ajisun0000000005
- [zk: 8] create -s /ajisun data
- Created /ajisun0000000006
-
- # 创建临时节点 /ajisun_ephemeral(当这个session连接断开的时候就会删除此节点)
- [zk: 9] create -e /ajisun_e data
- Created /ajisun_e
-
- # 创建顺序临时节点 /ajisun_s_e
- [zk: 10] create -s -e /ajisun_s_e data
- Created /ajisun_s_e0000000008
- [zk: 11] create -s -e /ajisun_s_e data
- Created /ajisun_s_e0000000009
-
- # 创建容器节点(当容器节点的最后一个子节点被删除,此容器会被删除,但是并不是立即删除)
- [zk: 34] create -c /ajisun_c data
- Created /ajisun_c
- [zk: 35] create -c /ajisun_c/01 data
- Created /ajisun_c/01
- [zk: 36] ls /ajisun_c
- [01]
- [zk: 37] delete /ajisun_c/01
- [zk: 38] get /ajisun_c
- data
- [zk: 39] get /ajisun_c
- Node does not exist: /ajisun_c
-
- # 创建有过期时间的节点
- # 需要在配置文件zoo.cfg中手动开启这个功能,增加配置 extendedTypesEnabled=true
- [zk: 40] create -t 3000 /ajisun_t data
- Created /ajisun_t
- [zk: 41] get /ajisun_t
- data
- [zk: 42] get /ajisun_t
- Node does not exist: /ajisun_t
-
get [-s] [-w] path
获取节点数据
- [zk: 45] create /ajisun 纪先生笔记
- Created /ajisun
-
- # 直接获取节点数据
- [zk: 46] get /ajisun
-
-
- # 显示节点数据信息
- [zk: 47] get -s /ajisun
- # 节点内容数据
- cZxid = 0x400000015 # 节点创建的事务ID(序号递增,每次create操作都会+1,创建session也会+1)
- ctime = Thu Apr 28 17:09:13 CST 2022 # 节点创建的时间
- mZxid = 0x400000015 # 节点最后一个更新时的事务ID
- mtime = Thu Apr 28 17:09:13 CST 2022 # 节点最后一次更新的时间
- pZxid = 0x400000015 # 节点的子节点最后一次被修改的事务ID
- cversion = 0 # 节点的版本号,更改次数
- dataVersion = 0 # 节点数据的版本号
- aclVersion = 0 # 节点ACL的更改次数
- ephemeralOwner = 0x0 # 如果是临时节点,则表示该节点的会话session ID;如果是持久化节点,则是0
- dataLength = 15 # 节点数据的长度
- numChildren = 0 # 当前节点的子节点数
-
- # 节点设置监听,然后更改节点的数据
- [zk: 48] get -w /ajisun
-
- [zk: 49] set /ajisun
- WATCHER::
- WatchedEvent state:SyncConnected type:NodeDataChanged path:/ajisun
- [zk: 50] get /ajisun
-
delete [-v version] path
删除节点(可以根据版本号删除)
- # 直接删除节点 /ajisun/02
- [zk: 54] ls /ajisun
- [01, 02]
- [zk: 55] delete /ajisun/02
- [zk: 56] ls /ajisun
- [01]
-
- #指定版本删除节点 /ajisun/01
- # 查看节点信息
- [zk: 57] ls -s /ajisun/01
- ......
- dataVersion = 0 # 此时版本是0
- ......
-
- # 更新节点信息 并查看
- [zk: 58] set /ajisun/01 ajisun01-01
- [zk: 59] ls -s /ajisun/01
- ......
- dataVersion = 1
- ......
-
- # 指定不存在的版本号删除,删除失败
- [zk: 60] delete -v 0 /ajisun/01
- version No is not valid : /ajisun/01
-
- # 指定正确版本号删除,删除成功
- [zk: 61] delete -v 1 /ajisun/01
- [zk: 62] ls /ajisun
- []
-
deleteall path
删除指定路径下的所有节点
- [zk: 64] ls /
- [ajisun, zookeeper]
-
- # 删除 /ajisun节点
- [zk: 65] deleteall /ajisun
- [zk: 66] ls /
- [zookeeper]
-
set [-s] [-v version] path data
更新指定节点的数据
- [zk: 77] create /ajisun 纪先生
- Created /ajisun
-
- # 设置并显示节点信息
- [zk: 78] set -s /ajisun 纪先生01
- cZxid = 0x40000001e
- ctime = Sun May 01 11:22:19 CST 2022
- mZxid = 0x40000001f
- mtime = Sun May 01 11:23:32 CST 2022
- pZxid = 0x40000001e
- cversion = 0
- dataVersion = 1 # 数据版本变成1
- aclVersion = 0
- ephemeralOwner = 0x0
- dataLength = 11
- numChildren = 0
-
- # 设置0版本失败
- [zk: 79] set -v 0 /ajisun 纪先生02
- version No is not valid : /ajisun
-
- # 设置1版本 成功
- [zk: 80] set -s -v 1 /ajisun 纪先生02
- cZxid = 0x40000001e
- ctime = Sun May 01 11:22:19 CST 2022
- mZxid = 0x400000021
- mtime = Sun May 01 11:28:05 CST 2022
- pZxid = 0x40000001e
- cversion = 0
- dataVersion = 2 # 版本号+1
- aclVersion = 0
- ephemeralOwner = 0x0
- dataLength = 11
- numChildren = 0
- 复制代码
stat [-w] path
查看节点的状态信息
- # 查看节点 /ajisun 信息
- [zk: 90] stat /ajisun
- cZxid = 0x40000001e
- ctime = Sun May 01 11:22:19 CST 2022
- mZxid = 0x400000021
- mtime = Sun May 01 11:28:05 CST 2022
- pZxid = 0x40000001e
- cversion = 0
- dataVersion = 2
- aclVersion = 0
- ephemeralOwner = 0x0
- dataLength = 11
- numChildren = 0
-
- # 节点设置监听(会话一)
- [zk: 91] stat -w /ajisun
- cZxid = 0x40000001e
- ctime = Sun May 01 11:22:19 CST 2022
- mZxid = 0x400000022
- mtime = Sun May 01 14:58:59 CST 2022
- pZxid = 0x40000001e
- cversion = 0
- dataVersion = 3
- aclVersion = 0
- ephemeralOwner = 0x0
- dataLength = 11
- numChildren = 0
-
- # 变更节点(会话二)
- [zk: 0] set /ajisun ajisun-stat
-
- # 收到通知(会话一)
- [zk: 92]
- WATCHER::
- WatchedEvent state:SyncConnected type:NodeDataChanged path:/ajisun
-
removewatches path
移除监听
- # 设置监听
- [zk: 93] get -w /ajisun
- ajisun-stat
-
- # 移除监听
- [zk: 94] removewatches /ajisun
-
- WATCHER::
- WatchedEvent state:SyncConnected type:DataWatchRemoved path:/ajisun
-
history
查看最近执行的11个命令
redo cmdno
重新执行某命令
- # 历史cmd记录
- [zk: 94] history
- 0 - stat -w /ajisun
- 1 - get /ajisun
- 2 - get -s /ajisun
- 3 - get -s /ajisun
- 4 - create /ajisun/01 01
- 5 - get -s /ajisun
- 6 - llll
- 7 - set -w /ajisun
- 8 - get -w /ajisun
- 9 - removewatches /ajisun
- 10 - history
-
- # 重做1命令
- [zk: 95] redo 1
- ajisun-stat
-
以上是常用命令的详细说明和使用方式,包括增删改查以及设置监听等命令