1. Common commands on the server side
After completing the pseudo-cluster deployment of zookeeper (the deployment tutorial refers to deploying the latest version of zookeeper pseudo-distributed cluster on linux ), you can execute these commands in the bin directory
- Start the ZK service:
bin/zkServer.sh start
- Check the ZK service status:
bin/zkServer.sh status
- Stop ZK service:
bin/zkServer.sh stop
- Restart the ZK service:
bin/zkServer.sh restart
- connect to the server:
zkCli.sh -server 127.0.0.1:2181
2. Common commands of the client
In Linux environment:
zkCli.sh -server 127.0.0.1:2181
After the connection is successful, the system will output the relevant environment and configuration information of ZooKeeper, as follows:
3. Common commands of the client
We can use the help command to view help:
3.1 Create Node
Using the create command, you can create a Zookeeper node, such as
create [-s] [-e] path data acl
Among them, -s or -e specify node characteristics, sequence or temporary node respectively, if not specified, it means persistent node; acl is used for permission control.
- Create a sequence node
#创建zk-test顺序节点
create -s /zk-test 123
You can see that a string of numbers is added after the created zk-test node to show the difference.
- Create a temporary node
#创建zk-temp临时节点
create -e /zk-temp 123
The temporary node will be automatically deleted after the client session ends. Use the quit command to exit the client.
Use the client to connect to the server again, and use the ls / command to view the nodes in the root directory
You can see that the zk-temp temporary node no longer exists in the root directory.
- Create permanent node
#创建zk-permanent永久节点
create /zk-permanent 123
3.2 Reading Nodes
The commands related to reading include the ls command and the get command. The ls command can list all the child nodes under the node specified by Zookeeper, and can only view all the child nodes of the first level under the specified node; the get command can obtain the data of the node specified by ZK. Data content and attribute information. Its usage is as follows:
ls [-s] [-w] [-R] path
get [-s] [-w] path
To get all the child nodes below the root node, use the ls command.
ls /
If you want to get the data content and attribute information of the root node, use the get -s command.
get -s /
You can also use the ls -s command to view node attribute information
ls -s /
You can see that the number of child nodes is 5.
To get the data content and properties of /zk-permanent, use the following commands:
get -s /zk-permanent
You can see that its data content is 123, and there are other attributes.
3.3 Update Node
Using the set command, you can update the data content of the specified node, the usage is as follows
set [-s] [-v version] path data
Among them, data is the new content to be updated, and version indicates the data version. For example, to update the data of the /zk-permanent node to 456, you can use the following command:
set /zk-permanent 456
Now dataVersion has become 1, indicating that an update has been made.
3.4 Delete Node
Use the delete command to delete the specified node on Zookeeper, the usage is as follows
delete [-v version] path
The version also represents the data version. Use the delete /zk-permanent command to delete the /zk-permanent node
delete /zk-permanent
As you can see, the /zk-permanent node has been successfully deleted. It is worth noting that if the deleted node has a child node, then the node cannot be deleted. The child node must be deleted first, and then the parent node must be deleted.