zookeeper客户端常用命令
一。连接zookeeper
1. 本地节点连接
[root@host1 bin]# ./zkCli.sh [zk: localhost:2181(CONNECTED) 0]
2. 指定节点连接
[root@host1 bin]# ./zkCli.sh -server host2:2181 [zk: host2:2181(CONNECTED) 0]
3. 客户端里连接
[zk: host2:2181(CONNECTED) 1] connect host3:2181
[zk: host3:2181(CONNECTED) 2]
4. help命令
[zk: host3:2181(CONNECTED) 2] help ZooKeeper -server host:port cmd args stat path [watch] set path data [version] ls path [watch] delquota [-n|-b] path ls2 path [watch] setAcl path acl setquota -n|-b val path history redo cmdno printwatches on|off delete path [version] sync path listquota path rmr path get path [watch] create [-s] [-e] path data acl # create [有序的] [短暂的] 路径 数据 权限 addauth scheme auth quit getAcl path close connect host:port [zk: host3:2181(CONNECTED) 3]
二。操作znode(树状结构,每个znode节点可以包含子节点和数据),ephemeral(短暂)节点不能有子节点
1. 查看znode节点。
[zk: host3:2181(CONNECTED) 9] ls / # / 为根节点
[zookeeper]
[zk: host3:2181(CONNECTED) 10]
2. 创建节点。
persistent(持久)节点(默认的),客户端断开不删除
[zk: host3:2181(CONNECTED) 16] create /app1 123qwe
Created /app1
ephemeral(短暂)节点,客户端断开就删除
[zk: host3:2181(CONNECTED) 19] create -e /app2 123qwefdsssa Created /app2 [zk: host3:2181(CONNECTED) 20]
sequentital(有序的)节点,可以搭配短暂和持久节点一起使用,在此父节点下序号会自曾(与其他父节点的有序节点不影响)
[zk: host3:2181(CONNECTED) 30] create -s /app1/a 123qwe Created /app1/a0000000001 [zk: host3:2181(CONNECTED) 31] create -s /app1/b 123qwewer Created /app1/b0000000002 [zk: host3:2181(CONNECTED) 32] create -s /app1/a 123qweweroeis Created /app1/a0000000003 [zk: host3:2181(CONNECTED) 33]
3 查看节点数据。
[zk: host3:2181(CONNECTED) 18] get /app1 123qwe # 节点保存的数据 cZxid = 0x100000006 # 创建的事务id(内部的事务控制编号) ctime = Sun Sep 02 05:46:06 CST 2018 # 创建时间 mZxid = 0x100000006 # 修改的事务id(内部的事务控制编号),没修改过和创建的事务id相同 mtime = Sun Sep 02 05:46:06 CST 2018 # 修改的时间 pZxid = 0x100000006 # 数据持久化的事务控制id cversion = 0 # 创建的版本号 dataVersion = 0 # 数据的版本号 aclVersion = 0 # 权限的版本号 ephemeralOwner = 0x0 # 短暂节点特有的,Session值 dataLength = 6 # 数据长度 numChildren = 0 # 子节点数 [zk: host3:2181(CONNECTED) 19]
4. 设置节点数据
[zk: host3:2181(CONNECTED) 37] set /app1 0000 cZxid = 0x100000006 ctime = Sun Sep 02 05:46:06 CST 2018 mZxid = 0x10000000e mtime = Sun Sep 02 06:28:57 CST 2018 pZxid = 0x10000000d cversion = 4 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 4 numChildren = 4 [zk: host3:2181(CONNECTED) 38] get /app1
5. 删除节点
只删除没有子节点的节点
[zk: localhost:2181(CONNECTED) 3] delete /app1/a
[zk: localhost:2181(CONNECTED) 4]
递归删除,可删除有子节点的节点
[zk: localhost:2181(CONNECTED) 7] rmr /app1
[zk: localhost:2181(CONNECTED) 8]
三。数据和节点的监督
1. 监督数据变化
[zk: localhost:2181(CONNECTED) 2] get /app1 watch 0000 cZxid = 0x100000006 ctime = Sun Sep 02 05:46:06 CST 2018 mZxid = 0x10000000e mtime = Sun Sep 02 06:28:57 CST 2018 pZxid = 0x10000000f cversion = 5 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 4 numChildren = 3 [zk: localhost:2181(CONNECTED) 3]
在其他地方改变 /app1 节点的数据,能够监听到数据发生改变,只监听一次
[zk: localhost:2181(CONNECTED) 3] WATCHER:: WatchedEvent state:SyncConnected type:NodeDataChanged path:/app1 [zk: localhost:2181(CONNECTED) 3]
2. 监督节点变化
[zk: localhost:2181(CONNECTED) 3] ls /app1 watch
[b0000000002, a0000000001, a0000000003]
[zk: localhost:2181(CONNECTED) 4]
在其他地方 改变 /app1 子节点,能够监听到。只能监听一次,且不包括下一级的节点变化。
[zk: localhost:2181(CONNECTED) 4] WATCHER:: WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/app1 [zk: localhost:2181(CONNECTED) 4]