ZooKeeper Command Line Interface (CLI) usage

ZooKeeper Command Line Interface (CLI) usage

ZooKeeper Command Line Interface (CLI) is used to interact with the ZooKeeper ensemble for development purpose. It is useful for debugging and working around with different options.

To perform ZooKeeper CLI operations, first turn on your ZooKeeper server ("bin/zkServer.sh start") and then, ZooKeeper client ("bin/zkCli.sh"). Once the client starts, you can perform the following operation:

1) Create znodes

2) Get data

3) Watch znode for changes

4) Set data

5) Create children of a znode

6) List children of a znode

7) Check Status

8) Remove / Delete a znode

Step 1 - Change the directory to /usr/local/zookeeper/bin

$ cd /usr/local/zookeeper/bin

Step 2 - Start zookeeper server

$ ./zkServer.sh start

Step 3 - Start client

$ ./zkCli.sh -server

Create znodes

Create a znode with the given path. The flag argument specifies whether the created znode will be ephemeral, persistent, or sequential. By default, all znodes are persistent.

a) Ephemeral znodes - (flag: e) will be automatically deleted when a session expires or when the client disconnects.

b) Sequential znodes - guaranty that the znode path will be unique.

c) Persistent - ZooKeeper ensemble will add sequence number along with 10 digit padding to the znode path. For example, the znode path /myapp will be converted to /myapp0000000001 and the next sequence number will be /myapp0000000002. If no flags are specified, then the znode is considered as persistent.

To create a Persistant(default) znode, shown below.

create /FirstZnode "Myfirstzookeeper-app"

To create a Sequential znode, add -s flag as shown below.

create -s /FirstZnode second-data

To create an Ephemeral znode, add -e flag as shown below.

create -e /SecondZnode "Ephemeral-data"

Get Data

It returns the associated data of the znode and metadata of the specified znode. You will get information such as when the data was last modified, where it was modified, and information about the data. This CLI is also used to assign watches to show notification about the data.

get /FirstZnode

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

get /FirstZnode0000000001

Watch znode for changes

Watches show a notification when the specified znode or znode's children data changes. You can set a watch only in get command.

get /FirstZnode 1

Set Data

Set the data of the specified znode. Once you finish this set operation, you can check the data using the get CLI command.

set /FirstZnode updated
get /FirstZnode updated
set /SecondZnode Data-updated
get /SecondZnode Data-updated

Create children of a znode

Creating children is similar to creating new znodes. The only difference is that the path of the child znode will have the parent path as well.

create /FirstZnode/Child1 firstchildren

List children

This command is used to list and display the children of a znode.

ls /FirstZnode

Check Status

Status describes the metadata of a specified znode. It contains details such as Timestamp, Version number, ACL, Data length, and Children znode.

stat /FirstZnode

Remove a Znode

Removes a specified znode and recursively all its children. This would happen only if such a znode is available.

rmr /FirstZnode

Dont forget to stop zookeeper server

$ ./zkServer.sh stop

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *