BIG
DATA

JAVA

ZooKeeper Command Line Interface (CLI)

Read more about »
  • Java 9 features
  • Read about Hadoop
  • Read about Storm
  • Read about Storm
 

ZooKeeper commands

ZooKeeper Command Line Interface (CLI) is used to interact with the ZooKeeper ensemble which lets you perform simple, file-like operations. It is useful for debugging purposes.

  • To perform ZooKeeper CLI operations, first start your ZooKeeper server and then, ZooKeeper client by “bin/zkCli.sh”. Once the client starts, from the shell, type help to get a listing of commands that can be executed from the client, as in:

    [zk: localhost:2181(CONNECTED) 0] help
    ZooKeeper -server host:port cmd args
            connect host:port
            get path [watch]
            ls path [watch]
            set path data [version]
            rmr path
            delquota [-n|-b] path
            quit
            printwatches on|off
            create [-s] [-e] path data acl
            stat path [watch]
            close
            ls2 path [watch]
            history
            listquota path
            setAcl path acl
            getAcl path
            sync path
            redo cmdno
            addauth scheme auth
            delete path [version]
            setquota -n|-b val path
    [zk: localhost:2181(CONNECTED) 1]
    
  • From here, you can try a few commands to get a feel for this simple command line interface. First, start by issuing the list command, as in ls:

    [zk: localhost:2181(CONNECTED) 1] ls /
    [zookeeper]
    
  • Next, create a new znode by running create /zk_test my_data. This creates a new znode and associates the string "my_data" with the node. You should see:

    [zk: localhost:2181(CONNECTED) 2] create /zk_test my_data
    Created /zk_test
    
  • Issue another ls / command to see what the directory looks like:

    [zk: localhost:2181(CONNECTED) 3]  ls /
    [zookeeper, zk_test]
    

    Notice that the zk_test directory has now been created.

  • Next, verify that the data was associated with the znode by running the get command, as in:

    [zk: localhost:2181(CONNECTED) 4] get /zk_test
    my_data
    cZxid = 0x8
    ctime = Mon Nov 30 18:41:06 IST 2015
    mZxid = 0x8
    mtime = Mon Nov 30 18:41:06 IST 2015
    pZxid = 0x8
    cversion = 0
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 7
    numChildren = 0
    
  • We can change the data associated with zk_test by issuing the set command, as in:

    [zk: localhost:2181(CONNECTED) 5] set /zk_test junk
    cZxid = 0x8
    ctime = Mon Nov 30 18:41:06 IST 2015
    mZxid = 0x9
    mtime = Mon Nov 30 18:43:06 IST 2015
    pZxid = 0x8
    cversion = 0
    dataVersion = 1
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 4
    numChildren = 0
    
  • Finally, let's delete the node by issuing:

    [zk: localhost:2181(CONNECTED) 6] delete /zk_test
    

Other important Zookeeper commands

Zookeeper Watch

Watches show a notification when the specified znode’s data get changed. You can set a watch only in get command, like:

get /path-to-znode [watch] 1

Lets run the watch command, as in:

[zk: localhost:2181(CONNECTED) 7] get /zk_test 1
my_data
cZxid = 0x8
ctime = Mon Nov 30 18:41:06 IST 2015
mZxid = 0x8
mtime = Mon Nov 30 18:41:06 IST 2015
pZxid = 0x8
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0

The output is similar to get command, but it will wait for znode changes in the background.

Create Sub znode

You create sub-znode in a similar way as creating new znodes. The only difference is that the path of the child znode will have the parent path as well. Lets create a children of zk_test znode, like below:

[zk: localhost:2181(CONNECTED) 8] create /zk_test/Child1 “firstchild”
created /zk_test/Child1
[zk: localhost:2181(CONNECTED) 9] create /zk_test/Child2 “secondchild”
created /zk_test/Child2

List Children

Now to get the list of all children of our znode 'zk_test' type like below:

[zk: localhost:2181(CONNECTED) 10] ls /zk_test
[Child1, Child2]

Check Status

The metadata of a znode contains details such as Timestamp, Version number, ACL, Data length, and Children znode. If you want to view the metadata, use check status command like below:

[zk: localhost:2181(CONNECTED) 11] stat /zk_test
cZxid = 0x8
ctime = Mon Nov 30 18:41:06 IST 2015
mZxid = 0x8
mtime = Mon Nov 30 18:41:06 IST 2015
pZxid = 0x8
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0

Quit

Finally to exit from command line interface, type quit:

[zk: localhost:2181(CONNECTED) 12] quit
Quitting...
[[email protected] bin]$

Zookeeper znode metadata

Using the get command we can view the data and metadata stored in our new znode.

[zk: localhost:2181(CONNECTED) 13] get /zk_test
my_data
cZxid = 0x8
ctime = Mon Nov 30 18:41:06 IST 2015
mZxid = 0x8
mtime = Mon Nov 30 18:41:06 IST 2015
pZxid = 0x8
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0

Let’s examine the data and metadata that is returned:

  • my_data :This line of text is the data that we stored in the znode.
  • cZxid = 0x8 :The zxid (ZooKeeper Transaction Id) of the change that caused this znode to be created.
  • ctime = Mon Nov 30 18:41:06 IST 2015 :The time when this znode was created.
  • mZxid = 0x8 :The zxid of the change that last modified this znode.
  • mtime = Mon Nov 30 18:41:06 IST 2015 :The time when this znode was last modified.
  • pZxid = 0x8 :The zxid of the change that last modified children of this znode.
  • cversion = 0 :The number of changes to the children of this znode.
  • dataVersion = 0 :The number of changes to the data of this znode.
  • aclVersion = 0 :The number of changes to the ACL of this znode.
  • ephemeralOwner = 0x0: The session id of the owner of this znode if the znode is an ephemeral node. If it is not an ephemeral node, it will be zero.
  • dataLength = 7 :The length of the data field of this znode.
  • numChildren = 0 :The number of children of this znode.