A pure TypeScript ZooKeeper client module for Node.js.
- node-zookeeper-client
- Requirements
- Usage
- API
- Client
- new Client(connectionString[, options])
- client.shell
- client.getSessionId()
- client.getSessionPassword()
- client.getSessionTimeout()
- client.connect()
- client.close()
- client.create(path[, data[, acl[, flags]]])
- client.create2(path[, data[, acl[, flags[, ttl]]]])
- client.delete(path[, version])
- client.setData(path, data[, version])
- client.getData(path[, watcher])
- client.setACL(path, acl[, version])
- client.getACL(path)
- client.exists(path[, watcher])
- client.getChildren(path[, watcher])
- client.getChildren2(path[, watcher])
- client.getAllChildrenNumber(path)
- client.getEphemerals(prefixPath)
- client.sync(path)
- client.transaction()
- client.getChildWatches()
- client.getDataWatches()
- client.getExistWatches()
- client.removeWatches(path, watcher, type)
- client.removeAllWatches(path, type)
- client.getConfig([watcher])
- Transaction
- Shell
- ZK.createClient(connectionString[, options])
- CreateMode
- OpCode
- Perms
- Xid
- WatcherType
- EventType
- EventState
- ExceptionCode
- ConnectionEvent
- Ids
- Client
- Jute
- License
- lodash
npm i @souche/node-zookeeper-clientconst ZK = require('@souche/node-zookeeper-client');
const client = ZK.createClient('127.0.0.1:2181');
(async () => {
console.log('start');
await client.connect();
if (!await client.exists('/test')) {
await client.create('/test');
}
await client.setData('/test', 'some data');
await client.create('/test/1');
console.log(await client.getChildren('/test'));
await client.delete('/test/1');
await client.close();
console.log('finish');
})().catch(err => {
console.log(err);
});const ZK = require('@souche/node-zookeeper-client');
const client = ZK.createClient('127.0.0.1:2181');
(async () => {
console.log('start');
await client.connect();
await client
.transaction()
.create('/test')
.setData('/test', 'some data')
.create('/test/1')
.create('/test/2')
.remove('/test/1')
.commit();
await client.close();
console.log('finish');
})().catch(err => {
console.log(err);
});- connectionString
string|Array<string> - options
object- authInfo
Array<{ scheme: string, auth: string | Buffer }>scheme:auth information - configNode
stringdefault: /zookeeper/config - connectTimeout
numbersocket connect timeout, default: 5000 - reconnectInterval
numberTime to wait after try all server failed, default: 1000 - retries
numberTimes to retry send packet to server, default: 3 - retryInterval
numberTime to wait before retry send, default: 0 - showFriendlyErrorStack
booleanShow friendly error stack, default:development ? true : false - logger
{ error: Function, info: Function, warn: Function, debug: Function } - PacketManager
PacketManager - WatcherManager
WatcherManager
- authInfo
- Returns: sessionId
buffer
The session id for this ZooKeeper client instance. The value returned is not valid until the client connects to a server and may change after a re-connect.
- Returns: sessionPassword
buffer
The session password for this ZooKeeper client instance. The value returned is not valid until the client connects to a server and may change after a re-connect.
- Returns: sessionTimeout
number
The negotiated session timeout for this ZooKeeper client instance. The value returned is not valid until the client connects to a server and may change after a re-connect.
- Returns:
Promise<void>
Start the client and try to connect to the ensemble.
- Returns:
Promise<void>
Close this client object. Once the client is closed, its session becomes invalid. All the ephemeral nodes in the ZooKeeper server associated with the session will be removed. The watches left on those nodes (and on their parents) will be triggered.
- path
stringthe path for the node - data
string|Bufferthe initial data for the node - acl
Array<Jute.data.ACL>the acl for the node, default:Ids.OPEN_ACL_UNSAFE - flags
numberspecifying whether the node to be created is ephemeral and/or sequential, default:CreateMode.PERSISTENT - Returns:
Promise<Jute.proto.CreateResponse>
Create a node with the given path. The node data will be the given data, and node acl will be the given acl.
- path
stringthe path for the node - data
string|Bufferthe initial data for the node - acl
Array<Jute.data.ACL>the acl for the node, default:Ids.OPEN_ACL_UNSAFE - flags
numberspecifying whether the node to be created is ephemeral and/or sequential, default:CreateMode.PERSISTENT - ttl
Bufferspecifying a TTL when mode is CreateMode.PERSISTENT_WITH_TTL or CreateMode.PERSISTENT_SEQUENTIAL_WITH_TTL - Returns:
Promise<Jute.proto.Create2Response>
Create a node with the given path and returns the Stat of that node. The node data will be the given data and node acl will be the given acl.
- path
stringthe path for the node - version
numberthe expected node version, default: -1 - Returns:
Promise<void>
Delete the node with the given path. The call will succeed if such a node exists, and the given version matches the node's version (if the given version is -1, it matches any node's versions).
- path
stringthe path for the node - data
string|Bufferthe data to set - version
numberthe expected node version, default: -1 - Returns:
Promise<Jute.proto.SetDataResponse>
Set the data for the node of the given path if such a node exists and the given version matches the version of the node (if the given version is -1, it matches any node's versions). Return the stat of the node.
- path
stringthe node path - watcher
(event: Jute.proto.WatcherEvent) => anyexplicit watcher - Returns:
Promise<Jute.proto.GetDataResponse>
Return the data and the stat of the node of the given path.
- path
stringthe given path for the node - acl
Array<Jute.data.ACL>the given acl for the node - version
numberthe given acl version of the node, default: -1 - Returns:
Promise<Jute.proto.SetACLResponse>
Set the ACL for the node of the given path if such a node exists and the given aclVersion matches the acl version of the node. Return the stat of the node.
- path
stringthe given path for the node - Returns:
Promise<Jute.proto.GetACLResponse>
Retrieve the ACL list and the stat of the node of the given path.
- path
stringthe node path - watcher
(event: Jute.proto.WatcherEvent) => anyexplicit watcher - Returns:
Promise<Jute.proto.ExistsResponse>
Return the stat of the node of the given path. Return null if no such a node exists.
- path
stringthe node path - watcher
(event: Jute.proto.WatcherEvent) => anyexplicit watcher - Returns:
Promise<Jute.proto.GetChildrenResponse>
Return the list of the children of the node of the given path.
- path
stringthe node path - watcher
(event: Jute.proto.WatcherEvent) => anyexplicit watcher - Returns:
Promise<Jute.proto.GetChildren2Response>
For the given znode path return the stat and children list.
- path
stringthe node path - Returns:
Promise<Jute.proto.GetAllChildrenNumberResponse>
Gets all numbers of children nodes under a specific path
- path
prefixPath - Returns:
Promise<Jute.proto.GetEphemeralsResponse>
Gets all the ephemeral nodes matching prefixPath created by this session. If prefixPath is "/" then it returns all ephemerals
- path
path - Returns:
Promise<Jute.proto.SyncResponse>
Flushes channel between process and leader.
- Returns:
Transaction
A Transaction is a thin wrapper on the multi method which provides a builder object that can be used to construct and commit an atomic set of operations.
- Returns:
Array<string>
- Returns:
Array<string>
- Returns:
Array<string>
- path
path - watcher
(event: Jute.proto.WatcherEvent) => anyexplicit watcher - type
numberthe type of watcher to be removed - Returns:
Promise<void>
For the given znode path, removes the specified watcher of given watcherType.
- path
path - type
numberthe type of watcher to be removed - Returns:
Promise<void>
For the given znode path, removes all the registered watchers of given watcherType.
- watcher
(event: Jute.proto.WatcherEvent) => anyexplicit watcher - Returns:
Promise<Jute.proto.GetDataResponse>
Return the last committed configuration (as known to the server to which the client is connected) and the stat of the configuration.
- path
stringthe path for the node - data
string|Bufferthe initial data for the node - acl
Array<Jute.data.ACL>the acl for the node, default:Ids.OPEN_ACL_UNSAFE - flags
numberspecifying whether the node to be created is ephemeral and/or sequential, default:CreateMode.PERSISTENT - Returns:
this
Add a create operation with given path, data, acls and mode.
- path
stringthe path for the node - version
numberthe expected node version, default: -1 - Returns:
this
Add a check (existence) operation with given path and optional version.
- path
stringthe path for the node - data
string|Bufferthe data to set - version
numberthe expected node version, default: -1 - Returns:
this
Add a set-data operation with the given path, data and optional version.
- path
stringthe path for the node - version
numberthe expected node version, default: -1 - Returns:
this
Add a delete operation with the given path and optional version.
- Returns:
Promise<Array<{ header: Jute.proto.MultiHeader, payload: object }>>
Execute the transaction atomically.
A high level api to use zookeeper with shell commands.
touch file.
mkdir -p dir.
cp file1 file2.
cp -r fileOrDir fileOrDir.
mv fileOrDir fileOrDir.
ls dir.
tree dir.
cat file.
rm fileOrDir.
rm -rf fileOrDir.
Jute is a serialization tool of Zookeeper.
Type mapping:
- ustring:
string - int:
number - long:
Buffer - buffer:
Buffer - vector:
Array - boolean:
boolean
See: https://github.com/apache/zookeeper/blob/master/zookeeper-jute/src/main/resources/zookeeper.jute
This module is licensed under MIT License