Apache ZooKeeper CLI (Command Line Interface) Operations
In our last Apache ZooKeeper Tutorial, we discussed ZooKeeper Leader Election. Today, we will see ZooKeeper CLI. In this Zookeeper article, we will learn the whole about ZooKeeper Command Line Interface. First, we will see what is CLI in ZooKeeper.
Also, we will discuss commands in ZooKeeper CLI. Also, there are several operations we can perform with the ZooKeeper ensemble for development purpose, we will also discuss all in detail.
So, let’s start Apache ZooKeeper CLI.
What is ZooKeeper Command Line Interface?
For development purpose, we can interact with the ZooKeeper ensemble, so, for that, we use ZooKeeper CLI (Command Line Interface). Its main purpose is for debugging and working around with different options.
In order to perform ZooKeeper CLI operations, at very first we have to turn on our ZooKeeper server (“bin/zkServer.sh start”). Afterward, we will also turn on ZooKeeper client (“bin/zkCli.sh”). Hence, we can perform the following operation, once the client starts:
- Create znodes.
- Get data.
- Watch znode for changes.
- Set data.
- Create children of a znode.
- List children of a znode.
- Check Status.
- Remove / Delete a znode.
Commands in ZooKeeper CLI
Let’s discuss ZooKeeper CLI Commands in detail:
i. Create Znodes
At very first, with the given path, create a znode. Basically, the flag argument specifies whether the created Znode will be ephemeral, persistent, or sequential. However, all znodes are persistent, by default.
In addition, when a session expires or when the client disconnects, Ephemeral znodes (flag:e) will be automatically deleted.
Although, Sequential znodes make sure that the Znode path will be unique.
Along with 10 digit padding to the znode path, ZooKeeper ensemble will add the sequence number. 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.
a. Syntax
create /path /data
b. Sample
create /FirstZnode “Myfirstzookeeper-app”
c. Output
[zk: localhost:2181(CONNECTED) 0] create /FirstZnode “Myfirstzookeeper-app” Created /FirstZnode
Now, add -s flag, in order to create a Sequential znode:
a. Syntax
create -s /path /data
b. Sample
create -s /FirstZnode second-data
c. Output
[zk: localhost:2181(CONNECTED) 2] create -s /FirstZnode “second-data” Created /FirstZnode0000000023
Further, add -e flag, to create an Ephemeral Znode:
a. Syntax
create -e /path /data
b. Sample
create -e /SecondZnode “Ephemeral-data”
c. Output
[zk: localhost:2181(CONNECTED) 2] create -e /SecondZnode “Ephemeral-data” Created /SecondZnode
Although, make sure the ephemeral znode will be deleted when a client connection is lost. So, by quitting the ZooKeeper CLI and then re-opening the CLI, we can try it.
ii. Get Data
In order to get the associated data of the znode as well as metadata of the specified znode, we use this operation. Information like when the data was last modified, where it was modified, and all the information about the data, we can get from it.
In addition, to assign watches to show notification about the data, we use this ZooKeper CLI Command.
a. Syntax
get /path
b. Sample
get /FirstZnode
c. 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
Though, we must enter the full path of the znode, to access a sequential znode:
a. Sample
get /FirstZnode0000000023
b. 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
iii. Watch
when the specified znode or znode’s children data changes, the operatipon “Watches” show a notification. However, only in get command, we can set a watch.
a. Syntax
get /path [watch] 1
b. Sample
get /FirstZnode 1
c. 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
Although, make sure the output for watch operation is same as normal get command, though it will wait for Znode changes in the background. <Start here>
iv. Set Data
As per its name, this operations sets the data of the specified znode. We can check the data using the get CLI command in ZooKeeper, once we finish this ZooKeeper set operation.
a. Syntax
set /path /data
b. Sample
set /SecondZnode Data-updated
c. 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 we assign watch option in ZooKeeper CLI getting command, its output will be as same as below −
d. 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
v. Create Children / Sub-znode
Basically, creating children is as same as creating new znodes. However, the only difference in both is that the path of the child znode will have the parent path as well.
a. Syntax
create /parent/path/subnode/path /data
b. Sample
create /FirstZnode/Child1 firstchildren
c. 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
vi. List Children
In order to list and display the children of a znode, we use this List Children ZooKeeper CLI command.
a. Syntax
ls /path
b. Sample
ls /MyFirstZnode
c. Output
[zk: localhost:2181(CONNECTED) 2] ls /MyFirstZnode [mysecondsubnode, myfirstsubnode]
vii. Check Status
Here, Status refers to the metadata of a specified znode. It consists of several details like Timestamp, Version number, ACL, Data length, and Children znode as well.
a. Syntax
stat /path
b. Sample
stat /FirstZnode
c. 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
viii. Remove a Znode
The operation, “Remove a Znode”, removes a specified znode and recursively all its children as well. However, only if such a znode is available, this would happen.
a. Syntax
rmr /path
b. Sample
rmr /FirstZnode
c. Output
[zk: localhost:2181(CONNECTED) 10] rmr /FirstZnode [zk: localhost:2181(CONNECTED) 11] get /FirstZnode Node does not exist: /FirstZnode
Though we can say except for the fact that it works only on znodes with no children, Delete (delete /path) command is same as remove command in Apache ZooKeeper Command-Line Interface.
So, this was all in ZooKeeper CLI. Hope you like our explanation of ZooKeeper Command-Line Operations.
Conclusion
Hence, in this ZooKeeper CLI tutorial, we discussed all the operations and the concept of ZooKeeper Command Line Interface. Hope it helps. Still, if any doubt regarding Apache ZooKeeper CLI, ask in the comment tab.
Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google