Backend Development 14 min read

Zookeeper Command Reference and Node Management Guide

This article provides a comprehensive tutorial on Zookeeper 3.5.10, covering client and server commands, node attributes, creation, querying, updating, deletion, listing children, checking node status, watcher usage, and detailed access control mechanisms with practical command examples.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Zookeeper Command Reference and Node Management Guide

Table of Contents

Client and Server Commands

Node Attributes

help Command

Create and Query Nodes

Update Nodes

Delete Nodes

List Child Nodes

Check Node Status

Watchers

Permission Control

Client and Server Commands

Switch to the bin directory of the Zookeeper installation and use the following commands to start, stop, restart, or check the status of the Zookeeper service, as well as to connect a client:

./zkServer.sh start
./zkServer.sh status
./zkServer.sh stop
./zkServer.sh restart
./zkCli.sh   # or ./zkCli.sh -server 127.0.0.1:2181

Node Attributes

Before using Zookeeper commands, understand the meaning of node attributes such as cZxid , ctime , mZxid , mtime , pZxid , cversion , dataVersion , aclVersion , ephemeralOwner , dataLength , and numChildren . These describe transaction IDs, creation/modification times, version counters, ACL changes, and data size.

help Command

Use the help command to view Zookeeper's basic commands and syntax for the current version.

ZooKeeper -server host:port cmd args
addauth scheme auth
close
config [-c] [-w] [-s]
connect host:port
create [-s] [-e] [-c] [-t ttl] path [data] [acl]
delete [-v version] path
... (other commands omitted for brevity)

Create and Query Nodes

To create a node, use create [-s] [-e] path data where -s creates a sequential node and -e creates an ephemeral node. To query a node, use get [-s] [-w] path with -s showing all info and -w showing only data.

// Create a persistent node
[zk: localhost:2181(CONNECTED) 0] create /node1 "123"
Created /node1
// Query node attributes
[zk: localhost:2181(CONNECTED) 1] get -s /node1
123
cZxid = 0x43
ctime = Wed Jul 29 21:27:31 CST 2020
... (other attributes)

Update Nodes

Modify node data with set [-s] [-v version] path data . The optional -v implements optimistic locking.

[zk: localhost:2181(CONNECTED) 13] set /node1 "456"
[zk: localhost:2181(CONNECTED) 14] get -w /node1
456
[zk: localhost:2181(CONNECTED) 15] set -v 0 /node1 "234"
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/node1

Delete Nodes

Delete a node with delete [-v version] path . To delete a node that has children, either delete children first or use deleteall path .

[zk: localhost:2181(CONNECTED) 0] delete /node1
... (error if node has children)
[zk: localhost:2181(CONNECTED) 8] deleteall /node1

List Child Nodes

List children using ls [-s] [-w] [-R] path or ls2 path [watch] .

[zk: localhost:2181(CONNECTED) 19] ls /
[a0000000001, b0000000002, c, hadoop, seqNode10000000011, zookeeper]
[zk: localhost:2181(CONNECTED) 22] ls /node1
[]
[zk: localhost:2181(CONNECTED) 24] ls /node1
[node11]

Check Node Status

Use stat path to view node status without data content.

[zk: localhost:2181(CONNECTED) 25] stat /node1
cZxid = 0x55
ctime = Wed Jul 29 22:05:16 CST 2020
... (other status fields)

Watchers

The deprecated get path [watch] has been replaced by get [-s] [-w] path . Watchers trigger once when node data changes.

// Register a watcher
[zk: localhost:2181(CONNECTED) 29] get -w /node1
node1
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/node1

Permission Control

Zookeeper uses ACLs to control access. An ACL entry follows scheme:id:permission where permission can be c (create), d (delete), r (read), w (write), a (admin). Supported schemes include world , ip , auth , and digest .

// World scheme example
[zk: localhost:2181(CONNECTED) 31] getAcl /node1
'world,'anyone: cdrwa
[zk: localhost:2181(CONNECTED) 32] setAcl /node1 world:anyone:drwa

// IP scheme example
[zk: localhost:2181(CONNECTED) 8] create /ipNode "ipNode"
[zk: localhost:2181(CONNECTED) 10] setAcl /ipNode ip:192.168.103.133:cdrwa,ip:192.168.103.132:cdrwa

// Auth scheme example
[zk: localhost:2181(CONNECTED) 36] addauth digest qxy:123456
[zk: localhost:2181(CONNECTED) 38] setAcl /node1 auth:qxy:cdrwa

// Digest scheme example (password hashed with SHA1+BASE64)
[zk: localhost:2181(CONNECTED) 5] create /digestNode "digestNode"
[zk: localhost:2181(CONNECTED) 2] setAcl /digestNode digest:qxy:hDF4uLZvMJqOX2ekKFa6kSz9HNo=:cdrwa
[zk: localhost:2181(CONNECTED) 6] addauth digest qxy:123456
[zk: localhost:2181(CONNECTED) 7] get /digestNode

Source: blog.csdn.net/xuan_lu/article/details/107675047

distributed systemscliZookeeperaccess controlNode Management
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.