zk-promise v1.0.1
Zookeeper Client Library
Overview
This library is designed to make it easier to work with zookeeper. It is promised based and make use of new async./await construct in nodejs.
Installation
Run this command to install locally:
npm i zk-promise --save
Tutorial
In a nodejs application, it can be imported/required like this:
const ZkClient = require('zk-promise').ZkClient;
Create Client
You need to create an instance of this client before interacting with zookeeper, as follows:
const ZkClient = require('zk-promise').ZkClient;
const zkClient = new ZkClient({
host: <zookeeper host ip>,
port: <zookeeper port>,
sessionTimeout: <session time out - default is 3 minutes>
});
How to Connect/Disconnect
Once you have the zookeeper client instance, you can use that to connect to zookeeper as follows:
await zkClient.connect();
and to disconnect, just close connection like this:
await zkClient.close();
How to Create znode
New node can be created by calling this:
const newNode = await zkClient.create('fully qualified path of the node', <json data or null>, <acls if any or null>, <CreateMode>);
where:
<CreateMode> could be:
- PERSISTENT
- PERSISTENT_SEQUENTIAL
- EPHEMERAL
- EPHEMERAL_SEQUENTIAL
NOTE:
Except for the parameter, all other parameters are optional. If no CreateMode is passed, it will default to PERSISTENT
The response will include the creatd node, and will something like this:
{
name: <name of the new node>,
parentNode: <path of the parent node>,
znode: <path of the new znode>
}
Remove znode
To remove a znode, use this function below:
await zkClient.remove('<target znode>');
or
await zkClient.removeWithRetries('<target znode>', <no of retries>);
where:
- <no of retries> is a positive integer
Check Existence of Node
Existence of node can be checked like this:
const exist = await zkClient.exists(<node>);
returns a boolean response.
Get Children Nodes
Get children nodes like this:
const children = await zkClient.getChildren('target path'); // such as '/' for root
Response will return an array of immediate children nodes.
Get Node Data
Use this below function to get data associated with a znode:
const nodeInfo = await zkClient.getData('<target znode>');
Response will take this form:
{
znode: <target znode>,
name: <target znode's name>,
data: <associated data>
}
Set Node Data
To set data on a znode, use one of the following methods:
const nodeInfo = await zkClient.setData('<target znode>', data);
Response will look like this:
{
znode: <target znode>,
name: <target znode's name>,
stat: <node stat>,
data: <associated data>
}
To safely set data, call this below method, that will first acquire lock and then set data:
const nodeInfo = await zkClient.safeSetData('<target znode>', data);
Response will be the same as above.
Acquire/Release Lock
Locks help synchronize activities in a distributed processing environment. You can wait for a lock before proceeding, like this:
const lock = await zkClient.acquire('<target znode>');
The response will return the znode that represents the lock. You will need this lock, in order to release lock. The lock will also go away, if the client holding lock dies. Because the lock is a sequential ephemeral node.
await zkClient.release('<lock znode>');