1、新增节点
(1)先启动ZooKeeper,再登录到ZooKeeper
[root@zhai bin]# ./zkServer.sh start ZooKeeper JMX enabled by default Using config: /home/zookeeper/zookeeper-3.4.10/bin/../conf/zoo.cfg Starting zookeeper ... already running as process 2183. [root@zhai bin]# ./zkCli.sh Connecting to localhost:2181
(2)创建持久化节点并写入数据
[zk: localhost:2181(CONNECTED) 1] create /hadoop "nihao2020" Created /hadoop [zk: localhost:2181(CONNECTED) 2]
根据结点路径读取数据
[zk: localhost:2181(CONNECTED) 2] get /hadoop nihao2020 cZxid = 0x4 ctime = Thu Oct 22 04:02:38 CST 2020 mZxid = 0x4 mtime = Thu Oct 22 04:02:38 CST 2020 pZxid = 0x4 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 9 numChildren = 0 [zk: localhost:2181(CONNECTED) 3]
输入quit命令退出后重新登录依旧能够读取到数据,说明数据是持久化存储的
(3)创建临时结点并写入数据
[zk: localhost:2181(CONNECTED) 1] create -e /test "a" Created /test [zk: localhost:2181(CONNECTED) 2] quit
退出后,使用get命令获取数据的时候数据已经不存在了:
[zk: localhost:2181(CONNECTED) 0] get /test Node does not exist: /test
(4)创建有序节点
[zk: localhost:2181(CONNECTED) 1] create -s /test "a" Created /test0000000002
2、更新节点
(1)更新节点
[zk: localhost:2181(CONNECTED) 2] get /hadoop nihao2020 cZxid = 0x4 ctime = Thu Oct 22 04:02:38 CST 2020 mZxid = 0x4 mtime = Thu Oct 22 04:02:38 CST 2020 pZxid = 0x4 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 9 numChildren = 0 [zk: localhost:2181(CONNECTED) 3] set /hadoop "zhaihuabing" cZxid = 0x4 ctime = Thu Oct 22 04:02:38 CST 2020 mZxid = 0xb mtime = Thu Oct 22 04:19:03 CST 2020 pZxid = 0x4 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 11 numChildren = 0 [zk: localhost:2181(CONNECTED) 4] get /hadoop zhaihuabing cZxid = 0x4 ctime = Thu Oct 22 04:02:38 CST 2020 mZxid = 0xb mtime = Thu Oct 22 04:19:03 CST 2020 pZxid = 0x4 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 11 numChildren = 0 [zk: localhost:2181(CONNECTED) 5]
每修改一次版本号加一
(2)带有版本号的更新
[zk: localhost:2181(CONNECTED) 0] get /hadoop zhaihuabing cZxid = 0x4 ctime = Thu Oct 22 04:02:38 CST 2020 mZxid = 0xb mtime = Thu Oct 22 04:19:03 CST 2020 pZxid = 0x4 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 11 numChildren = 0 [zk: localhost:2181(CONNECTED) 1] set /hadoop "2020001" 0 version No is not valid : /hadoop [zk: localhost:2181(CONNECTED) 2] set /hadoop "2020002" 1 cZxid = 0x4 ctime = Thu Oct 22 04:02:38 CST 2020 mZxid = 0xf mtime = Thu Oct 22 04:26:19 CST 2020 pZxid = 0x4 cversion = 0 dataVersion = 2 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 7 numChildren = 0 [zk: localhost:2181(CONNECTED) 3] get /hadoop 2020002 cZxid = 0x4 ctime = Thu Oct 22 04:02:38 CST 2020 mZxid = 0xf mtime = Thu Oct 22 04:26:19 CST 2020 pZxid = 0x4 cversion = 0 dataVersion = 2 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 7 numChildren = 0
第一次更新的时候版本号版本号不对应,提示版本号错误,在版本号一致的时候更新成功
3、删除节点
(1)删除节点
[zk: localhost:2181(CONNECTED) 4] delete /hadoop [zk: localhost:2181(CONNECTED) 5] get /hadoop Node does not exist: /hadoop
(2)带有版本号的删除
[zk: localhost:2181(CONNECTED) 8] set /hadoop "2020003" Node does not exist: /hadoop [zk: localhost:2181(CONNECTED) 9] create /hadoop "2020003" Created /hadoop [zk: localhost:2181(CONNECTED) 10] get /hadoop 2020003 cZxid = 0x13 ctime = Thu Oct 22 04:32:19 CST 2020 mZxid = 0x13 mtime = Thu Oct 22 04:32:19 CST 2020 pZxid = 0x13 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 7 numChildren = 0 [zk: localhost:2181(CONNECTED) 11] delete /hadoop 1 version No is not valid : /hadoop [zk: localhost:2181(CONNECTED) 12] delete /hadoop 0 [zk: localhost:2181(CONNECTED) 13] get /hadoop Node does not exist: /hadoop
带有版本号以后版本号只有在对应的情况下节点才能删除成功
(3)delete命令不能删除带有子节点的节点
[zk: localhost:2181(CONNECTED) 14] create /test "test" Created /test [zk: localhost:2181(CONNECTED) 15] create /test/t1 "t1" Created /test/t1 [zk: localhost:2181(CONNECTED) 16] delete /test Node not empty: /test
(4)使用rmr命令删除节点以及节点的子节点
[zk: localhost:2181(CONNECTED) 17] rmr /test [zk: localhost:2181(CONNECTED) 18] get /test Node does not exist: /test [zk: localhost:2181(CONNECTED) 19] get /test/t1 Node does not exist: /test/t1