Zookeeper API – Java Binding & C Binding
Stay updated with the latest technology trends while you're on the move - Join DataFlair's Telegram Channel
1. Zookeeper API
Today, we will see Apache ZooKeeper API. Moreover, in this ZooKeeper API tutorial, we will learn ZooKeeper Java API and C API. Also, we will discuss ZooKeper Java API example. There is an official API binding for Java and C, in Zookeeper. However, for most of the languages (.NET, Python, etc.) ZooKeeper community provides unofficial API.
So, let’s start the ZooKeeper API tutorial.
2. What is Zookeeper API?
There is an official API binding for Java and C, in Zookeeper. However, for most of the languages (.NET, Python, etc.) ZooKeeper community provides unofficial API. Basically, an application can connect, interact, manipulate data, coordinate, and finally disconnect from a ZooKeeper ensemble, by using ZooKeeper API.
In addition, we can use all the functionality of the ZooKeeper ensemble in a simple and safe manner, because of the rich set of features of ZooKeeper API. Moreover, these APIs offers both synchronous as well as asynchronous methods.
Do you know about ZooKeeper CLI
So, we ZooKeeper API and ZooKeeper ensemble completely complement each other in every aspect. Also, it benefits the developers in a great way.
If these professionals can make a switch to Big Data, so can you:
3. Bindings in ZooKeeper API
In two languages, Java and C, the ZooKeeper client libraries come. So, let’s understand both in detail:
a. ZooKeeper Java API
ZooKeeper Java binding is made up of two packages, such as org.apache.zookeeper and org.apache.zookeeper.data. Apart from that packages which make up ZooKeeper are used internally or are part of the server implementation. Moreover, the generated classes which are used simply as containers do make the org.apache.zookeeper.data package.
In addition, the ZooKeeper class is the main class ZooKeeper Java client uses. Only by an optional session id and password, its two constructors differ. And, across instances of a process, ZooKeeper supports session recovery.
Let’s discuss ZooKeeper Leader Election
Though, two threads are created, when a ZooKeeper object is created, they are an IO thread and an event thread. By using Java NIO, all IO happens on the IO thread. And on the event thread, all event callbacks happen. Furthermore, Session maintenance also happens on IO thread. It includes reconnecting to ZooKeeper servers as well as maintaining the heartbeat.
Also, the responses for the synchronous methods process in the IO thread and for asynchronous methods and watch events on the event thread.
Things to remember in ZooKeeper Java Binding
Now few points to notice that result from this design:
- For asynchronous calls and watcher callbacks, all completions will be made in order, but one at a time. However, there are no boundaries on the caller that it can perform any processing they wish, but they have to make sure that during that time no other callbacks will be processed.
- The processing of the IO thread or the processing of the synchronous calls is not blocked by Callbacks.
- And, the Synchronous calls may not get back in the correct sequence.
Ultimately, there are several rules associated with shutdown are straightforward, like the ZooKeeper object becomes invalid, as soon as a ZooKeeper object is closed or receives a fatal event (SESSION_EXPIRED and AUTH_FAILED).
b. C Binding
Basically, the C binding has a multi-threaded library and a single-threaded library. On defining a multi-threaded library, it is the easiest one to use and also it is very same as Java API. However, for handling connection maintenance and callbacks, this library will create an IO thread and an event dispatch thread. Whereas, we use the single-threaded library in event-driven applications especially by exposing the event loop which is used in the multithreaded library.
Have a look at ZooKeeper Queues
There are two shared libraries in this package; zookeeper_st and zookeeper_mt. Although, for integrating into the application’s event loop, the former only offers the asynchronous APIs as well as callbacks. Basically, it supports the platforms were a pthread library is not available or is unstable (i.e. FreeBSD 4.x). However, application developers should link with zookeeper_mt, in all other cases, because it involves support for both Sync as well as Async API.
i. Installation of ZooKeeper C Binding
We need to follow the below steps if we are building the client from a check-out from the Apache repository. Although, we need to skip to step 3 if we are building from a project source package which we download from apache.
- At very first, Run ant compile_jute from the ZooKeeper top-level directory (…/trunk). So, it will create a directory of the name “generated” under …/trunk/src/c.
- After that, change directory to the…/trunk/src/c and run autoreconf -if to bootstrap autoconf, automake, and libtool. Also, make sure we have autoconf version 2.59 or greater installed. Skip to step 4.
- Further, unzip/untar the source tarball and cd to the zookeeper-x.x.x/src/c directory, if we are building from a project source package.
- Now, to generate the makefile, run ./configure <our-options>. Here are some of the options the configure utility supports that can be useful in this step:
This option enables optimization, as well as enables, debug info compiler options. (Disabled by default.)
However, this option disables Sync API support; zookeeper_mt library won’t built. (Enabled by default.)
It does not build static libraries. (Enabled by default.)
And, this one does not build shared libraries. (Enabled by default.)
- Also, to build the libraries and install them, run make or make install.
- Furthermore, run make doxygen-doc, to generate doxygen documentation for the ZooKeeper API. However, in a new subfolder of name docs, all documentation are available. However, this command only generates HTML, by default.
ii. Using the C Client
By running a ZooKeeper server and connecting to it using one of the client applications, we can test our client that was built as part of the installation procedure. We could also use cli_st (singlethreaded, built against zookeeper_st library), but here we are using cli_mt (multithreaded, built against zookeeper_mt library) in this example:
$ cli_mt zookeeper_host:9876
Now, for executing simple ZooKeeper commands, this is a client application which gives us a shell. Hence, it displays a shell prompt, once successfully started and connected to the server. In this way, we can enter ZooKeeper commands.
For example, in order to create a node:
> create /my_new_node
Now, to verify that the node’s been created:
> ls /
Here, we must see a list of node those are children of the root node “/”.
However, we have to remember few things in order to be able to use the ZooKeeper API in our application:
- Also, include ZooKeeper header: #include <zookeeper/zookeeper.h>
- To enable the multi-threaded version of the library, compile with -DTHREADED compiler flag, if we are building a multithreaded client. Afterward, link against the zookeeper_mt library. However, do not compile with -DTHREADED, if we are building a single-threaded client, still ensure to link against the zookeeper_st library.
Let’s explore ZooKeeper ZNodes
So, this was all in ZooKeeper API tutorial. Hope you like our explanation
Hence, in this Apache ZooKeeper API tutorial, we have seen the whole concept of Zookeeper API (Java and C) in detail. Moreover, we discussed the ZooKeeper Java API example. Still, if you have any questions to ask regarding Zookeeper API, feel free to ask. Keep visiting Data Flair, keep learning!