此篇的操作都是在上篇的基础上进行的。
目录
下 表 为 客 户 端 常 用 的 s h e l l 操 作 color{#FF0000}{下表为客户端常用的shell操作} 下表为客户端常用的shell操作
命令基本语法 | 功能描述 |
---|---|
help | 显示所有操作命令 |
ls path [watch] | 使用 ls 命令来查看当前znode中所包含的内容 |
ls2 path [watch] | 查看当前节点数据并能看到更新次数等数据 |
create | 普通创建 |
create | -s 含有序列 |
create | -e 临时(重启或者超时消失) |
get path [watch] | 获得节点的值 |
set | 设置节点的具体值 |
stat | 查看节点状态 |
delete | 删除节点 |
rmr | 递归删除节点 |
[bigdata@hadoop002 zookeeper-3.4.10]$ bin/zkCli.sh
2. 显示所有操作
[zk: localhost:2181(CONNECTED) 1] help
3. 查看当前znode中所包含的内容
[zk: localhost:2181(CONNECTED) 2] ls /
4. 查看当前节点详细数据
[zk: localhost:2181(CONNECTED) 3] ls2 /
5. 分别创建2个普通节点
[zk: localhost:2181(CONNECTED) 6] create /sanguo "jinlian" Created /sanguo
Node already exists: /sanguo
[zk: localhost:2181(CONNECTED) 7] create /sanguo/shuguo "liubei"
Created /sanguo/shuguo
6. 获取节点的值
[zk: localhost:2181(CONNECTED) 8] get /sanguo
[zk: localhost:2181(CONNECTED) 9] get /sanguo/shuguo
7. 创建短暂节点
[zk: localhost:2181(CONNECTED) 10] create -e /sanguo/wuguo "zhouyu"
Created /sanguo/wuguo
- 1. 在当前客户端时能查看到的
[zk: localhost:2181(CONNECTED) 11] ls /sanguo
[wuguo, shuguo]
- 2. 退出当前客户端然后再重启客户端
[zk: localhost:2181(CONNECTED) 12] quit
[bigdata@hadoop002 zookeeper-3.4.10]$ bin/zkCli.sh
- 3. 再次查看根目录下短暂节点已经删除
[zk: localhost:2181(CONNECTED) 0] ls /sanguo
[shuguo]
8. 创建带序号的节点
- 1.先创建一个普通的根节点/sanguo/weiguo
[zk: localhost:2181(CONNECTED) 1] create /sanguo/weiguo "caocao"
Created /sanguo/weiguo
-
2. 创建带序号的节点
如果原来没有序号节点,序号从0开始依次递增。如果原节点下已有2个节点,则再排序时从2开始,以此类推。
[zk: localhost:2181(CONNECTED) 5] set /sanguo/weiguo "simayi"
10. 节点的值变化监听
- 1. 在hadoop004主机上注册监听/sanguo节点数据变化
[zk: localhost:2181(CONNECTED) 26] [zk: localhost:2181(CONNECTED) 8] get /sanguo watch
- 2. 在hadoop003主机上修改/sanguo节点的数据
[zk: localhost:2181(CONNECTED) 1] set /sanguo "xisi"
- 3. 观察hadoop004主机收到数据变化的监听
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/sanguo
-
4. 图解
- 1. 在hadoop004主机上注册监听/sanguo节点的子节点变化
[zk: localhost:2181(CONNECTED) 1] ls /sanguo watch
[shuguo, weiguo]
- 2. 在hadoop003主机/sanguo节点上创建子节点
[zk: localhost:2181(CONNECTED) 2] create /sanguo/jin "simayi"
Created /sanguo/jin
- 3. 观察hadoop004主机收到子节点变化的监听
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/sanguo
- 4. 图解
[zk: localhost:2181(CONNECTED) 4] delete /sanguo/jin
13. 递归删除节点
[zk: localhost:2181(CONNECTED) 15] rmr /sanguo/shuguo
14. 查看节点状态
[zk: localhost:2181(CONNECTED) 17] stat /sanguo
本篇博客就到这里了,下一篇博客博主将为大家带来Zookeeper的API操作,敬请期待!!!
看
完
就
赞
,
养
成
习
惯
!
!
!
color{#FF0000}{看完就赞,养成习惯!!!}
看完就赞,养成习惯!!!^ _ ^ ❤️ ❤️ ❤️
码字不易,大家的支持就是我坚持下去的动力。点赞后不要忘了关注我哦!
本文摘自 :https://blog.51cto.com/u