ZK zookeeper客户端命令行操作

1 基本语法

命令基本语法 功能描述
help 显示所有操作命令
ls path [watch] 使用 ls 命令来查看当前znode中所包含的内容
ls2 path [watch] 查看当前节点数据并能看到更新次数等数据
create 普通创建 -s 含有序列 -e 临时(重启或者超时消失)
get path [watch] 获得节点的值
set 设置节点的具体值
stat 查看节点状态
delete 删除节点
rmr 递归删除节点

2 基础操作

2.1 启动客户端

[root@localhost ~]$ /opt/module/zookeeper-3.4.10/bin/zkCli.sh

2.2 显示所有操作命令

[zk: localhost:2181(CONNECTED) 1] help

2.3 查看当前 znode 包含内容

[zk: localhost:2181(CONNECTED) 1] ls /

2.4 查看当前节点详细数据

[zk: localhost:2181(CONNECTED) 1] 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

2.5 创建节点

[zk: localhost:2181(CONNECTED) 3] create /xiyouji "baigujing"
Created /xiyouji
[zk: localhost:2181(CONNECTED) 4] create /xiyouji/huaguoshan "sunwukong"
Created /xiyouji/huaguoshan

2.6 获取节点的值

[zk: localhost:2181(CONNECTED) 5] get /xiyouji
baigujing
cZxid = 0x100000003
ctime = Wed Mar 20 19:15:22 CST 2021
mZxid = 0x100000003
mtime = Wed Aug 20 19:15:22 CST 2021
pZxid = 0x100000004
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 1
[zk: localhost:2181(CONNECTED) 6]
[zk: localhost:2181(CONNECTED) 6] get /xiyouji/huaguosha
sunwukong
cZxid = 0x100000004
ctime = Wed Mar 20 19:15:23 CST 2021
mZxid = 0x100000004
mtime = Wed Mar 20 19:15:23 CST 2021
pZxid = 0x100000004
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0

2.7 创建短暂节点

[zk: localhost:2181(CONNECTED) 7] create -e /xiyouji/tianting "erlangshen"
Created /xiyouji/tianting
  1. 在当前客户端可以查看到

    [zk: localhost:2181(CONNECTED) 3] ls /xiyouji 
    [huaguoshan, tianting]
    
  2. 退出客户端之后重启

    [zk: localhost:2181(CONNECTED) 12] quit
    [root@localhost ~]$ /opt/module/zookeeper-3.4.10/bin/zkCli.sh
    
  3. 再次查看根目录短暂节点已删除

    [zk: localhost:2181(CONNECTED) 3] ls /xiyouji 
    [huaguoshan]
    

2.8 创建带序号的节点

创建普通节点

[zk: localhost:2181(CONNECTED) 1] create /xiyouji/tianbing "tianbing6"
Created /xiyouji/tianbing6

创建带序号的节点

[zk: localhost:2181(CONNECTED) 2] create -s /xiyouji/tianbing/tianbing1 "xiaotianquan"
Created /xiyouji/tianbing/tianbing10000000000
[zk: localhost:2181(CONNECTED) 3] create -s /xiyouji/tianbing/tianbing2 "xiaotianquan"
Created /xiyouji/tianbing/tianbing20000000001
[zk: localhost:2181(CONNECTED) 4] create -s /xiyouji/tianbing/tianbing3 "xiaotianquan"
Created /xiyouji/tianbing/tianbing30000000002

如果原来没有序号节点,序号从0开始依次递增。如果原节点下已有2个节点,则再排序时从2开始,以此类推。

2.9 修改节点值

[zk: localhost:2181(CONNECTED) 6] set /xiyouji/tianbing "tuotatianwang"

2.10 节点值变化监听

  1. node1 主机 上注册监听 /xiyouji节点数据变化

    [zk: localhost:2181(CONNECTED) 26] [zk: localhost:2181(CONNECTED) 8] get /xiyouji  watch
    
  2. node2 主机 修改 /xiyouji节点的值

    [zk: localhost:2181(CONNECTED) 1] set /xiyouji "didi"
    
  3. node1 主机 收到数据变化的监听

    WATCHER::
    WatchedEvent state:SyncConnected type:NodeDataChanged path:/xiyouji
    

2.11 节点的子节点变化监听(路径变化)

  1. node1 主机 上注册监听 /huaguoshan 节点的子节点变化

    [zk: localhost:2181(CONNECTED) 26] [zk: localhost:2181(CONNECTED) 8] ls /xiyouji  watch
    
  2. node2 主机 创建 /huaguoshan 的子节点

    [zk: localhost:2181(CONNECTED) 1] create /xiyouji/wujiguan "wujiguan"
    Created /sanguo/wujiguan
    
  3. node1 主机 收到子节点变化的监听

    WATCHER::
    WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/xiyouji
    

2.12 删除节点

[zk: localhost:2181(CONNECTED) 5] delete /xiyouji/wujiguan

2.13 递归删除节点

[zk: localhost:2181(CONNECTED) 5] rmr /xiyouji/huaguoshan

2.14 查看节点状态

[zk: localhost:2181(CONNECTED) 5] stat /xiyouji
cZxid = 0x100000003
ctime = Wed Mar 20 20:03:23 CST 2021
mZxid = 0x100000011
mtime = Wed Mar 20 20:21:23 CST 2021
pZxid = 0x100000014
cversion = 9
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 1

3 相关信息

  • 博文不易,辛苦各位猿友点个关注和赞,感谢

你可能感兴趣的:(ZK zookeeper客户端命令行操作)