A node CRUD
1.1 start the service and connectivity services
# 启动服务
bin/zkServer.sh start
#连接服务 不指定服务地址则默认连接到localhost:2181
zkCli.sh -server hadoop001:2181
1.2 help command
Use help
can view all commands and formats.
1.3 View node list
View a list of nodes there ls path
and ls2 path
two command, which is the former enhancements, not only can view all the nodes in the specified path, you can also view information on the current node.
[zk: localhost:2181(CONNECTED) 0] ls /
[cluster, controller_epoch, brokers, storm, zookeeper, admin, ...]
[zk: localhost:2181(CONNECTED) 1] ls2 /
[cluster, controller_epoch, brokers, storm, zookeeper, admin, ....]
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x130
cversion = 19
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 11
1.4 new node
create [-s] [-e] path data acl #其中-s 为有序节点,-e 临时节点
Create a node and write data:
create /hadoop 123456
Create an ordered node, node name specified at this time to create a node name + increment number:
[zk: localhost:2181(CONNECTED) 23] create -s /a "aaa"
Created /a0000000022
[zk: localhost:2181(CONNECTED) 24] create -s /b "bbb"
Created /b0000000023
[zk: localhost:2181(CONNECTED) 25] create -s /c "ccc"
Created /c0000000024
Create a temporary node, the node will be temporarily deleted after the session expires:
[zk: localhost:2181(CONNECTED) 26] create -e /tmp "tmp"
Created /tmp
1.5 View node
1. Obtain the node data
# 格式
get path [watch]
[zk: localhost:2181(CONNECTED) 31] get /hadoop
123456 #节点数据
cZxid = 0x14b
ctime = Fri May 24 17:03:06 CST 2019
mZxid = 0x14b
mtime = Fri May 24 17:03:06 CST 2019
pZxid = 0x14b
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
Each attribute node in the following table. One important concept is Zxid (ZooKeeper Transaction Id), every change ZooKeeper node has a unique Zxid, if Zxid1 less than Zxid2, the Zxid1 changes occur before Zxid2 change.
State property | Explanation |
---|---|
cZxid | Transaction ID when creating a data node |
ctime | Time of creating a data node |
mZxid | When the transaction ID data nodes last update |
mtime | The time when the last update of the data nodes |
pZxid | Child node of the node data when the last transaction ID is modified |
cversion | Changing the number of child nodes |
data version | Changing the number of node data |
aclVersion | Changing the number of nodes ACL |
ephemeralOwner | If the node is a temporary node, the created session SessionID the node; if the node is a persistent node, the attribute value is 0 |
dataLength | The length of the data content |
numChildren | The number of child nodes of the current node data |
2. Check node status
You can use stat
the command to view the status of a node, and its return value get
command but does not return the data node.
[zk: localhost:2181(CONNECTED) 32] stat /hadoop
cZxid = 0x14b
ctime = Fri May 24 17:03:06 CST 2019
mZxid = 0x14b
mtime = Fri May 24 17:03:06 CST 2019
pZxid = 0x14b
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
1.6 update node
Update command node set
, it can be modified directly, as follows:
[zk: localhost:2181(CONNECTED) 33] set /hadoop 345
cZxid = 0x14b
ctime = Fri May 24 17:03:06 CST 2019
mZxid = 0x14c
mtime = Fri May 24 17:13:05 CST 2019
pZxid = 0x14b
cversion = 0
dataVersion = 1 # 注意更改后此时版本号为 1,默认创建时为 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
You can make changes based on the version number, this time similar to the optimistic locking mechanism, when the data version number of your incoming data version number (dataVersion) and the current node does not comply, zookeeper will reject this revision:
[zk: localhost:2181(CONNECTED) 34] set /hadoop 678 0
version No is not valid : /hadoop #无效的版本号
1.7 Delete Node
Syntax delete a node is as follows:
delete path [version]
And update the same data node, can also pass the version number, the version number of your data when the incoming data version number (dataVersion) and the current node does not comply, zookeeper will not perform the removal.
[zk: localhost:2181(CONNECTED) 36] delete /hadoop 0
version No is not valid : /hadoop #无效的版本号
[zk: localhost:2181(CONNECTED) 37] delete /hadoop 1
[zk: localhost:2181(CONNECTED) 38]
To delete a node and all its descendant nodes, you can use a recursive delete command rmr path
.
Second, the listener
2.1 get path [watch]
The use of get path [watch]
registered listeners when the nodes can change the content, give notice to the client. Note that the zookeeper trigger a one-time (One-time trigger), is triggered after the first will immediately fail.
[zk: localhost:2181(CONNECTED) 4] get /hadoop watch
[zk: localhost:2181(CONNECTED) 5] set /hadoop 45678
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/hadoop #节点值改变
2.2 stat path [watch]
The use of stat path [watch]
registered listeners can occur when a change in the node status, notification to the client.
[zk: localhost:2181(CONNECTED) 7] stat /hadoop watch
[zk: localhost:2181(CONNECTED) 8] set /hadoop 112233
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/hadoop #节点值改变
2.3 ls\ls2 path [watch]
Use ls path [watch]
or ls2 path [watch]
registration of the listener can listen to all the nodes under child node increases, and delete operations.
[zk: localhost:2181(CONNECTED) 9] ls /hadoop watch
[]
[zk: localhost:2181(CONNECTED) 10] create /hadoop/yarn "aaa"
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/hadoop
Three, zookeeper four-character command
command | Functional Description |
---|---|
conf | For more information on service configuration print. |
cons | Complete connection to this server for all client / session details listed. Comprises a number of packets received / transmitted, a session ID, the operation delay, the last performed operation information. |
dump | Session and provisional list node unfinished. This only applies to Leader node. |
envi | For more information print service environment. |
Bib | Testing services are in the correct state. If it is correct then return "imok", otherwise it makes no corresponding. |
stat | Brief details of the server and the connecting client list. |
wchs | All watch lists of simple information. |
wchc | For more information watch the server listed by session. |
wchp | For more information watch the server listed by path. |
More four-character command can refer to the official document: https: //zookeeper.apache.org/doc/current/zookeeperAdmin.html
Before use requires yum install nc
installation nc command, using the example below:
[root@hadoop001 bin]# echo stat | nc localhost 2181
Zookeeper version: 3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03,
built on 06/29/2018 04:05 GMT
Clients:
/0:0:0:0:0:0:0:1:50584[1](queued=0,recved=371,sent=371)
/0:0:0:0:0:0:0:1:50656[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 0/0/19
Received: 372
Sent: 371
Connections: 2
Outstanding: 0
Zxid: 0x150
Mode: standalone
Node count: 167
More big data series can be found GitHub open source project : Big Data Getting Started