ZooKeeper学习笔记(四)——shell客户端命令操作
ZooKeeper客户端命令行操作
-
启动服务端
[simon@hadoop102 zookeeper-3.4.10]$ bin/zkServer.sh start
-
查看状态信息
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [simon@hadoop102 zookeeper-3.4.10]$ bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Mode: follower
-
启动客户端
[simon@hadoop102 zookeeper-3.4.10]$ bin/zkCli.sh
-
查看当前znode中所包含的内容
[zk: localhost:2181(CONNECTED) 1] ls / [zookeeper]
-
查看当前节点的详细数据
[zk: localhost:2181(CONNECTED) 2] ls2 / [zookeeper] 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
-
分别创建两个普通节点
[zk: localhost:2181(CONNECTED) 3] create /simon "user" [zk: localhost:2181(CONNECTED) 0] create /simon/a "a" Created /simon/a
-
获得节点的值
[zk: localhost:2181(CONNECTED) 1] get /simon user cZxid = 0x100000004 ctime = Thu Oct 24 21:59:31 CST 2019 mZxid = 0x100000004 mtime = Thu Oct 24 21:59:31 CST 2019 pZxid = 0x100000007 cversion = 1 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 4 numChildren = 1 [zk: localhost:2181(CONNECTED) 2] get /simon/a a cZxid = 0x100000007 ctime = Thu Oct 24 22:19:10 CST 2019 mZxid = 0x100000007 mtime = Thu Oct 24 22:19:10 CST 2019 pZxid = 0x100000007 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 1 numChildren = 0
-
创建临时节点
[zk: localhost:2181(CONNECTED) 3] create -e /simon/temp "temp" Created /simon/temp
(1)在客户端可以查看到
[zk: localhost:2181(CONNECTED) 5] ls /simon [a, temp]
(2)退出当前客户端之后再重启客户端
[zk: localhost:2181(CONNECTED) 6] quit Quitting... 2019-10-24 22:22:00,789 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x26dfe0bc0f60002 closed 2019-10-24 22:22:00,790 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x26dfe0bc0f60002 [simon@hadoop102 zookeeper-3.4.10]$ bin/zkCli.sh
(3)再次查看根目录下临时节点已经被删除
[zk: localhost:2181(CONNECTED) 0] ls /simon [a]
-
创建带序号的节点
(1)先创建一个普通的根节点
/simon/b
[zk: localhost:2181(CONNECTED) 1] create /simon/b "b" Created /simon/b
(2)创建一个带序号的节点
[zk: localhost:2181(CONNECTED) 1] create -s /simon/b/ba "ba" Created /simon/b/ba0000000001 [zk: localhost:2181(CONNECTED) 2] create -s /simon/b/bb "bb" Created /simon/b/bb0000000002 [zk: localhost:2181(CONNECTED) 3] create -s /simon/b/bc "bc" Created /simon/b/bc0000000003
如果原来没有节点序号,序号从0开始依次递增。如果原节点下已经有2个节点,则再排序时从2开始,以此类推。
-
修改节点数据的值
[zk: localhost:2181(CONNECTED) 4] set /simon/b "B" [zk: localhost:2181(CONNECTED) 5] get /simon/b [zk: localhost:2181(CONNECTED) 5] get /simon/b B cZxid = 0x10000000b ctime = Thu Oct 24 22:23:07 CST 2019 mZxid = 0x100000012 mtime = Thu Oct 24 22:26:16 CST 2019 pZxid = 0x100000011 cversion = 4 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 1 numChildren = 4
-
节点值的变化监听
(1)在Hadoop104主机上注册监听
/simon
节点数据的变化[zk: localhost:2181(CONNECTED) 0] get /simon watch #监听根节点用:get 子节点:ls user cZxid = 0x100000004 ctime = Thu Oct 24 21:59:31 CST 2019 mZxid = 0x100000004 mtime = Thu Oct 24 21:59:31 CST 2019 pZxid = 0x10000000b cversion = 4 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 4 numChildren = 2
(2)在Hadoop103主机上修改
/simon
节点的数据[zk: localhost:2181(CONNECTED) 0] set /simon "hello world" cZxid = 0x100000004 ctime = Thu Oct 24 21:59:31 CST 2019 mZxid = 0x100000015 mtime = Thu Oct 24 22:29:32 CST 2019 pZxid = 0x10000000b cversion = 4 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 11 numChildren = 2
(3)在Hadoop104节点上观察数据的变化的监听
[zk: localhost:2181(CONNECTED) 1] WATCHER:: WatchedEvent state:SyncConnected type:NodeDataChanged path:/simon
-
节点的子节点变化监听(路劲变化)
(1)在Hadoop104主机上注册监听
/simon
节点的子节点变化[zk: localhost:2181(CONNECTED) 0] ls /simon watch [a, b]
(2)在Hadoop103主机
/simon
节点创建子节点[zk: localhost:2181(CONNECTED) 4] create /simon/c "c" Created /simon/c
(3)观察Hadoop104主机收到的子节点变化的监听
[zk: localhost:2181(CONNECTED) 4] ls /simon watch WATCHER:: WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/simon
-
删除节点
[zk: localhost:2181(CONNECTED) 6] delete /simon/b/bd
-
递归删除节点
[zk: localhost:2181(CONNECTED) 7] rmr /simon/b
-
查看节点的状态
[zk: localhost:2181(CONNECTED) 8] stat /simon cZxid = 0x100000004 ctime = Thu Oct 24 21:59:31 CST 2019 mZxid = 0x100000015 mtime = Thu Oct 24 22:29:32 CST 2019 pZxid = 0x100000021 cversion = 6 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 11 numChildren = 2