zkCli 是学 ZooKeeper 的第一个工具。这篇用官方的 ZooKeeperMain 类把基础命令挨个执行一遍,把每条命令的真实输出和各种错误信息记下来,方便以后对照。
[zk] create /app hello
Created /app
[zk] get /app
hello
[zk] set /app world
(无输出)
[zk] get -s /app
world
cZxid = 0x2
ctime = Thu Oct 01 09:09:01 CST 2026
mZxid = 0x5
mtime = Thu Oct 01 09:09:02 CST 2026
pZxid = 0x2
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0
[zk] ls /
[app, zookeeper] [zk] create /app/a 1
Created /app/a
[zk] create /app/b 2
Created /app/b
[zk] ls -R /app
/app
/app/a
/app/b
[zk] delete /app
Node not empty: /app
Exiting JVM with code 1
[zk] deleteall /app
(无输出)
[zk] ls /
[zookeeper] [zk] create /v x
Created /v
[zk] set -v 5 /v y
version No is not valid : /v
Exiting JVM with code 1
[zk] set -v 0 /v y
(无输出)
[zk] get /nope
Node does not exist: /nope
Exiting JVM with code 1
[zk] create /v dup
Node already exists: /v
Exiting JVM with code 1
[zk] create /a/b/c x
Node does not exist: /a/b/c
Exiting JVM with code 1失败命令的最后一行是 Exiting JVM with code 1,这是 ZooKeeperMain 在命令行模式下的行为:命令失败就以退出码 1 结束,写脚本时可以直接用退出码判断。
[zk] create -e /tmp1 data
Created /tmp1
[zk] ls /
[tmp1, v, zookeeper]
[zk] create -s /job- x
Created /job-0000000003
[zk] create -s /job- x
Created /job-0000000004
[zk] ls /
[job-0000000003, job-0000000004, tmp1, v, zookeeper] [zk] getAllChildrenNumber /
7
[zk] ls -s /
[job-0000000003, job-0000000004, tmp1, v, zookeeper]
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x23
cversion = 5
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 5
[zk] whoami
Auth scheme: User
ip: 127.0.0.1
ZooKeeper -server host:port -client-configuration properties-file cmd args
addWatch [-m mode] path # optional mode is one of [PERSISTENT, PERSISTENT_RECURSIVE] - default is PERSISTENT_RECURSIVE
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 [-b batch size]
delquota [-n|-b|-N|-B] path
get [-s] [-w] [-b] [-x] path
getAcl [-s] path
getAllChildrenNumber path