Apache ZooKeeper Command Line Interface (CLI) usage

posted on Nov 20th, 2016

Apache ZooKeeper

Apache ZooKeeper is a software project of the Apache Software Foundation. It is essentially a distributed hierarchical key-value store, which is used to provide a distributed configuration service, synchronization service, and naming registry for large distributed systems. ZooKeeper was a sub-project of Hadoop but is now a top-level project in its own right.

ZooKeeper's architecture supports high availability through redundant services. The clients can thus ask another ZooKeeper leader if the first fails to answer. ZooKeeper nodes store their data in a hierarchical name space, much like a file system or a tree data structure. Clients can read from and write to the nodes and in this way have a shared configuration service. Updates are totally ordered.

Pre Requirements

1) A machine with Ubuntu 14.04 LTS operating system.

2) Apache ZooKeeper 3.4.6 pre installed (How to set up single server ZooKeeper on Ubuntu 14.04)

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"

Apache ZooKeeper Command Line Interface (CLI) Usage

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

Apache ZooKeeper Command Line Interface (CLI) Usage

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

Please share this blog post and follow me for latest updates on

facebook             google+             twitter             feedburner

Previous Post                                                                                          Next Post

Labels : ZooKeeper Single Server Setup   ZooKeeper Multi Server Setup   ZooKeeper Java API Usage