Step 2: ZooKeeper Framework Installation
6. ZOOKEEPER – CLI
16
Output
[zk: localhost:2181(CONNECTED) 0] create /FirstZnode “Myfirstzookeeper-app”
Created /FirstZnode
To create a Sequential znode, add -s flag as shown below.
Syntax
create -s /path /data
Sample
create -s /FirstZnode second-data
Output
[zk: localhost:2181(CONNECTED) 2] create -s /FirstZnode “second-data”
Created /FirstZnode0000000023
To create an Ephemeral znode, add -e flag as shown below.
Syntax
create -e /path /data
Sample
create -e /SecondZnode “Ephemeral-data”
Output
[zk: localhost:2181(CONNECTED) 2] create -e /SecondZnode “Ephemeral-data”
Created /SecondZnode
Remember when a client connection is lost, the ephemeral znode will be deleted. You can try it by quitting the ZooKeeper CLI and then re-opening the CLI.
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.
17
Syntax
get /path
Sample
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 a sequential znode, you must enter the full path of the znode.
Sample
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
18 cversion = 0
dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 13 numChildren = 0
Watch
Watches show a notification when the specified znode or znode’s children data changes. You can set a watch only in get command.
Syntax
get /path [watch] 1
Sample
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 normal get command, but it will wait for znode changes in the background. <Start here>
19
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.
Syntax
set /path /data
Sample
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 assigned watch option in get command (as in previous command), then the output will be similar as shown below:
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
20 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 Children / Sub-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.
Syntax
create /parent/path/subnode/path /data
Sample
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
List Children
This command is used to list and display the children of a znode.
Syntax
ls /path
Sample
ls /MyFirstZnode
21
Output
[zk: localhost:2181(CONNECTED) 2] ls /MyFirstZnode [mysecondsubnode, myfirstsubnode]
Check Status
Status describes the metadata of a specified znode. It contains details such as Timestamp, Version number, ACL, Data length, and Children znode.
Syntax
stat /path
Sample
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
22
Remove a Znode
Removes a specified znode and recursively all its children. This would happen only if such a znode is available.
Syntax
rmr /path
Sample
rmr /FirstZnode
Output
[zk: localhost:2181(CONNECTED) 10] rmr /FirstZnode [zk: localhost:2181(CONNECTED) 11] get /FirstZnode Node does not exist: /FirstZnode
Delete (delete /path) command is similar to remove command, except the fact that it works only on znodes with no children.
ZooKeeper
23 ZooKeeper has an official API binding for Java and C. The ZooKeeper community provides unofficial API for most of the languages (.NET, python, etc.). Using ZooKeeper API, an application can connect, interact, manipulate data, coordinate, and finally disconnect from a ZooKeeper ensemble.
ZooKeeper API has a rich set of features to get all the functionality of the ZooKeeper ensemble in a simple and safe manner. ZooKeeper API provides both synchronous and asynchronous methods.
ZooKeeper ensemble and ZooKeeper API completely complement each other in every aspect and it benefits the developers in a great way. Let us discuss Java binding in this chapter.
Basics of ZooKeeper API
Application interacting with ZooKeeper ensemble is referred as ZooKeeper Client or simply Client.
Znode is the core component of ZooKeeper ensemble and ZooKeeper API provides a small set of methods to manipulate all the details of znode with ZooKeeper ensemble.
A client should follow the steps given below to have a clear and clean interaction with ZooKeeper ensemble.
Connect to the ZooKeeper ensemble. ZooKeeper ensemble assign a Session ID for the client.
Send heartbeats to the server periodically. Otherwise, the ZooKeeper ensemble expires the Session ID and the client needs to reconnect.
Get / Set the znodes as long as a session ID is active.
Disconnect from the ZooKeeper ensemble, once all the tasks are completed. If the client is inactive for a prolonged time, then the ZooKeeper ensemble will automatically disconnect the client.
Java Binding
Let us understand the most important set of ZooKeeper API in this chapter. The central part of the ZooKeeper API is ZooKeeper class. It provides options to connect the ZooKeeper ensemble in its constructor and has the following methods:
connect – connect to the ZooKeeper ensemble
create – create a znode
exists – check whether a znode exists and its information
getData – get data from a particular znode
setData – set data in a particular znode