zookeeper有类似于mysql的调用命令,在bin目录下可以通过zkCli.sh命令实现
通过命令进入zookeeper后台
(./zkCli.sh -server ip:port)
root@ubuntu:/usr/local/zookeeper00/bin# ./zkCli.sh -server localhost:6181
查看zookeeper中所有节点
( ls / ls2 /)
[zk: localhost:6181(CONNECTED) 6] ls /
[zookeeper, data]
[zk: localhost:6181(CONNECTED) 1] ls2 /
[zookeeper, data]
cZxid = 0x0
ctime = Thu Jan 01 00:00:00 UTC 1970
mZxid = 0x0
mtime = Thu Jan 01 00:00:00 UTC 1970
pZxid = 0x200000002
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 2
创建节点
create [-s] [-e] path data acl [-s]是顺序节点 [-e]是临时节点
[zk: localhost:6181(CONNECTED) 7] create -s /config "001"
Created /config0000000007
[zk: localhost:6181(CONNECTED) 8] create -s /config "002"
Created /config0000000008
[zk: localhost:6181(CONNECTED) 9] create -s /config "003"
Created /config0000000009
[zk: localhost:6181(CONNECTED) 10] ls / #内部按照顺序排列
[zookeeper, data, config0000000008, config0000000009, config0000000007]
[zk: localhost:6181(CONNECTED) 13] create -e /config "005" #创建临时节点
Created /config
[zk: localhost:6181(CONNECTED) 14] ls /
[zookeeper, data, config0000000008, config0000000009, config0000000007, config, config0000000010]
[zk: localhost:6181(CONNECTED) 15] quit #quit退出,再进入
[zk: localhost:6181(CONNECTED) 0] ls / #生成的临时节点 config 随着session结束消失
[zookeeper, data, config0000000008, config0000000009, config0000000007, config0000000010]
[zk: localhost:6181(CONNECTED) 1]
删除节点
delete path 删除一个节点
rmr path 递归删除节点,对于有子节点的情况
[zk: localhost:6181(CONNECTED) 0] ls /
[zookeeper, data, config0000000008, config0000000009, config0000000007, config0000000010]
[zk: localhost:6181(CONNECTED) 3] delete /config0000000008 #删除子节点
[zk: localhost:6181(CONNECTED) 4] delete /config0000000009
[zk: localhost:6181(CONNECTED) 5] delete /config0000000007
[zk: localhost:6181(CONNECTED) 10] delete /config0000000010
Node not empty: /config0000000010 #/config0000000010有子节点,delete报错
[zk: localhost:6181(CONNECTED) 11] ls /config0000000010
[test]
[zk: localhost:6181(CONNECTED) 12] rmr /config0000000010 #用rmr递归删除
[zk: localhost:6181(CONNECTED) 13] ls /
[zookeeper, data]
[zk: localhost:6181(CONNECTED) 14]
获取更新节点数据
get 获取节点
set 更新节点
[zk: localhost:6181(CONNECTED) 16] create /config "" #创建节点
Created /config
[zk: localhost:6181(CONNECTED) 18] ls /
[zookeeper, data, config]
[zk: localhost:6181(CONNECTED) 19] create /config/name "xiaoming" #创建两个子节点并赋值
Created /config/name
[zk: localhost:6181(CONNECTED) 20] create /config/password "123456"
Created /config/password
[zk: localhost:6181(CONNECTED) 22] ls /config
[name, password]
[zk: localhost:6181(CONNECTED) 23] get /config/name #获取其中一个子节点的值
xiaoming
[zk: localhost:6181(CONNECTED) 24] set /config/name "xiaowang" #set赋值
[zk: localhost:6181(CONNECTED) 25] get /config/name #赋值成功
xiaowang
指定配额
setquota -n|-b val path 某个Znode指定多少存储空间或者允许创建多少个节点
n 指定可以设置多少个子节点
b 指定可以设置多大空间(byte)
[zk: 10.143.143.185:6182(CONNECTED) 1] setquota -n 5 /config #设置5个节点限制
Comment: the parts are option -n val 5 path /config
[zk: 10.143.143.185:6182(CONNECTED) 2] ls /config
[name, password]
[zk: 10.143.143.185:6182(CONNECTED) 3] create /config/nickname ""
Created /config/nickname
[zk: 10.143.143.185:6182(CONNECTED) 4] create /config/smallname ""
Created /config/smallname
[zk: 10.143.143.185:6182(CONNECTED) 5] create /config/familyname ""
Created /config/familyname
[zk: 10.143.143.185:6182(CONNECTED) 6] create /config/bigname ""
Created /config/bigname
[zk: 10.143.143.185:6182(CONNECTED) 7] ls /config
[name, nickname, familyname, smallname, password, bigname] #创建了6个节点
[zk: 10.143.143.185:6182(CONNECTED) 8] quit
#查看zookeeper.out日志,看到有日志警告
2019-03-27 09:05:29,477 [myid:7] - WARN [CommitProcessor:7:DataTree@302] - Quota exceeded: /config count=6 limit=5
2019-03-27 09:05:38,671 [myid:7] - WARN [CommitProcessor:7:DataTree@302] - Quota exceeded: /config count=7 limit=5
[zk: 10.143.143.185:6182(CONNECTED) 0] listquota /config #查看配额情况
absolute path is /zookeeper/quota/config/zookeeper_limits
Output quota for /config count=5,bytes=-1
Output stat for /config count=7,bytes=14
[zk: 10.143.143.185:6182(CONNECTED) 1] stat /config #查看节点状态
cZxid = 0x20000002e
ctime = Wed Mar 27 08:22:59 UTC 2019
mZxid = 0x20000002e
mtime = Wed Mar 27 08:22:59 UTC 2019
pZxid = 0x200000052
cversion = 6
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 6
[zk: 10.143.143.185:6182(CONNECTED) 2]
查看节点状态
stat /path
[zk: localhost:6181(CONNECTED) 6] stat /zk-test
cZxid = 0x200000095
ctime = Tue Apr 16 07:26:18 UTC 2019
mZxid = 0x200000096
mtime = Tue Apr 16 07:26:18 UTC 2019
pZxid = 0x200000095
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x6027f3c847d001c
dataLength = 3
numChildren = 0
网友评论