1. 命令行语法:
命令基本语法 功能描述 | |
help 显示所有操作命令 | |
ls path 使用ls命令来查看当前Znode的子节点[可监听] -w 监听子节点变化(watch) -s 附加次级信息 | |
create 普通创建 -s 含有序列(sequential) -e 临时(重启或超时消失) | |
get path 获取节点的值 -w 监听节点内容变化 -s 附加次级信息 | |
set 设置节点的具体值 | |
stat 查看节点状态 | |
delete 删除节点 | |
deleteall 递归删除节点 | |
第一步:启动zookeeper的集群,
再启动客户端
bin/zkCli.sh -server hadoop106:2181
2. znode节点数据信息
2.1 查看当前znode的内容
[zk: hadoop106:2181(CONNECTED) 1] ls /
[zookeeper]
2.2 查看当前节点的详细内容
[zk: hadoop106:2181(CONNECTED) 2] ls -s /
[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
信息解读:
(1)czxId:创建节点的事务zx1d
每次修改 ZooKeeper 状态都会产生一个ZooKeeper 事务 ID。事务ID是 ZooKeeper 中所
有修改总的次序。每次修改都有唯一的 zxid,如果 zxid1 小于 zxid2,那么 zxid1 在 zxid2 之
前发生。
(2)ctime:znode 被创建的毫秒数(从 1970 年开始)
(3)mzxid:znode 最后更新的事务 zxid
(4)mtime:znode 最后修改的亳秒数(从 1970 年开始)
(5)pZxid:znode 最后更新的子节点 zxid
(6)cversion:znode 子节点变化号,znode 子节点修改次数
(6)dataversion:znode 数据变化号
(8)aclVersion:znode 访问控制列表的变化号
(9)ephemeralOwner:如果是临时节点,这个是 znode 拥有者的 session id。如果不是
临时节点则是 0。
(10)dataLength:znode 的数据长度
(11)numChildren:znode 子节点数量
2.3 节点类型
(1)分别创建两个普通节点(永久节点 + 不带序号)
[zk: hadoop106:2181(CONNECTING) 0] create /sanguo "sanguoyanyi"
Created /sanguo
[zk: hadoop106:2181(CONNECTED) 1] create /jinchao "jinchao"
Created /jinchao
(2)获取节点的值 : get -s path
[zk: hadoop106:2181(CONNECTED) 3] get -s /sanguo
sanguoyanyi
cZxid = 0x300000002
ctime = Sat Oct 26 20:40:52 CST 2024
mZxid = 0x300000002
mtime = Sat Oct 26 20:40:52 CST 2024
pZxid = 0x300000002
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 11
numChildren = 0
[zk: hadoop106:2181(CONNECTED) 4] get -s /jinchao
jinchao
cZxid = 0x300000003
ctime = Sat Oct 26 20:41:10 CST 2024
mZxid = 0x300000003
mtime = Sat Oct 26 20:41:10 CST 2024
pZxid = 0x300000003
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0
(3)创建带序号的永久节点
[zk: hadoop106:2181(CONNECTED) 5] create -s /sanguo/shuguo
Created /sanguo/shuguo0000000000
[zk: hadoop106:2181(CONNECTED) 6] create -s /sanguo/shuguo
Created /sanguo/shuguo0000000001
[zk: hadoop106:2181(CONNECTED) 7] create -s /sanguo/wuguo
Created /sanguo/wuguo0000000002
如果原来没有序列节点,序号从0开始依次递增。如果原节点下已经有2个节点,则再排序时从2开始。
(4)创建短暂节点(临时节点 + 带序号与不带序号)
[zk: hadoop106:2181(CONNECTED) 16] create -e /hanchao
Created /hanchao
[zk: hadoop106:2181(CONNECTED) 17] create -e -s /hanchao/hanxiandi
Ephemerals cannot have children: /hanchao/hanxiandi
[zk: hadoop106:2181(CONNECTED) 18] create -e -s /whatcanisay
Created /whatcanisay0000000004
不能在临时节点的下面创建子节点。
(5)修改节点的值
[zk: hadoop106:2181(CONNECTED) 24] get -s /sanguo
sanguoyanyi
cZxid = 0x300000002
ctime = Sat Oct 26 20:40:52 CST 2024
mZxid = 0x300000002
mtime = Sat Oct 26 20:40:52 CST 2024
pZxid = 0x300000006
cversion = 3
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 11
numChildren = 3
[zk: hadoop106:2181(CONNECTED) 25] set /sanguo "sanguo"
[zk: hadoop106:2181(CONNECTED) 26] get -s /sanguo
sanguo
cZxid = 0x300000002
ctime = Sat Oct 26 20:40:52 CST 2024
mZxid = 0x30000000f
mtime = Sat Oct 26 20:59:11 CST 2024
pZxid = 0x300000006
cversion = 3
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 3
2.4 监听器原理
客户端注册监听它关心的目录节点,当目录节点发生节点(数据改变,节点删除,子目录节点但增加或删除)时,zookeeper会通知客户端。监听机制保证zookeeper保存的任何的数据的任何改变都能快速的响应到监听了该节点的应用程序。
(1)节点的值变化监听
在Hadoop07主机上注册监听/sanguo的节点数据变化
[zk: hadoop106:2181(CONNECTED) 0] get -w /sanguo
sanguo
在Hadoop106主机上修改/sanguo节点的值
[zk: hadoop106:2181(CONNECTED) 27] set /sanguo "xxoo"
观察Hadoop107收到数据变化的监听
[zk: hadoop106:2181(CONNECTED) 1]
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/sanguo
注意:在Hadoop106再多次修改/sanguo的值,Hadoop106上不会再收到监听。因为注册一次,只能监听一次。想再次监听,需要再次注册。
(2)节点的子节点变化监听
在Hadoop107主机上注册监听/sanguo节点的子节点变化
[zk: hadoop106:2181(CONNECTED) 0] ls -w /sanguo
[shuguo0000000000, shuguo0000000001, wuguo0000000002]
在Hadoop106主机/sanguo节点上创建节点
[zk: hadoop106:2181(CONNECTED) 28] create /sanguo/xxoo
Created /sanguo/xxoo
观察Hadoop106主机收到了子节点变化的监听
[zk: hadoop106:2181(CONNECTED) 1]
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/sanguo
注意:节点的路径变化,也是注册一次,生效一次。想多次生效,就需要多次注册。
2.5 节点的删除与查看
(1)删除节点
[zk: hadoop106:2181(CONNECTED) 32] delete /sanguo/wuguo0000000002
(2)递归删除节点
deleteall path
(3)查看节点状态
stat path