Operating ZooKeeper with zkCli.sh: Interactive and Batch Commands
Location and startup
- zkCli.sh resides in the ZooKeeper installation’s bin directory.
$ ls $ZK_HOME/bin
zkCleanup.sh zkCli.sh zkEnv.sh zkServer.sh
- Connect to a running server (single instance):
$ zkCli.sh -server 127.0.0.1:2181
- On successful connection, an interactive prompt appears. The prompt includes a monotonically increasing history index:
WatchedEvent state:SyncConnected type:None path:null
[zk: 127.0.0.1:2181(CONNECTED) 0]
Discovering available commands
Run help inside the shell to list supported operations:
[zk: 127.0.0.1:2181(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
ls path [watch]
ls2 path [watch]
get path [watch]
stat path [watch]
set path data [version]
create [-s] [-e] path data acl
delete path [version]
getAcl path
setAcl path acl
sync path
addauth scheme auth
listquota path
delquota [-n|-b] path
setquota -n|-b val path
history
redo cmdno
printwatches on|off
close
connect host:port
quit
The numeric token in the prompt increments each time a command is executed and can be used by history/redo.
Non-interactive (one-shot) usage
Commands may be issued directly from the shell without entering interactive mode:
$ zkCli.sh -server 127.0.0.1:2181 stat /
General form:
zkCli.sh -server HOST:PORT COMMAND [ARGS]
Example:
$ zkCli.sh -server 127.0.0.1:2181 help
Command categorise
- Creation and deletion: create, delete
- Existence and metadata: stat
- Data access: get, set
- Children enumeration: ls, ls2
- Synchronization: sync
- Access control: getAcl, setAcl, addauth
- Session/utility: history, redo, printwatches, connect, quit
Working with znodes
Create a top-level znode and write initial data:
[zk: 127.0.0.1:2181(CONNECTED) 1] create /app "v1"
Created /app
Create a child znode with quoted payload:
[zk: 127.0.0.1:2181(CONNECTED) 2] create /app/config "alpha"
Created /app/config
List children of a path (non-rceursive):
[zk: 127.0.0.1:2181(CONNECTED) 3] ls /
[zookeeper, app]
[zk: 127.0.0.1:2181(CONNECTED) 4] ls /app
[config]
Read data and metadata to a znode:
[zk: 127.0.0.1:2181(CONNECTED) 5] get /app
v1
cZxid = 0x3
ctime = Tue Feb 20 10:02:31 UTC 2024
mZxid = 0x3
mtime = Tue Feb 20 10:02:31 UTC 2024
pZxid = 0x4
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 1
[zk: 127.0.0.1:2181(CONNECTED) 6] get /app/config
alpha
cZxid = 0x4
ctime = Tue Feb 20 10:03:02 UTC 2024
mZxid = 0x4
mtime = Tue Feb 20 10:03:02 UTC 2024
pZxid = 0x4
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0
Show only status information (no data payload):
[zk: 127.0.0.1:2181(CONNECTED) 7] stat /app
cZxid = 0x3
ctime = Tue Feb 20 10:02:31 UTC 2024
mZxid = 0x3
mtime = Tue Feb 20 10:02:31 UTC 2024
pZxid = 0x4
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 1
List children and include the parent’s stats in one call:
[zk: 127.0.0.1:2181(CONNECTED) 8] ls2 /
[zookeeper, app]
cZxid = 0x0
ctime = Thu Jan 01 00:00:00 UTC 1970
mZxid = 0x0
mtime = Thu Jan 01 00:00:00 UTC 1970
pZxid = 0x3
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 2
Modify an existing node’s data, returning the new metadata:
[zk: 127.0.0.1:2181(CONNECTED) 9] set /app "v2"
cZxid = 0x3
ctime = Tue Feb 20 10:02:31 UTC 2024
mZxid = 0xa
mtime = Tue Feb 20 10:10:15 UTC 2024
pZxid = 0x4
cversion = 1
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 1
Attempting to delete a non-empty node fails; remove children first:
[zk: 127.0.0.1:2181(CONNECTED) 10] delete /app
Node not empty: /app
[zk: 127.0.0.1:2181(CONNECTED) 11] delete /app/config
[zk: 127.0.0.1:2181(CONNECTED) 12] delete /app
History and replay
Display recent commands and their indices:
[zk: 127.0.0.1:2181(CONNECTED) 13] history
5 - get /app
6 - get /app/config
7 - stat /app
8 - ls2 /
9 - set /app "v2"
10 - delete /app
11 - delete /app/config
12 - delete /app
13 - history
Re-run a previous command by its number:
[zk: 127.0.0.1:2181(CONNECTED) 14] redo 8
[zookeeper, app]
Session termination
Exit the shell:
[zk: 127.0.0.1:2181(CONNECTED) 15] quit
Quitting...