In the Zookeeper installation and directory structure , in the end, in order to be able to generate related log files and snapshot files, we performed some operations to create / modify nodes. Here we will introduce some details. First, we need to start the ZK server. This It has been introduced before, and then we need to use the client to connect, as follows:
After the execution, we have successfully connected. Here we can perform the operation of creating / modifying / deleting nodes. Here we first introduce the node model and type of Zookeeper.
First of all, the view structure of ZooKeeper is similar to the standard Unix file system. Each node is called a "data node" or ZNode. Each znode can store data and can also mount child nodes, so it can be called a "tree". This The tree structure, we can clearly see through Zookeeper's graphical client tool ZooInspector introduced earlier. The second point to note is thatEach znode must have a value, if there is no value, the node cannot be created successfully。
In Zookeeper, znode is a node with a similar path to the Unix file system. You can store or obtain data from this node. In addition, you can add, delete, modify, and check znode through the client. You can also register a watcher to monitor changes in znode.
Znode has four forms of directory nodes (default is persistent). For persistent nodes and temporary nodes, under the same znode, the node name is unique.
- Persistent node (persistent):, the
create /node1 value1
client disconnects zk will not delete the persistent type node - Ephemeral: When the
create -e /node2 value2
client disconnects, zk will delete the ephemeral type node.
In addition, the above-mentioned persistent nodes and temporary nodes can be combined with sequential nodes, which are divided into persistent sequential nodes and temporary sequential nodes. ,as follows: - Sequential node:
create -s /node3 value3
Set the sequence identifier when creating a znode, a value will be appended to the znode name, and the sequence number is a monotonically increasing counter, maintained by the parent node
Next, let's create the different types of nodes described above in sequence. First, let's create a persistent node as follows:
Once created, through which we can get /node1
view information node just created, as follows:
You can also see the value set in the node when it was created, and there are many other information, the meaning is as follows:
Attributes | data structure | description |
---|---|---|
cZxid | long | The Zxid value of the node being created |
ctime | long | When the node was created |
mZxid | long | The zXid value of the node being modified |
mtime | long | The time the node was last modified |
pZxid | long | The transaction ID when the child node was last modified |
cversion | long | The modified version number of the node's own child nodes |
data version | long | The modified version number of the node |
aclVersion | long | The revision number of the node's ACL |
ephemeralOwner | long | If this node is a temporary node, then its value is the session ID of the owner of this node; otherwise, its value is 0 |
dataLength | int | The length of the node data field |
numChildren | int | Number of child nodes owned by the node |
We may be the result on the table information see we have just created a lasting node, and then we can set /node1 /value11
modify the value node1 node, and then view its node information, initiating where nodes modified, the modified node version number will change
If we want to create a child node under a node, it is also very simple, as follows:
In addition, we can ls
query information to all nodes, as follows:
You can also ls2
view its details, and get information similar to that shown
In addition, we can clearly view the value under each node through Zookeeper's graphical client tool ZooInspector
So what do we want to delete a node? able to passdelete /node1
But after we send prompt us to run the node is not empty, so if there is a child node to the next node, then, delete command can not remove it, we can first child node delete, and then delete the node, or by rmr /node1
directly deleted.
When we completed the operation, you can quit
command to exit the client, if the problem can help
get tips
In the above, we used the persistent node as an example to introduce commonly used operation commands, so what if we want to create a temporary node? Is also very simple, just add -e
to, and we can see ephemeralOwner temporary node is not 0
This node When we use quit
the command quit, again reconnect, you will find temporary node has been deleted
In addition, we said before that whether it is a persistent node or a temporary node, its node name is unique. If we create a node with the same name, it will definitely not be created.
But we can create a sequence node, because creating a sequence node, a value will be appended to the znode name, the sequence number is a monotonically increasing counter, maintained by the parent node
In addition, there are some common four-character commands in Zookeeper, which support the interaction of certain four-character command letters with them to obtain the current status and related information of the ZooKeeper service. You can submit the corresponding commands to ZooKeeper through telnet or nc. When you use it, you need to install nc.
-
echo stat | nc 127.0.0.1 2181
to see which node is selected as the follower or leader -
echo ruok | nc 127.0.0.1 2181
Test whether the server is started, if the reply is imok, it has been started -
echo dump | nc 127.0.0.1 2181
lists unprocessed sessions and temporary nodes -
echo kill | nc 127.0.0.1 2181
turn off the server -
echo conf | nc 127.0.0.1 2181
output detailed information about service configuration -
echo cons | nc 127.0.0.1 2181
lists the complete connection / session details of all clients connected to the server -
echo envi | nc 127.0.0.1 2181
output detailed information about the service environment (different from the conf command) -
echo reqs | nc 127.0.0.1 2181
lists unprocessed requests -
echo wchs | nc 127.0.0.1 2181
lists the details of the server watch -
echo wchc | nc 127.0.0.1 2181
lists the details of the server watch by session, and its output is a list of watch-related sessions -
echo wchp | nc 127.0.0.1 2181
lists the details of the server watch by path. It outputs a path related to the session