This is the 10th day of my participation in the August More Text Challenge. For details, see:August is more challenging

download

We can go to the ZooKeeper official website to download, here I downloaded the ZooKeeper version 3.7.0

After decompressing the files, go to the apache-zookeeper-3.7.0-bin/bin directory, which contains the client, server, and some tools. On Windows, you can run the. CMD file. On Mac or Linux, you can run the.sh file. The zkCli file is the ZooKeeper client. You can use this client to connect to the ZooKeeper registry to view, add or delete nodes.

use

I’m demonstrating this on a Mac environment

Here’s how to use this client.

The connection

The first step is to connect to the ZooKeeper service. When you run zkcli. sh without any arguments, it defaults to connect to the local localhost:2181 address. If you want to connect to a specified address, you can use the -server configuration:

./zkCli.sh -server localhost:2181

# You can also add a connection timeout (in milliseconds)
./zkCli.sh -timeout 3000 -server localhost:2181
Copy the code

When connected, the terminal will look like this:

[zk: localhost:2181(CONNECTED) 0] 
Copy the code

You can then type help to see the command executed:

ZooKeeper -server host:port -client-configuration properties-file cmd args
	addWatch [-m mode] path # optional mode is one of [PERSISTENT, PERSISTENT_RECURSIVE] - default is PERSISTENT_RECURSIVE
	addauth scheme auth
	close
	config [-c] [-w] [-s]
	connect host:port
	create [-s] [-e] [-c] [-t ttl] path [data] [acl]
	delete [-v version] path
	deleteall path [-b batch size]
	delquota [-n|-b|-N|-B] path
	get [-s] [-w] path
	getAcl [-s] path
	getAllChildrenNumber path
	getEphemerals path
	history
	listquota path
	ls [-s] [-w] [-R] path
	printwatches on|off
	quit
	reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*]
	redo cmdno
	removewatches path [-c|-d|-a] [-l]
	set [-s] [-v version] path data
	setAcl [-s] [-v version] [-R] path acl
	setquota -n|-b|-N|-B val path
	stat [-w] path
	sync path
	version
	whoami
Copy the code

In the client, you can also use the connect command to switch the ZooKeeper service address:

[zk: localhost:2181(CONNECTED) 12] connect localhost:2181
Copy the code

Create a node

Permanent node

You can use the create command to create a permanent node:

[zk: localhost:2181(CONNECTED) 18] create /node
Created /node
Copy the code

Temporary node

Temporary node When the client disconnects, the node is deleted

[zk: localhost:2181(CONNECTED) 19] create -e /tmp_node
Created /tmp_node
Copy the code

Ephemerals cannot have children; Ephemerals cannot have children is reported when trying to create one: Ephemerals cannot have children

[zk: localhost:2181(CONNECTED) 20] create -e /tmp_node/t1
Ephemerals cannot have children: /tmp_node/t1
Copy the code

Order node

ZooKeeper can create incrementing sequential nodes for us

[zk: localhost:2181(CONNECTED) 23] create -s /seq_node
Created /seq_node0000000017
[zk: localhost:2181(CONNECTED) 24] create -s /seq_node
Created /seq_node0000000018
[zk: localhost:2181(CONNECTED) 25] create -s /seq_node
Created /seq_node0000000019
Copy the code

Container node

A container node is deleted when all of its children are deleted

# Create a container node
[zk: localhost:2181(CONNECTED) 39] create -c /container
Created /container

# Create a child node of the container node
[zk: localhost:2181(CONNECTED) 43] create /container/c1
Created /container/c1
[zk: localhost:2181(CONNECTED) 44] create /container/c2
Created /container/c2

Remove the child nodes of the container node
[zk: localhost:2181(CONNECTED) 46] delete /container/c1
[zk: localhost:2181(CONNECTED) 47] delete /container/c2

After a while, the container node is removed
[zk: localhost:2181(CONNECTED) 56] get /container
org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /container
Copy the code

TTL node

ZooKeeper can also set the validity period for a node in milliseconds. To create this node, need a ZooKeeper server at startup Settings to ZooKeeper. ExtendedTypesEnabled = true, otherwise will fail: when creating KeeperErrorCode = Unimplemented

The unit of expiration time set at creation time is milliseconds:

[zk: localhost:2181(CONNECTED) 57] create -t 3000 /ttl_node
Created /ttl_node
Copy the code

Remove nodes

In zkCli, you can use the delete command to delete a node. When deleting a node, ensure that there are no child nodes under the node

[zk: localhost:2181(CONNECTED) 6] create /node1
Created /node1
[zk: localhost:2181(CONNECTED) 7] delete /node1

# Error when deleting a child node
[zk: localhost:2181(CONNECTED) 10] create /node1
Created /node1
[zk: localhost:2181(CONNECTED) 11] create /node1/n1
Created /node1/n1
[zk: localhost:2181(CONNECTED) 12] delete /node1
Node not empty: /node1
Copy the code

If you want to deletea node including all of its children at once, you can use the deleteall command to do so

[zk: localhost:2181(CONNECTED) 14] deleteall /node1
Copy the code

Look at the node

View all child nodes

We can use the ls command to view all the children of a node. For example, we can use the ls command to view all the children of a node.

[zk: localhost:2181(CONNECTED) 20] ls /
[quota, zookeeper]
Copy the code

View the child nodes of the ZooKeeper node

[zk: localhost:2181(CONNECTED) 21] ls /zookeeper
[config, quota]
Copy the code

Check the node status

You can use stat to check the status of a node, such as the root node:

[zk: localhost:2181(CONNECTED) 22] stat/ cZxid = 0x0 ctime = Thu Jan 01 08:00:00 CST 1970 mZxid = 0x0 mtime = Thu Jan 01 08:00:00 CST 1970 pZxid = 0xe cversion  = 4 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 2Copy the code

Field description:

field describe
czxid Create znode zxID
mzxid Change the zxID of znode last time (create, delete, set direct child node, set itself node count)
pzxid When the zxID of a child node is modified last time, it will be counted when a child node is created or deleted, but not when a child node is set.
ctime Time to create a Znode, in milliseconds
mtime Time when the znode was last modified, in milliseconds
dataVersion Number of times zNode data has been modified
cversion The number of times a child node has been modified (the number of times a set child node has been modified)
aclVersion Number of ACL changes on the Znode
ephemeralOwner Session ID of a temporary Znode. If it is not a temporary znode, the value is 0
dataLength Length of data carried by the ZNode, in bytes
numChildren Number of immediate children (grandchildren are not recursively calculated)

View node data

You can use the set command to set the node data, and when you want to get the set data, you can use the get command to get the node data

[zk: localhost:2181(CONNECTED) 27] create /node
Created /node
[zk: localhost:2181(CONNECTED) 28] set[zk: localhost:2181(CONNECTED) 29] Get /node My dataCopy the code

Terminal Visual client

Download at github.com/greycodee/z…

Github:github.com/greycodee/z…