ZK的基本使用
3888端口蓬坡,用于leader選舉的端口
命令行使用
創(chuàng)建
create [-s | -e ] path data acl
其中 -s 表示順序節(jié)點吉嫩,
-e 表示 臨時節(jié)點
不加的話表伦,默認(rèn)是持久化節(jié)點
acl是權(quán)限呼巷,默認(rèn)不加權(quán)限限制
sh $ZK_HOME/bin/zkCli.sh
[zk: localhost:2181(CONNECTED) 0] create /zk-jsy-book 122jin
Created /zk-jsy-book
通過執(zhí)行以上命令膘怕,可以在根目錄下創(chuàng)建一個 zk-jsy-book的節(jié)點,其值是 122jin
讀取
ls path
類似于linux下的ls命令吠卷,可以列舉出path下所有的一級子節(jié)點
[zk: localhost:2181(CONNECTED) 2] ls /
[zk-jsy-book, zookeeper]
[zk: localhost:2181(CONNECTED) 3] ls /zk-jsy-book
[]
get path
可以獲取某個節(jié)點的值及其對應(yīng)屬性
[zk: localhost:2181(CONNECTED) 5] create /zk-jsy-book/book1 111 --- 再創(chuàng)建一個子節(jié)點
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獲取節(jié)點值及屬性信息
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 --- 獲取子節(jié)點值及屬性信息
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]
注意,無法刪除一個有子節(jié)點的節(jié)點撤嫩,如上所示偎捎。