ZooKeeper provides a very simple command-line client, zkCli, under the bin directory of the ZooKeeper installation directory.
[root@s1 zk]# ls /usr/local/zookeeper/bin/ zkCleanup.sh zkCli.sh zkEnv.sh zkServer.sh
First connect to a ZooKeeper instance that has been started. For example:
zkCli.sh -server localhost:2181
A lot of information will be output during the connection. When the connection is successful, it will enter ZooKeeper's interactive mode:
WatchedEvent state:SyncConnected type:None path:null [zk: localhost:2181(CONNECTED) 0] # Enter the commands allowed by ZooKeeper here
For example, input help and output help information.
[zk: localhost:2181(CONNECTED) 0] help ZooKeeper -server host:port cmd args stat path [watch] set path data [version] ls path [watch] delquota [-n|-b] path ls2 path [watch] setAcl path acl setquota -n|-b val path history redo cmdno printwatches on|off delete path [version] sync path listquota path rmr path get path [watch] create [-s] [-e] path data acl addauth scheme auth quit getAcl path close connect host:port [zk: localhost:2181(CONNECTED) 1]
There is an increasing number in the interactive command prompt, for example, when the above operation is performed sequentially, it changes from 0 to 1. This number represents the historical number of the command.
You can also use the non-interactive mode of zkCli.sh to perform one-time operations in the following format:
zkCli.sh -server IP:PORT COMMAND
For example, the help command:
zkCli.sh -server localhost:2181 help
ZooKeeper's commands are rarely simple and can be roughly divided into several types:
-
create: create a node somewhere in the tree.
-
delete: delete a node.
-
Exists: Test whether a node exists.
-
get data: Read node data.
-
set data: Write data to a node.
-
get children: Retrieve a list of child nodes of a node.
- sync: Waiting for data to be propagated.
Note that this is only the functional type of commands, and does not mean that they are commands.
For example, create a znode node:
[zk: localhost:2181(CONNECTED) 1] create /zk_test mydata1 Created /zk_test
Where / zk_test is the node name and mydata1 is the data carried by the node.
Create a child node:
[zk: localhost:2181(CONNECTED) 2] create /zk_test/child_node1 "mydata2" Created /zk_test/child_node1
View the child nodes under a node. Note that there is no recursive retrieval.
[zk: localhost:2181(CONNECTED) 3] ls / [zookeeper, zk_test] [zk: localhost:2181(CONNECTED) 4] ls /zk_test [child_node1]
Get node data, status information:
[zk: localhost:2181(CONNECTED) 6] get /zk_test mydata1 cZxid = 0x4 ctime = Wed Jun 27 02:05:44 CST 2018 mZxid = 0x4 mtime = Wed Jun 27 02:05:44 CST 2018 pZxid = 0x5 cversion = 1 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 7 numChildren = 1 [zk: localhost:2181(CONNECTED) 7] get /zk_test/child_node1 mydata2 cZxid = 0x5 ctime = Wed Jun 27 02:29:12 CST 2018 mZxid = 0x5 mtime = Wed Jun 27 02:29:12 CST 2018 pZxid = 0x5 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 7 numChildren = 0
If you just want to get the status information of the node, you can use the stat command:
[zk: localhost:2181(CONNECTED) 8] stat /zk_test cZxid = 0x4 ctime = Wed Jun 27 02:05:44 CST 2018 mZxid = 0x4 mtime = Wed Jun 27 02:05:44 CST 2018 pZxid = 0x5 cversion = 1 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 7 numChildren = 1
The ls2 command can obtain the child nodes under a given node and the state information of the given node:
[zk: localhost:2181(CONNECTED) 12] ls2 / [zookeeper, zk_test] # List of child nodes cZxid = 0x0 ctime = Thu Jan 01 08:00:00 CST 1970 mZxid = 0x0 mtime = Thu Jan 01 08:00:00 CST 1970 pZxid = 0x4 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 2 # Number of child nodes
The set command is used to modify the node:
[zk: localhost:2181(CONNECTED) 13] set /zk_test "mydata2" cZxid = 0x4 ctime = Wed Jun 27 02:05:44 CST 2018 mZxid = 0x9 mtime = Wed Jun 27 02:42:45 CST 2018 pZxid = 0x7 cversion = 2 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 7 numChildren = 0
Delete can delete nodes. Note that it will not be deleted recursively, so to delete a node containing a child node, you need to delete all the child nodes first.
[zk: localhost:2181(CONNECTED) 21] delete /zk_test Node not empty: /zk_test [zk: localhost:2181(CONNECTED) 22] delete /zk_test/child_node1
The history command lists 10 command histories of recent operations and gives the number of each historical command. The redo commands can be re-invoked based on the number of historical commands.
[zk: localhost:2181(CONNECTED) 18] history 8 - stat /zk_test 9 - get / 10 - get child / 11 - get children / 12 - ls2 / 13 - ls / 14 - ls2 /zk_test 15 - get / 16 - help 17 - ls /zk 18 - history
Re-execute the historical order No. 13:
[zk: localhost:2181(CONNECTED) 27] redo 13 [zookeeper, zk_test]
quit command exits ZooKeeper's interactive command line.
[zk: localhost:2181(CONNECTED) 29] quit Quitting...