命令基本语法 | 功能描述 |
---|---|
help | 显示所有操作命 |
ls path | 使用 ls 命令来查看当前 znode 的子节点 [可监听]-w 监听子节点变化-s 附加次级信息 |
create | 普通创建-s 含有序列-e 临时(重启或者超时消失) |
get path | 获得节点的值 [可监听]-w 监听节点内容变化-s 附加次级信息 |
set | 设置节点的具体值 |
stat | 查看节点状态 |
delete | 删除节点 |
deleteall | 递归删除节点 |
[xqzhao@hadoop100 zookeeper-3.5.7]$ bin/zkCli.sh
[xqzhao@hadoop100 zookeeper-3.5.7]$ bin/zkCli.sh -server hadoop100:2181
[zk: hadoop100:2181(CONNECTED) 1] help
[zk: hadoop100:2181(CONNECTED) 0] quit
[zk: hadoop100:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: hadoop100:2181(CONNECTED) 5] ls -s /
[zookeeper]cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1
czxid
:创建节点的事务 zxidctime
:znode 被创建的毫秒数(从 1970 年开始)mzxid
:znode 最后更新的事务 zxidmtime
:znode 最后修改的毫秒数(从 1970 年开始)pZxid
:znode 最后更新的子节点 zxidcversion
:znode 子节点变化号,znode 子节点修改次数dataversion
:znode 数据变化号aclVersion
:znode 访问控制列表的变化号ephemeralOwner
:如果是临时节点,这个是 znode 拥有者的 session id。如果不是临时节点则是 0。dataLength
:znode 的数据长度numChildren
:znode 子节点数量[zk: hadoop100:2181(CONNECTED) 1] create /sanguo "diaochan"
Created /sanguo
[zk: hadoop100:2181(CONNECTED) 2] ls /
[sanguo, zookeeper]
[zk: hadoop100:2181(CONNECTED) 3] create /sanguo/shuguo "liubei"
Created /sanguo/shuguo
# 注意:创建节点时,要赋值
[zk: hadoop100:2181(CONNECTED) 4] get -s /sanguo
diaochan
cZxid = 0x300000008
ctime = Mon Jul 19 09:07:11 PDT 2021
mZxid = 0x300000008
mtime = Mon Jul 19 09:07:11 PDT 2021
pZxid = 0x300000009
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 1
/sanguo/weiguo
[zk: hadoop100:2181(CONNECTED) 5] create /sanguo/weiguo "caocao"
Created /sanguo/weiguo
[zk: hadoop100:2181(CONNECTED) 6] create -s /sanguo/weiguo/zhangliao "zhangliao"
Created /sanguo/weiguo/zhangliao0000000000
[zk: hadoop100:2181(CONNECTED) 7] ls /sanguo/weiguo
[zhangliao0000000000]
[zk: hadoop100:2181(CONNECTED) 8] create -s /sanguo/weiguo/zhangliao "zhangliao"
Created /sanguo/weiguo/zhangliao0000000001
[zk: hadoop100:2181(CONNECTED) 9] create -s /sanguo/weiguo/xuchu "xuchu"
Created /sanguo/weiguo/xuchu0000000002
如果原来没有序号节点,序号从 0 开始依次递增。如果原节点下已有 2 个节点,则再排序时从 2 开始,以此类推。
[zk: hadoop100:2181(CONNECTED) 10] create -e /sanguo/wuguo "zhouyu"
Created /sanguo/wuguo
[zk: hadoop100:2181(CONNECTED) 11] create -e -s /sanguo/wuguo "zhouyu"
Created /sanguo/wuguo0000000003
[zk: hadoop100:2181(CONNECTED) 12] ls /sanguo
[shuguo, weiguo, wuguo, wuguo0000000003]
# 重启客户端之后
[zk: localhost:2181(CONNECTED) 0] ls /sanguo
[shuguo, weiguo]
(1) 增加节点
增加节点请看本博客第4.3小节
的详细描述。
(2) 删除节点
[zk: localhost:2181(CONNECTED) 4] delete /sanguo/jin
[zk: localhost:2181(CONNECTED) 15] deleteall /sanguo/shuguo
[zk: localhost:2181(CONNECTED) 1] set /sanguo/weiguo "simayi"
[zk: localhost:2181(CONNECTED) 17] stat /sanguo
cZxid = 0x100000003
ctime = Wed Aug 29 00:03:23 CST 2018
mZxid = 0x100000011
mtime = Wed Aug 29 00:21:23 CST 2018
pZxid = 0x100000014
cversion = 9
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 1
hadoop102
主机上注册监听 /sanguo
节点数据变化[zk: localhost:2181(CONNECTED) 4] get -w /sanguo
diaochan
hadoop101
主机上修改 /sanguo
节点的数据[zk: localhost:2181(CONNECTED) 2] set /sanguo "xishi"
hadoop102
主机收到数据变化的监听[zk: localhost:2181(CONNECTED) 5]
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/sanguo
注意:在hadoop101
再多次修改/sanguo的值,hadoop102
上不会再收到监听。因为注册一次,只能监听一次。想再次监听,需要再次注册。
hadoop102
主机上注册监听 /sanguo
节点的子节点变化[zk: localhost:2181(CONNECTED) 2] ls -w /sanguo
[shuguo, weiguo]
[zk: localhost:2181(CONNECTED) 3] create /sanguo/jin "simayi"
Created /sanguo/jin
[zk: localhost:2181(CONNECTED) 3]
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/sanguo
注意:节点的路径变化,也是注册一次,生效一次。想多次生效,就需要多次注册。
声明:本文是学习时记录的笔记,如有侵权请告知删除!
原视频地址:https://www.bilibili.com/video/BV1to4y1C7gw