ZK的基本使用
3888端口,用于leader选举的端口
命令行使用
创建
create [-s | -e ] path data acl
其中 -s 表示顺序节点,
-e 表示 临时节点
不加的话,默认是持久化节点
acl是权限,默认不加权限限制
sh $ZK_HOME/bin/zkCli.sh
[zk: localhost:2181(CONNECTED) 0] create /zk-jsy-book 122jin
Created /zk-jsy-book
通过执行以上命令,可以在根目录下创建一个 zk-jsy-book的节点,其值是 122jin
读取
ls path
类似于linux下的ls命令,可以列举出path下所有的一级子节点
[zk: localhost:2181(CONNECTED) 2] ls /
[zk-jsy-book, zookeeper]
[zk: localhost:2181(CONNECTED) 3] ls /zk-jsy-book
[]
get path
可以获取某个节点的值及其对应属性
[zk: localhost:2181(CONNECTED) 5] create /zk-jsy-book/book1 111 --- 再创建一个子节点
Created /zk-jsy-book/book1
[zk: localhost:2181(CONNECTED) 6] ls /zk-jsy-book --- ls查看
[book1]
[zk: localhost:2181(CONNECTED) 7] get /zk-jsy-book --- get获取节点值及属性信息
122jin
cZxid = 0x2d
ctime = Tue Jul 24 12:47:37 GMT 2018
mZxid = 0x2d
mtime = Tue Jul 24 12:47:37 GMT 2018
pZxid = 0x2e
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 1
[zk: localhost:2181(CONNECTED) 8] get /zk-jsy-book/book1 --- 获取子节点值及属性信息
111
cZxid = 0x2e
ctime = Tue Jul 24 13:21:01 GMT 2018
mZxid = 0x2e
mtime = Tue Jul 24 13:21:01 GMT 2018
pZxid = 0x2e
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
更新
set path data [version]
[zk: localhost:2181(CONNECTED) 9] set /zk-jsy-book test
cZxid = 0x2d
ctime = Tue Jul 24 12:47:37 GMT 2018
mZxid = 0x2f
mtime = Tue Jul 24 13:39:13 GMT 2018
pZxid = 0x2e
cversion = 1
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 1
[zk: localhost:2181(CONNECTED) 10] get /zk-jsy-book
test
cZxid = 0x2d
ctime = Tue Jul 24 12:47:37 GMT 2018
mZxid = 0x2f
mtime = Tue Jul 24 13:39:13 GMT 2018
pZxid = 0x2e
cversion = 1
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 1
[zk: localhost:2181(CONNECTED) 11]
可以看到,dataVersion变成了1
删除
delete path
[zk: localhost:2181(CONNECTED) 11] ls /
[zk-jsy-book, zookeeper]
[zk: localhost:2181(CONNECTED) 12] delete /zk-jsy-book
Node not empty: /zk-jsy-book
[zk: localhost:2181(CONNECTED) 13] delete /zk-jsy-book/book1
[zk: localhost:2181(CONNECTED) 14] get /zk-jsy-book/book1
Node does not exist: /zk-jsy-book/book1
[zk: localhost:2181(CONNECTED) 15] delete /zk-jsy-book
[zk: localhost:2181(CONNECTED) 16] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 17]
注意,无法删除一个有子节点的节点,如上所示。