ZooKeeper的shell操作命令
使用zkServer.sh
启动ZooKeeper服务器后,接下来就可以使用zkCli.sh
脚本启动ZooKeeper客户端来连接ZooKeeper服务器;Windows对应的是zkServer.cmd
和zkCli.cmd
。
(1)使用help
命令可查看ZooKeeper的shell操作可用的命令,如下:
[zk: localhost:2181(CONNECTED) 4] help
ZooKeeper -server host:port cmd args
stat path [watch]
set path data [version]
ls path [watch]
delquota [-n|-b] path
ls2 path [watch]
setAcl path acl
setquota -n|-b val path
history
redo cmdno
printwatches on|off
delete path [version]
sync path
listquota path
rmr path
get path [watch]
create [-s] [-e] path data acl
addauth scheme auth
quit
getAcl path
close
connect host:port
(2)查看当前Znode下包含的内容:ls path [watch]
[zk: localhost:2181(CONNECTED) 5] ls /
[dubbo, zookeeper]
(3)查看当前节点详细的数据:ls2 path [watch]
[zk: localhost:2181(CONNECTED) 6] ls2 /zookeeper
[quota]
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
(4)创建普通节点(必须在创建节点时有数据):create path data acl
[zk: localhost:2181(CONNECTED) 7] create /name
[zk: localhost:2181(CONNECTED) 8] ls /
[dubbo, zookeeper]
[zk: localhost:2181(CONNECTED) 9] create /name "xingze"
Created /name
[zk: localhost:2181(CONNECTED) 10] ls /
[name, dubbo, zookeeper]
[zk: localhost:2181(CONNECTED) 11] ls /name
[]
[zk: localhost:2181(CONNECTED) 12] ls2 /name
[]
cZxid = 0x18
ctime = Wed Dec 18 19:28:31 CST 2019
mZxid = 0x18
mtime = Wed Dec 18 19:28:31 CST 2019
pZxid = 0x18
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
[zk: localhost:2181(CONNECTED) 13] get /name
xingze
cZxid = 0x18
ctime = Wed Dec 18 19:28:31 CST 2019
mZxid = 0x18
mtime = Wed Dec 18 19:28:31 CST 2019
pZxid = 0x18
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
(5)获得节点的值:get path [watch]
[zk: localhost:2181(CONNECTED) 13] get /name
xingze
cZxid = 0x18
ctime = Wed Dec 18 19:28:31 CST 2019
mZxid = 0x18
mtime = Wed Dec 18 19:28:31 CST 2019
pZxid = 0x18
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
(6)创建短暂/临时节点:create -e path data acl
[zk: localhost:2181(CONNECTED) 14] create -e /age 21
Created /age
[zk: localhost:2181(CONNECTED) 15] ls /
[name, dubbo, zookeeper, age]
[zk: localhost:2181(CONNECTED) 16] quit
quit
命令用来退出当前shell操作窗口,接下来重新打开shell客户端连接,查看创建的临时节点是否还存在:
[zk: localhost:2181(CONNECTED) 0] ls /
[name, dubbo, zookeeper]
可以发现,临时节点在当前shell窗口连接断开之后,就会自动删除。
(7)创建带序号的节点:create -s path data acl
[zk: localhost:2181(CONNECTED) 1] create -s /age 21
Created /age0000000003
[zk: localhost:2181(CONNECTED) 2] ls /
[name, age0000000003, dubbo, zookeeper]
重复创建带序号的节点并不会覆盖之前的节点,如下:
[zk: localhost:2181(CONNECTED) 3] create -s /age 21
Created /age0000000004
[zk: localhost:2181(CONNECTED) 4] create -s /age 21
Created /age0000000005
[zk: localhost:2181(CONNECTED) 5] ls /
[name, age0000000003, dubbo, age0000000004, zookeeper, age0000000005]
注:如果原来没有序号节点,序号会从0开始依次递增。如果原节点下已有2个节点,则再排序时从2开始,以此类推。
(8)修改节点数据值:set path data [version]
[zk: localhost:2181(CONNECTED) 6] set /age 22
Node does not exist: /age
[zk: localhost:2181(CONNECTED) 7] ls /
[name, age0000000003, dubbo, age0000000004, zookeeper, age0000000005]
[zk: localhost:2181(CONNECTED) 8] set /age0000000003 22
cZxid = 0x1c
ctime = Wed Dec 18 19:49:49 CST 2019
mZxid = 0x20
mtime = Wed Dec 18 19:56:54 CST 2019
pZxid = 0x1c
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 0
[zk: localhost:2181(CONNECTED) 9] get /age0000000003
22
cZxid = 0x1c
ctime = Wed Dec 18 19:49:49 CST 2019
mZxid = 0x20
mtime = Wed Dec 18 19:56:54 CST 2019
pZxid = 0x1c
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 0
(9)监听节点的子节点变化:涉及到集群,后续补。。。。。。
(10)删除节点:
[zk: localhost:2181(CONNECTED) 0] ls /
[name, age0000000003, dubbo, age0000000004, zookeeper, age0000000005]
[zk: localhost:2181(CONNECTED) 1] delete /age*
Node does not exist: /age*
[zk: localhost:2181(CONNECTED) 2] delete /age0000000003
[zk: localhost:2181(CONNECTED) 3] ls /
[name, dubbo, age0000000004, zookeeper, age0000000005]
来源:CSDN
作者:xingze0516
链接:https://blog.csdn.net/qq_42220174/article/details/103603219