Operation commands for nodes in ZooKeeper (view, create, delete nodes)

When the sky is healthy, a gentleman strives to constantly strive for self-improvement; when the terrain is turbulent, a gentleman carries things with great virtue.

Everyone is lazy, but continuous learning is the foundation of a good life. Let’s encourage each other!

The articles are all for learning and organizing notes, mainly for sharing records. If there are any mistakes, please correct them and learn and progress together.

Article directory

  • 1. help command
  • 2. ls to view node information
    • 2.1 View root node information
    • 2.2 View detailed information of the root node
    • 2.3 View the name of the root node
    • 2.4 View all nodes and child nodes of the root node
  • 3. get gets node information
  • 4. createCreate node
  • 5. create -e creates a temporary node
  • 6. create -s creates sequentially numbered nodes
  • 7. delete delete node (empty node)
  • 8. deleteall deletes nodes (non-empty nodes)
  • 9. stat to view node status

References to ZooKeeper related articles:
Download, install, configure and use ZooKeeper
Detailed explanation of ZooKeeper configuration file zoo.cfg parameters

Start the server service and cli client
Run the following commands in the cli client window

1. help help command

Enter help to call out all related commands
You can find the specified command according to your needs, then enter the found command, and you can continue to view the use of the corresponding command.

For example, if you continue to enter ls for the ls command, you will see the detailed usage of the ls command.

2. ls to view node information

grammar:

ls [-s] [-w] [-R] path

-s means to view detailed information and can be omitted
-w means only looking at the node name, which can be omitted
-R means to view all nodes and sub-nodes of the root path, which can be omitted
path represents the node path, which can be the root node /, or a sub-node path such as /a, or /a/b/c

2.1 View root node information

ls/

2.2 View detailed information of the root node

ls -s /

The parameters are described in detail as follows:
[zookeeper]
Represents an array of child node names. If there are multiple arrays, they are separated by commas in square brackets.
Next is the status information of the node, also called the stat structure

cZxid = 0x0
Represents the zxid (ZooKeeper Transaction ID) of the transaction that created the znode
Transaction ID is a globally unique id assigned to each update operation/transaction operation of ZooKeeper, which represents zxid. The smaller the value, the earlier it is executed.
0x0 represents the hexadecimal number 0

ctime = Thu Jan 01 08:00:00 CST 1970
Indicates creation time

mZxid = 0x0
Represents the zxid of the last update

mtime = Thu Jan 01 08:00:00 CST 1970
Indicates the time of the last update

pZxid = 0x0
Represents the zxid of the last updated child node

cversion = -1
Indicates the change number of the child node, that is, the number of times the child node has been modified. -1 indicates that it has not been modified.

dataVersion = 0
Indicates the change number of the current node, 0 means it has not been modified.

aclVersion = 0
Indicates the change number of the access control list, access control list

ephemeralOwner = 0x0
Indicates that if a temporary node is used, it indicates the sessionId of the owner of the current node.

dataLength = 0
Indicates the data length

numChildren = 1
Represents the number of child nodes

2.3 View the name of the root node

ls -w /

2.4 View all nodes and sub-nodes of the root node

ls -R /

3. get gets node information

grammar:

get [-s] [-w] path

How to get root node information

get -s /

4. createCreate node

grammar:

create node name node content

Create a node. If the node content contains spaces, it needs to be enclosed in double quotes.
For example, create a node named a1 with the content a

create /a1 a

Then get the information of the child node a1 and see that the data length dataLength is the length of a 1

Create a child node a2 with the content aa bb cc and add double quotes "". If not, an error will be reported.

create /a2 "aa bb cc"

Use the get command to check the data length. dataLength is 8, which is the length of aa bb cc, including spaces.

5. create -e creates a temporary node

grammar:

create -e node name node content

Create a temporary node. When the connection is disconnected, the temporary node will be automatically deleted.

Create a temporary node a3 as follows with the content aaa

create -e /a3 aaa

Then view the list of all nodes

ls -s /


At this point, the child node a3 has been created.
Close the service and restart the client. Press the shortcut key Ctrl + C in the zkCli window to close the service. zkCli.cmd and press Enter to start.

View all node data of the root node again

ls -s /


The temporary node a3 is gone

6. create -s creates sequentially numbered nodes

grammar:

create -s node path node content

Create a sequentially numbered node, that is, a node with a serial number. The rule of the serial number is the order of the number of sibling nodes of the newly created node, starting from 0, and including temporary nodes and deleted nodes (that is, deleted The subsequent nodes will also be counted as sequence numbers)
For example, create a sequence node /a4 aaaa

create -s /a4 aaaa

At this time, you will see that the name of the new node changes to a40000000003

Then view all node data of the root node

ls -s /

zookeeper nodes are not included
Because the number of nodes at the same level is a1, a2, and a3 (temporary nodes, which are gone after restarting), a total of three nodes
The order is 012
Therefore, the serial number of a4 is a4 plus the assigned serial number 0000000003

The characteristics of sequentially numbered nodes are as follows:

  • The sequentially numbered node will follow the node name. The final name of the node is node name + serial number, such as /a40000000003
  • The sequence number is an incrementing counter
  • The sequence number is maintained by parent node, starting from the number of existing child nodes (including temporary nodes and deleted nodes)
  • If the child node is empty, it starts from 0000000000 and increments by 1.
  • In a distributed system, sequence numbers can be used to globally order all events, and clients can infer the order of events based on the sequence numbers.

7. delete delete node (empty node)

grammar:

delete node path

Note: delete deletes nodes only for empty nodes, non-empty nodes (which contain child nodes) cannot be deleted by delete
First create some nodes and sub-nodes

create /a1/b1 b


View all nodes under the root node

As shown in the figure, you can see nodes a1, a2, a40000000003, a5, a6 ,zookeeper
Among them, the a1 and a2 nodes contain child nodes.
Let’s first delete the a1 node containing child nodes

delete /a1

An error is reported. The node is not empty, that is, the non-empty node cannot be deleted using delete.

Now delete the node a5 that does not have child nodes (that is, empty nodes)

delete /a5

No error is reported, indicating that the deletion is successful. Then check all nodes to see if a5 is still there, or it is no longer there.

8. deleteall deletes nodes (non-empty nodes)

grammar:

deleteall node path

First check the child nodes in the non-empty node a1 to make sure it is a non-empty node containing child nodes.

ls -s /a1


As shown in the figure, a1 contains three child nodes: b1, b2, and b3, indicating that it is a non-empty node. node
Use deleteall to delete a1

deleteall /a1


Then check all the nodes under the root node and find that the a1 node is gone.

9. stat to view node status

Check the node status. This feels the same as the ls and get commands.

stat node path

like

stat /

Thanks for reading, may you become rich!