Zookeeper API - Apache ZooKeeper

What is Zookeeper API?

ZooKeeper has an executive API fastening for Java and C. The ZooKeeper community offers unofficial API for most of the languages (.NET, python, etc.). Using ZooKeeper API, an application can connect, interact, control data, coordinate, and finally disconnect from a ZooKeeper ensemble.

ZooKeeper API has a well-to-do set of features to get all the functionality of the ZooKeeper ensemble in an effortless and safe manner. ZooKeeper API offers both synchronous and asynchronous methods.
ZooKeeper ensemble and ZooKeeper API completely complement each other in every attribute and it reimbursements the developers in a great way. Let us discuss Java binding in this chapter.

Basics of ZooKeeper API

Application interacting with ZooKeeper ensemble is known as ZooKeeper Client or simply Client.
Znode is the center 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 ensembles assign a Session ID for the client.
  • Send heartbeats to the server sporadically. Otherwise, the ZooKeeper ensemble run outs of 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 realize the most important set of ZooKeeper API in this chapter. The central part of the ZooKeeper API is ZooKeeper class. It gives 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
  • getChildren − get all sub-nodes available in a particular znode
  • delete − get a particular znode and all its children
  • close − close a connection

Connect to the ZooKeeper Ensemble

The ZooKeeper class provides connection functionality through its constructor. The signature of the constructor is as follows −
Where,
  • connectionString − ZooKeeper ensemble host.
  • sessionTimeout − session timeout in milliseconds.
  • watcher − an object implementing “Watcher” interface. The ZooKeeper ensemble returns the connection status through the watcher object.
Let us generate a new helper class ZooKeeperConnection and add a method connect. The connect method creates a ZooKeeper object, connects to the ZooKeeper ensemble, and then returns the object.
Here CountDownLatch is used to stop (wait) the main process until the client connects with the ZooKeeper ensemble.
The ZooKeeper ensemble replies the connection status throughout the Watcher callback. The Watcher callback will be called once the client connects with the ZooKeeper ensemble and the Watcher callback calls the countDown process of the CountDownLatch to let go the lock, await in the main process.
Here is the complete code to connect with a ZooKeeper ensemble.

Coding: ZooKeeperConnection.java

Save the above code and it will be used in the next section for connecting the ZooKeeper ensemble.

Create a Znode

The ZooKeeper class provides create method to create a new znode in the ZooKeeper ensemble. The signature of the create method is as follows −
Where,
  • path − Znode path. For example, /myapp1, /myapp2, /myapp1/mydata1, myapp2/mydata1/myanothersubdata
  • data − data to store in a specified znode path
  • acl − access control list of the node to be created. ZooKeeper API provides a static interface ZooDefs.Ids to get some of basic acl list. For example, ZooDefs.Ids.OPEN_ACL_UNSAFE returns a list of acl for open znodes.
  • createMode − the type of node, either ephemeral, sequential, or both. This is an enum.
Let us create a new Java application to check the create functionality of the ZooKeeper API. Create a file ZKCreate.java. In the main method, create an object of type ZooKeeperConnection and call the connect method to connect to the ZooKeeper ensemble.
The connect method will return the ZooKeeper object zk. Now, call the create method of zk object with custom path and data.
The complete program code to create a znode is as follows −

Coding: ZKCreate.java

Once the submission is accumulated and executed, a znode with the specified data will be produced in the ZooKeeper ensemble. You can check it using the ZooKeeper CLI zkCli.sh.

Exists – Check the Existence of a Znode

The ZooKeeper class offers the exists method to check the existence of a znode. It profits the metadata of a znode, if the specified znode exists. The signature of the exists method is as follows −
Where,
  • path − Znode path
  • watcher − boolean value to specify whether to watch a specified znode or not
Let us create a new Java application to check the “exists” functionality of the ZooKeeper API. Create a file “ZKExists.java”. In the main method, create ZooKeeper object, “zk” using “ZooKeeperConnection” object. Then, call “exists” method of “zk” object with custom “path”. The complete listing is as follow −

Coding: ZKExists.java

Once the application is compiled and executed, you will get the below output.

getData Method

The ZooKeeper class provides getData method to get the data attached in a specified znode and its status. The signature of the getData method is as follows −
Where,
  • path − Znode path.
  • watcher − Callback function of type Watcher. The ZooKeeper ensemble will notify through the Watcher callback when the data of the specified znode changes. This is one-time notification.
  • stat − Returns the metadata of a znode.
Let us create a new Java application to understand the getData functionality of the ZooKeeper API. Create a file ZKGetData.java. In the main method, create a ZooKeeper object zk using he ZooKeeperConnection object. Then, call the getData method of zk object with custom path.
Here is the complete program code to get the data from a specified node −

Coding: ZKGetData.java

On one occasion the submission is compiled and executed, you will get the following output
And the application will wait for further notification from the ZooKeeper ensemble. Change the data of the specified znode using ZooKeeper CLI zkCli.sh.
Now, the application will print the following output and exit.

setData Method

The ZooKeeper class provides setData method to modify the data attached in a specified znode. The signature of the setData method is as follows −
Where,
  • path − Znode path
  • data − data to store in a specified znode path.
  • version − Current version of the znode. ZooKeeper updates the version number of the znode whenever the data gets changed.
Let us now create a new Java application to understand the setData functionality of the ZooKeeper API. Create a file ZKSetData.java. In the main method, create a ZooKeeper object zk using the ZooKeeperConnection object. Then, call the setData method of zk object with the specified path, new data, and version of the node.
Here is the complete program code to modify the data attached in a specified znode.

Code: ZKSetData.java

Once the application is compiled and executed, the data of the specified znode will be changed and it can be checked using the ZooKeeper CLI, zkCli.sh.

getChildren Method

The ZooKeeper class provides getChildren method to get all the sub-node of a particular znode. The signature of the getChildren method is as follows −
Where,
  • path − Znode path.
  • watcher − Callback function of type “Watcher”. The ZooKeeper ensemble will notify when the specified znode gets deleted or a child under the znode gets created / deleted. This is a one-time notification.

Coding: ZKGetChildren.java

Before running the program, let us create two sub-nodes for /MyFirstZnode using the ZooKeeper CLI, zkCli.sh.

Now, compiling and running the program will output the above created znodes.

Delete a Znode

The ZooKeeper class supplies delete method to delete a specified znode. The signature of the delete method is as follows −
Where,
  • path − Znode path.
  • version − Current version of the znode.

Let us create a new Java application to understand the delete functionality of the ZooKeeper API. Create a file ZKDelete.java. In the main method, create a ZooKeeper object zk using ZooKeeperConnection object. Then, call the delete method of zk object with the specified path and version of the node.

The complete program code to delete a znode is as follows −

Coding: ZKDelete.java

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Apache ZooKeeper Topics