zookeeper3.8.0 常用命令

概念1:zookeeper 有两种节点类型分为持久节点与短暂节点,持久节点当客户端与zookeeper断开后连接后节点依旧存在,短暂节点客户端和服务端断开连接后,创建的节点会自动删除

概念2:带序号节点和不带序号节点,带序号节点可以创建相同节点,序号会自动增加,不带序号节点,不能创建相同节点

概念3:客户端注册监听它关心的目录,当前目录节点发生变化(数据改变,节点删除,子目录节点增加删除)时,ZooKeeper 会通知客户端。监听机制保证Zookeeper保存的任何的数据的任何改变都能快速的响应到监听了改节点的应用程序。

创建节点-持久节点

#创建普通节点
[zk: localhost:2181(CONNECTED) 1] create /animal "动物"
Created /animal
#获取根节点下的节点
[zk: localhost:2181(CONNECTED) 2] ls /
[animal, zookeeper]
#获取节点信息
[zk: localhost:2181(CONNECTED) 3] get -s /animal
动物
#创建节点的事务id
cZxid = 0x30000000e 
#创建时间
ctime = Mon Jun 27 15:03:21 CST 2022
#最后一次创建节点的事务id
mZxid = 0x30000000e
#修改的时间
mtime = Mon Jun 27 15:03:21 CST 2022
#父节点的id
pZxid = 0x30000000e
#创建的版本
cversion = 0
#数据库的版本
dataVersion = 0
#权限的版本
aclVersion = 0
#是否是临时节点
ephemeralOwner = 0x0
#数据的长度
dataLength = 6
#子节点的个数
numChildren = 0
#创建有序节点
[zk: localhost:2181(CONNECTED) 4] create -s /animal/birds/xique "喜鹊"
Created /animal/birds/xique0000000000
[zk: localhost:2181(CONNECTED) 5] ls /animal/birds
[xique0000000000]
[zk: localhost:2181(CONNECTED) 6] create /animal/birds/banjiu
Created /animal/birds/banjiu
[zk: localhost:2181(CONNECTED) 7] ls /animal/birds
[banjiu, xique0000000000]
[zk: localhost:2181(CONNECTED) 8] create -s /animal/birds/dujuan
Created /animal/birds/dujuan0000000002
[zk: localhost:2181(CONNECTED) 9] ls /animal/birds
[banjiu, dujuan0000000002, xique0000000000]
[zk: localhost:2181(CONNECTED) 10] create -s /animal/birds/gezi
Created /animal/birds/gezi0000000003
[zk: localhost:2181(CONNECTED) 11] ls /animal/birds
[banjiu, dujuan0000000002, gezi0000000003, xique0000000000]
#创建短暂节点
[zk: localhost:2181(CONNECTED) 12] create -e /animal/birds/wuya "乌鸦"
Created /animal/birds/wuya
#创建带序号的短暂节点
[zk: localhost:2181(CONNECTED) 13] create -e -s /animal/birds/yeji "野鸡"
Created /animal/birds/yeji0000000006
[zk: localhost:2181(CONNECTED) 7] ls /animal/birds
[banjiu, dujuan0000000002, gezi0000000003, wuya, xique0000000000, yeji0000000006]
#退出
[zk: localhost:2181(CONNECTED) 8] quit
#退出后暂时节点自动删除
[zk: localhost:2181(CONNECTED) 0] ls /animal/birds
[banjiu, dujuan0000000002, gezi0000000003, xique0000000000]
#修改节点的值
[zk: localhost:2181(CONNECTED) 2] set /animal/birds/banjiu "斑鸠"
[zk: localhost:2181(CONNECTED) 3] get -s /animal/birds/banjiu
斑鸠
cZxid = 0x300000015
ctime = Mon Jun 27 15:30:28 CST 2022
mZxid = 0x300000023
mtime = Mon Jun 27 16:25:25 CST 2022
pZxid = 0x300000015
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
#监听节点
[zk: localhost:2181(CONNECTED) 9] get -w /animal/birds/banjiu
斑鸠
#更改节点信息
[zk: localhost:2181(CONNECTED) 11] set /animal/birds/banjiu "斑鸠"
#
[zk: localhost:2181(CONNECTED) 10]
WATCHER::
##监听到节点数据发生变更,如果再次修改不会再监听,因为注册一次,监听一次,想再次监听,需要再次注册
WatchedEvent state:SyncConnected type:NodeDataChanged path:/animal/birds/banjiu
#监听节点
[zk: localhost:2181(CONNECTED) 14] ls -w /animal
[birds]
#创建节点 
[zk: localhost:2181(CONNECTED) 4] create /animal/beast
Created /animal/beast
[zk: localhost:2181(CONNECTED) 15]
WATCHER::
#监听到子节点发生变更
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/animal
[zk: localhost:2181(CONNECTED) 16] ls /animal
[beast, birds]
#删除节点
[zk: localhost:2181(CONNECTED) 17] delete /animal/beast
[zk: localhost:2181(CONNECTED) 18] ls /animal
[birds]
#删除包含子节点的父节点
[zk: localhost:2181(CONNECTED) 19] deleteall /animal
#查看节点状态信息
[zk: localhost:2181(CONNECTED) 21] stat /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 = -2
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 2
#退出客户端
[zk: localhost:2181(CONNECTED) 25] quit








你可能感兴趣的:(分布式,大数据)