Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Zookeeper CLI


May 26, 2021 Zookeeper


Table of contents


The ZooKeeper Command Line Interface (CLI) is used to interact with the ZooKeeper collection for development. It helps debug and resolve different options.

To perform the ZooKeeper CLI operation, first open the ZooKeeper server ("bin/zkServer.sh start"), and then open the ZooKeeper client ("bin/zkCli.sh"). Once the client starts, you can do the following:
1, create znode
2, get the data
3, monitor the change of znode
4, set the data
5, create a child node of znode
6. List the child nodes of znode
7, check the status
8. Remove/delete znode
Now let's look at the commands above one by one with an example.

Create Znodes

Create a znode with a given path. T he flag parameter specifies whether the znode created is temporary, persistent, or sequential. By default, all znode is persistent.
When a session expires or the client disconnects, the temporary node (flag:-e) is automatically deleted.
The sequential node guarantees that the znode path will be unique.
The ZooKeeper collection fills the znode path with a 10-bit serial number. F or example, the znode path /myapp will be converted to /myapp000000001 and the next serial number will be /myapp0000000002. If flag is not specified, znode is considered durable.

Grammar

create /path /data

Example

create /FirstZnode “Myfirstzookeeper-app"

Output

[zk: localhost:2181(CONNECTED) 0] create /FirstZnode “Myfirstzookeeper-app"
Created /FirstZnode

To create a sequential node, add flag: -s, as shown below.

Grammar

create -s /path /data

Example

create -s /FirstZnode second-data

Output

[zk: localhost:2181(CONNECTED) 2] create -s /FirstZnode “second-data"
Created /FirstZnode0000000023

To create a temporary node, add flag: -e, as shown below.

Grammar

create -e /path /data

Example

create -e /SecondZnode “Ephemeral-data"

Output

[zk: localhost:2181(CONNECTED) 2] create -e /SecondZnode “Ephemeral-data"
Created /SecondZnode

Remember that when the client disconnects, the temporary node is deleted. Y ou can try by exiting the ZooKeeper CLI and then reopening the CLI.

Get the data

It returns the associated data for znode and the metadata for the specified znode. Y ou'll get information, such as when the data was last modified, where it was modified, and information about the data. /b11> This CLI is also used to assign monitors to display data-related notifications.

Grammar

get /path 

Example

get /FirstZnode

Output

[zk: localhost:2181(CONNECTED) 1] get /FirstZnode
“Myfirstzookeeper-app"
cZxid = 0x7f
ctime = Tue Sep 29 16:15:47 IST 2015
mZxid = 0x7f
mtime = Tue Sep 29 16:15:47 IST 2015
pZxid = 0x7f
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 22
numChildren = 0

To access sequential nodes, you must enter the full path of znode.

Example

get /FirstZnode0000000023

Output

[zk: localhost:2181(CONNECTED) 1] get /FirstZnode0000000023
“Second-data"
cZxid = 0x80
ctime = Tue Sep 29 16:25:47 IST 2015
mZxid = 0x80
mtime = Tue Sep 29 16:25:47 IST 2015
pZxid = 0x80
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 13
numChildren = 0

Watch (Monitoring)

The monitor displays a notification when the specified znode or znode's child data changes. Y ou can only set watch in the get command.

Grammar

get /path [watch] 1

Example

get /FirstZnode 1

Output

[zk: localhost:2181(CONNECTED) 1] get /FirstZnode 1
“Myfirstzookeeper-app"
cZxid = 0x7f
ctime = Tue Sep 29 16:15:47 IST 2015
mZxid = 0x7f
mtime = Tue Sep 29 16:15:47 IST 2015
pZxid = 0x7f
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 22
numChildren = 0

The output is similar to a normal get command, but it waits in the background for znode to change. Start here. .

Set up the data

Set the data that specifies znode. /b10> After you complete this setup, you can use the get CLI command to examine the data.

Grammar

set /path /data

Example

set /SecondZnode Data-updated

Output

[zk: localhost:2181(CONNECTED) 1] get /SecondZnode “Data-updated"
cZxid = 0x82
ctime = Tue Sep 29 16:29:50 IST 2015
mZxid = 0x83
mtime = Tue Sep 29 16:29:50 IST 2015
pZxid = 0x82
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x15018b47db00000
dataLength = 14
numChildren = 0

If you assign the watch option in the get command, as in the previous command, the output will look something like this.

Output

[zk: localhost:2181(CONNECTED) 1] get /FirstZnode “Mysecondzookeeper-app"

WATCHER: :

WatchedEvent state:SyncConnected type:NodeDataChanged path:/FirstZnode
cZxid = 0x7f
ctime = Tue Sep 29 16:15:47 IST 2015
mZxid = 0x84
mtime = Tue Sep 29 17:14:47 IST 2015
pZxid = 0x7f
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 23
numChildren = 0

Create a child/child node

Creating a child node is similar to creating a new znode. /b10> The only difference is that the path of the child znode will also have a parent path.

Grammar

create /parent/path/subnode/path /data

Example

create /FirstZnode/Child1 firstchildren

Output

[zk: localhost:2181(CONNECTED) 16] create /FirstZnode/Child1 “firstchildren"
created /FirstZnode/Child1
[zk: localhost:2181(CONNECTED) 17] create /FirstZnode/Child2 “secondchildren"
created /FirstZnode/Child2

Children are listed

This command is used to list and display znode's children.

Grammar

ls /path

Example

ls /MyFirstZnode

Output

[zk: localhost:2181(CONNECTED) 2] ls /MyFirstZnode
[mysecondsubnode, myfirstsubnode]

Check the status

The status describes the metadata for the specified znode. /b10> It contains timestamps, version numbers, ACLs, data lengths, and subznode.

Grammar

stat /path

Example

stat /FirstZnode

Output

[zk: localhost:2181(CONNECTED) 1] stat /FirstZnode
cZxid = 0x7f
ctime = Tue Sep 29 16:15:47 IST 2015
mZxid = 0x7f
mtime = Tue Sep 29 17:14:24 IST 2015
pZxid = 0x7f
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 23
numChildren = 0

Remove Znode

Remove the specified znode and recursively all of its child nodes. /b10> This will only happen if such znode is available.

Grammar

rmr /path

Example

rmr /FirstZnode

Output

[zk: localhost:2181(CONNECTED) 10] rmr /FirstZnode
[zk: localhost:2181(CONNECTED) 11] get /FirstZnode
Node does not exist: /FirstZnode

The delete/path command is similar to the remove command, except that it applies only to znode without child nodes.