其他分享
首页 > 其他分享> > Zookeeper之客户端命令行操作

Zookeeper之客户端命令行操作

作者:互联网

Zookeeper客户端命令行操作

  1. 启动客户端
    [codecat@hadoop103 zookeeper-3.5.9]$ zkCli.sh
    
  2. 显示所有操作命令
    [zk: localhost:2181(CONNECTED) 0] help
    
  3. 查看当前 znode 中所包含的内容
    [zk: localhost:2181(CONNECTED) 0] ls /
    
  4. 查看当前节点详细数据
    [zk: localhost:2181(CONNECTED) 2] ls -s /
    
  5. 分别创建 2 个普通节点
    [zk: localhost:2181(CONNECTED) 3] create /sanguo "jinlian"
    Created /sanguo
    [zk: localhost:2181(CONNECTED) 4] create /sanguo/shuguo "liubei"
    Created /sanguo/shuguo
    
  6. 获得节点的值
    [zk: localhost:2181(CONNECTED) 5] get /sanguo 
    jinlian
    
  7. 创建短暂节点
    [zk: localhost:2181(CONNECTED) 7] create -e /sanguo/wuguo "zhouyu"
    
    • 在当前客户端是能查看到的
      [zk: localhost:2181(CONNECTED) 8] ls /sanguo 
      [shuguo, wuguo]
      
    • 退出当前客户端然后再重启客户端,再次查看根目录下短暂节点已经删除
      [zk: localhost:2181(CONNECTED) 0] ls /sanguo 
      [shuguo]
      
  8. 创建带序号的节点
    • 先创建一个普通的根节点
      [zk: localhost:2181(CONNECTED) 1] create /sanguo/weiguo "caocao"
      Created /sanguo/weiguo
      
    • 创建带序号的节点
      [zk: localhost:2181(CONNECTED) 2] create -s /sanguo/weiguo/xiaoqiao "jinlian"
      Created /sanguo/weiguo/xiaoqiao0000000000
      [zk: localhost:2181(CONNECTED) 3] create -s /sanguo/weiguo/daqiao "jinlian"
      Created /sanguo/weiguo/daqiao0000000001
      [zk: localhost:2181(CONNECTED) 4] create -s /sanguo/weiguo/diaocan "jinlian"
      Created /sanguo/weiguo/diaocan0000000002
      
      如果原来没有序号节点,序号从 0 开始依次递增。如果原节点下已有 2 个节点,则再排序时从 2 开始,以此类推
  9. 修改节点数据值
    [zk: localhost:2181(CONNECTED) 5] set /sanguo/weiguo "simayi"
    
  10. 节点的值变化监听
    • hadoop104 主机上注册监听/sanguo 节点数据变化
      [zk: localhost:2181(CONNECTED) 1] get -w /sanguo 
      
    • hadoop103 主机上修改/sanguo 节点的数据
      [zk: localhost:2181(CONNECTED) 6] set /sanguo "xisi"
      
    • 观察 hadoop104 主机收到数据变化的监听
      WATCHER::
      WatchedEvent state:SyncConnected type:NodeDataChanged path:/sanguo
      
  11. 节点的子节点变化监听(路径变化)
    • hadoop104 主机上注册监听/sanguo 节点的子节点变化
      [zk: localhost:2181(CONNECTED) 4] ls -w /sanguo 
      
    • hadoop103 主机/sanguo节点上创建子节点
      [zk: localhost:2181(CONNECTED) 7] create /sanguo/jin "simayi"
      
    • 观察 hadoop104 主机收到子节点变化的监听
      WATCHER::
      WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/sanguo
      
  12. 删除节点
    [zk: localhost:2181(CONNECTED) 8] delete /sanguo/jin
    
  13. 递归删除节点
    [zk: localhost:2181(CONNECTED) 9] deleteall /sanguo/shuguo
    
  14. 查看节点状态
    [zk: localhost:2181(CONNECTED) 12] stat /sanguo
    

标签:zk,Zookeeper,2181,CONNECTED,命令行,sanguo,节点,localhost,客户端
来源: https://blog.csdn.net/qq_42735631/article/details/120545883