zookeeper3.4.x客户端(zkCli)常用命令-ls、create、get、set、delete、rmr 作者:马育民 • 2021-11-18 14:53 • 阅读:10109 # 介绍 zookeeper3.4 与 3.5以上版本的命令不同 |命令 |描述 | | ------------ | ------------ | |help |显示所有操作命令 | |ls| 查看当前 znode 中所包含的内容| |create| 创建节点,`-s` 有序节点 ,`-e` 临时节点| |get| 获得节点的数据| |set| 修改节点的数据| |stat| 查看节点状态| |delete| 删除节点| |rmr| 递归删除节点| # 启动 ### 启动服务器端 ``` /program/bin/zk.sh start ``` ### 客户端 ``` cd /program/zookeeper-3.4.14 ``` ``` bin/zkCli.sh ``` ##### 连接指定server ``` bin/zkCli.sh -server hadoop1:2181 ``` # help 查看命令 ``` help ``` 显示结果如下: ``` ZooKeeper -server host:port cmd args addauth scheme auth close config [-c] [-w] [-s] connect host:port create [-s] [-e] [-c] [-t ttl] path [data] [acl] delete [-v version] path deleteall path delquota [-n|-b] path get [-s] [-w] path getAcl [-s] path history listquota path ls [-s] [-w] [-R] path ls2 path [watch] printwatches on|off quit reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*] redo cmdno removewatches path [-c|-d|-a] [-l] rmr path set [-s] [-v version] path data setAcl [-s] [-v version] [-R] path acl setquota -n|-b val path stat [-w] path sync path ``` # ls 查看某节点下的子节点 ``` ls / ``` ### 查看详细信息 ``` ls2 / ``` 显示结果如下: ``` [test, zookeeper]cZxid = 0x0 ctime = Thu Jan 01 08:00:00 CST 1970 mZxid = 0x0 mtime = Thu Jan 01 08:00:00 CST 1970 pZxid = 0x4 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 2 ``` **解释:**详见 [链接](https://www.malaoshi.top/show_1IXKi9e9Ewp.html "链接") # create 创建持久化节点 默认创建节点是持久化节点,必须指定数据 ``` create /services test ``` 执行结果如下: ``` Created /services ``` 继续创建: ``` create /services/notice http://192.168.1.100:8080 ``` 执行结果如下: ``` Created /services/notice ``` # get 获取节点的数据 ``` get /services/notice ``` 执行结果如下: ``` 192.168.1.100:8080 ``` ### 获取详细信息 ``` get -s /services/notice ``` 执行结果如下: ``` http://192.168.1.200:8080 cZxid = 0x5 ctime = Sat Jan 02 09:39:34 CST 2021 mZxid = 0x7 mtime = Sat Jan 02 11:09:59 CST 2021 pZxid = 0x5 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 25 numChildren = 0 ``` # set 修改节点的数据 ``` set /services/notice 192.168.1.200:8081 ``` 无执行结果 # 创建短暂节点 ``` create -e /services/order http://192.168.1.101:8080 ``` **解释:** 参数 `-e` 表示该节点是 **短暂节点**(e是ephemeral首字母) 重新启动该客户端,查看该节点 ``` ls /services/order ``` 显示如下: ``` Node does not exist: /services/order ``` ### 查看 ``` get /services/order ``` 显示如下: [![](/upload/0/0/1IX4SbPbmS0D.png)](/upload/0/0/1IX4SbPbmS0D.png) 其值是 sessionid,如下图: [![](/upload/0/0/1IX4SbounA45.png)](/upload/0/0/1IX4SbounA45.png) # 创建带有序号的持久化节点 ``` create -s /services/user http://192.168.1.102:8080 ``` **解释:** 参数 `-s` 表示该节点是 **带有序号的节点**(s是SEQUENTIAL首字母) 显示结果如下: ``` Created /services/user0000000002 ``` ### 序号为什么从2开始 因为当前 `/test`节点下已经有一个节点了,所以 创建带有序号的节点,序号从2开始 # 创建带有序号的短暂节点 ``` create -s -e /services/user http://192.168.1.102:8080 ``` **解释:** 参数 `-s` 表示该节点是 **带有序号的节点**(s是SEQUENTIAL首字母) 显示结果如下: ``` Created /services/user0000000003 ``` # 删除节点 `delete`可以删除节点。如果有子节点,则不能删除 ### 例子 先创建一个节点 ``` create /services/name lilei ``` 删除该节点 ``` delete /services/name ``` # 删除节点(不论是否有子节点) ``` rmr /services ``` # 监听、通知节点改变 见:https://www.malaoshi.top/show_1IXKSKpP4je.html 原文出处:http://malaoshi.top/show_1IX2FY1u2hiL.html