zkCli.sh
In the bin directory is ZooKeeper client zkCli.sh
./zkCli.sh -timeout 5000 -server 127.0.0.1:2181 ZooKeeper client and establish links
timeout: Timeout in milliseconds
r: read-only mode, when the node broken, service can also provide the read
Example:
./zkCli.sh -timeout 5000 -server 127.0.0.1:2181
1 zhangliuningdeMacBook-Pro:bin sherry$ ./zkCli.sh -timeout 5000 -server 127.0.0.1:2181 2 Connecting to 127.0.0.1:2181 3 2016-08-27 15:07:04,036 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.8--1, built on 02/06/2016 03:18 GMT 4 2016-08-27 15:07:04,039 [myid:] - INFO [main:Environment@100] - Client environment:host.name=192.168.0.105 5 2016-08-27 15:07:04,039 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_71 6 2016-08-27 15:07:04,041 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation 7 2016-08-27 15:07:04,041 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_71.jdk/Contents/Home/jre 8 2016-08-27 15:07:04,042 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/Users/sherry/Applications/zk1/bin/../build/classes:/Users/sherry/Applications/zk1/bin/../build/lib/*.jar:/Users/sherry/Applications/zk1/bin/../lib/slf4j-log4j12-1.6.1.jar:/Users/sherry/Applications/zk1/bin/../lib/slf4j-api-1.6.1.jar:/Users/sherry/Applications/zk1/bin/../lib/netty-3.7.0.Final.jar:/Users/sherry/Applications/zk1/bin/../lib/log4j-1.2.16.jar:/Users/sherry/Applications/zk1/bin/../lib/jline-0.9.94.jar:/Users/sherry/Applications/zk1/bin/../zookeeper-3.4.8.jar:/Users/sherry/Applications/zk1/bin/../src/java/lib/*.jar:/Users/sherry/Applications/zk1/bin/../conf: 9 2016-08-27 15:07:04,042 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/Users/sherry/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:. 10 2016-08-27 15:07:04,042 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/var/folders/_n/d05bph5x5bl6k5p0pkx2g66m0000gn/T/ 11 2016-08-27 15:07:04,042 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA> 12 2016-08-27 15:07:04,042 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Mac OS X 13 2016-08-27 15:07:04,042 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=x86_64 14 2016-08-27 15:07:04,042 [myid:] - INFO [main:Environment@100] - Client environment:os.version=10.11.5 15 2016-08-27 15:07:04,043 [myid:] - INFO [main:Environment@100] - Client environment:user.name=sherry 16 2016-08-27 15:07:04,043 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/Users/sherry 17 2016-08-27 15:07:04,043 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/Users/sherry/Applications/zk1/bin 18 2016-08-27 15:07:04,044 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=5000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@531d72ca 19 Welcome to ZooKeeper! 20 2016-08-27 15:07:04,081 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) 21 JLine support is enabled 22 2016-08-27 15:07:04,187 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@876] - Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session 23 [zk: 127.0.0.1:2181(CONNECTING) 0] 2016-08-27 15:07:04,245 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x156cacb918b0000, negotiated timeout = 5000 24 25 WATCHER:: 26 27 WatchedEvent state:SyncConnected type:None path:null
Above, and to express our ZooKeeper connection is established, you can interact with ZooKeeper the command
h: help list
h 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
Display command can be executed
Command and database similar classification, can be divided into four categories CRUD
ZooKeeper data structure:
Most of the time, our team ZooKeeper operations are in the nodes CRUD
Commonly used commands:
Queries related instructions
ls path: lists the files in the path
[zk: 127.0.0.1:2181(CONNECTED) 4] ls / [zookeeper]
Above: lists all files in the root directory node, is currently the only system that comes with a zookeeper node
stat path: View node status
[zk: 127.0.0.1:2181(CONNECTED) 6] stat /zookeeper cZxid = 0x0 ctime = Thu Jan 01 08:00:00 CST 1970 mZxid = 0x0 mtime = Thu Jan 01 08:00:00 CST 1970 pZxid = 0x0 cversion = -1 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 1
cZxid: creating a transaction when the node id
pZxid: the list of children was last modified transaction the above mentioned id
cversion: node version
dataCersion: data version number
aclVerson: acl permission version
......
GET path: acquiring content specified node
ls2 path: path node list child node and status information
creation instruction
create [-s] [-e] path data acl
1 [zk: 127.0.0.1:2181(CONNECTED) 10] create /node_1 123 2 Created /node_1
Above: node_1 created in the root node 123 carries the data
Use get / node_1 verify whether to add nodes and their data successfully
[Zk: 127.0.0.1:2181(CONNECTED) 15] Create -E / Node_1 / Node_1_1 234 Created / Node_1 / Node_1_1
It creates a temporary node (-e), and 234 carry data, using the stat command to see this new temporary node
[zk: 127.0.0.1:2181(CONNECTED) 16] stat /node_1/node_1_1 cZxid = 0x200000003 ctime = Sat Aug 27 15:27:30 CST 2016 mZxid = 0x200000003 mtime = Sat Aug 27 15:27:30 CST 2016 pZxid = 0x200000003 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x156cacb918b0000 dataLength = 3 numChildren = 0
ephemeralOwner value is not 0, the version number of the temporary node, a node if it is a permanent value of 0x0
1 [zk: 127.0.0.1:2181(CONNECTED) 1] create -s /node_1/node_1_1 234 2 Created /node_1/node_1_10000000001
By using the -s parameter to create a sequence of nodes, although we specified node name is node_1_1, but in fact, the name is node_1_10000000001, if we repeat the:
[zk: 127.0.0.1:2181(CONNECTED) 2] create -s /node_1/node_1_1 234 Created /node_1/node_1_10000000002
As results
This feature, we can use it to generate a primary key generator in a distributed environment
-s and -e can be used simultaneously
Exit:
quit
[zk: 127.0.0.1:2181(CONNECTED) 19] quit Quitting... 2016-08-27 15:29:44,760 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x156cacb918b0000 closed 2016-08-27 15:29:44,763 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x156cacb918b0000
If you log in again this time, execute ls / node_1, found that child node is empty, create a child node of node_1_1 missing, it is because it has been created before time specified, we created a temporary node. When the client session ends, the temporary node will be deleted.
To amend the relevant specified:
set path data [version]
[zk: 127.0.0.1:2181(CONNECTED) 14] set /node_1 998 cZxid = 0x200000002 ctime = Sat Aug 27 15:24:39 CST 2016 mZxid = 0x20000000a mtime = Sat Aug 27 15:38:40 CST 2016 pZxid = 0x200000008 cversion = 5 dataVersion = 2 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 3 numChildren = 3
If we modified several times, you will find dataVersion, that is, data version, changes have occurred in the non-stop (increment)
If we set the time manually to specify a version number, you must check out the results on a consistent, otherwise it will error.
This can be used to modify the node data in our time, to ensure that we modify the data has not been modified before others. Because if people revised, we will not be successful this amendment
Delete command:
delete path [version]
Removes the specified node data, the role of its version parameter specifies the agreed to set
delete / node_1 / node_1_10000000001
Full delete the entire node
Note: delete only delete nodes do not contain child nodes, if you want to delete a node contains child nodes, use the command rmr
rmr /node_1