Original link http://coolxing.iteye.com/blog/1871328
The zookeeper data structure is very similar to the normal file system. See:
Picture references from Developerworks
Each node in the diagram is called a znode. Each znode consists of 3 parts:
- Stat. This is the status information that describes the Znode version, permissions, and so on.
- Data. The data associated with the Znode.
- Children. The child node under the Znode.
Zookeeper command
Before delving into the various parts of Znode, you need to familiarize yourself with some common zookeeper commands first.
Connect to Server
Bash code
- Bin/zkcli.sh-server 10.1.39.43:4180
Lists the child node of the specified node
Bash code
- [Zk:10.1.39.43:4180 (CONNECTED) 9] LS/
- [Hello, Filesync, zookeeper, Xing, server, group, log]
- [Zk:10.1.39.43:4180 (CONNECTED)] Ls/hello
- []
Create the Znode node and specify the associated data
Bash code
- Create/hello World
Create the node/hello and associate the string "World" to the node.
Get data and status information for Znode
Bash code
- [Zk:10.1.39.43:4180 (CONNECTED) 7] Get/hello
- World
- Czxid = 0x10000042c
- CTime = Fri 17:57:33 CST 2013
- Mzxid = 0x10000042c
- Mtime = Fri 17:57:33 CST 2013
- Pzxid = 0x10000042c
- cversion = 0
- dataversion = 0
- aclversion = 0
- Ephemeralowner = 0x0
- Datalength = 5
- Numchildren = 0
Delete Znode
Bash code
- [Zk:localhost:4180 (CONNECTED)] delete/xing/item0000000001
- [Zk:localhost:4180 (CONNECTED)] Delete/xing
- Node not empty:/xing
Use the Delete command to delete the specified znode. When the Znode has a child znode, it must first delete all its child znode, otherwise the operation will fail. The RMR command can be used in place of the Delete command, RMR is a recursive delete command, and the RMR command removes the child nodes first if the specified node has child nodes.
Status information for the Znode node
When you use the GET command to get the data for a specified node, it also returns the state information for that node, called Stat. It contains the following fields:
- Czxid. The Zxid when the node is created.
- Mzxid. The Zxid of the node when the latest update occurs.
- CTime. Timestamp when the node was created.
- Mtime. Timestamp when the node's latest update occurred.
- Dataversion. The number of times the node data was updated.
- Cversion. The number of updates to its child nodes.
- Aclversion. The number of updates to the node ACL (authorization information).
- Ephemeralowner. If the node is a ephemeral node, the Ephemeralowner value represents the session ID that is bound to the node. If the node is not a ephemeral node, the Ephemeralowner value is 0. As for what is the ephemeral node, look at the following narration.
- Datalength. The number of bytes of node data.
- Numchildren. The number of child nodes.
Zxid
The status information for the Znode node contains Czxid and Mzxid, so what is ZXID?
Each change in the zookeeper state corresponds to an increment Transaction id
, which is called Zxid. Because of the increment of Zxid, if ZXID1 is less than Zxid2, then zxid1 must precede zxid2. Create any node, or update the data of any node, or deleting any node will cause the zookeeper state to change, resulting in an increase in the value of ZXID.
Session
Before client and server communication, you first need to establish a connection, which is called the session. After the connection is established, if a connection timeout occurs, authorization fails, or the connection is explicitly closed, the connection is in the closed state, at which point the session ends.
Node type
When you tell the Ephemeralowner field of a node state, the nodes that are mentioned are ephemeral nodes, and some are not. So what types of nodes do they have? What are the characteristics of each type of node?
persistent
. The persistent node is not bound to a specific session and does not disappear with the end of the session that created the node, but persists until the node is explicitly deleted.
ephemeral
. The ephemeral node is temporary, and if the session that created the node ends, the node is automatically deleted. The ephemeral node cannot have child nodes. Although the ephemeral node is bound to the session that created it, the other session can read and write the associated data in that node as long as the node is not deleted. Use the-e parameter to specify the create ephemeral node.
Bash code
- [Zk:localhost:4180 (CONNECTED) 4] create-e/xing/ei World
- Created/xing/ei
sequence
. Strictly speaking, sequence is not one of the node types. The sequence node can be either ephemeral or persistent. When you create a sequence node, ZooKeeper server adds a sequence of numbers after the specified node name, which is incremented. As a result, you can create the same sequence node multiple times and get different nodes. Use the-s parameter to specify the create sequence node.
Bash code
- [zk:localhost:4180 (CONNECTED) 0] create-s/xing/item World
- created/xing/item0000000001
- [Zk:localhost:4180 (CONNECTED) 1] create-s/xing/item World
- created/xing/item0000000002
- [Zk:localhost:4180 (CONNECTED) 2] create-s/xing/item World
- created/xing/item0000000003
- [Zk:localhost:4180 (CONNECTED) 3] create-s/xing/item World
- created/xing/item0000000004
Watch
Watch means listening to events of interest. On the command line, the following commands can specify whether to listen for the appropriate event.
ls command. The first parameter of the LS command specifies Znode, and the second parameter, if true, indicates the increase or decrease of the child nodes listening to the Znode, and the delete event for the Znode itself.
Bash code
- [Zk:localhost:4180 (CONNECTED)] ls/xing true
- []
- [Zk:localhost:4180 (CONNECTED)] Create/xing/item item000
- Watcher::
- Watchedevent state:syncconnected type:nodechildrenchanged path:/xing
- Created/xing/item
Get command. The first parameter of the GET command specifies Znode, and the second parameter, if True, describes the update and delete events that are listening for the znode.
Bash code
- [Zk:localhost:4180 (CONNECTED)] get/xing true
- World
- Czxid = 0x100000066
- CTime = Fri 22:30:01 CST 2013
- Mzxid = 0x100000066
- Mtime = Fri 22:30:01 CST 2013
- Pzxid = 0x100000066
- cversion = 0
- dataversion = 0
- aclversion = 0
- Ephemeralowner = 0x0
- Datalength = 5
- Numchildren = 0
- [Zk:localhost:4180 (CONNECTED)] Create/xing/item item000
- Created/xing/item
- [Zk:localhost:4180 (CONNECTED)] Rmr/xing
- Watcher::
- Watchedevent state:syncconnected type:nodedeleted path:/xing
Stat command. The stat command is used to obtain status information for Znode. The first parameter specifies Znode, and if the second argument is true, the update and delete events for that node are monitored.
"Go" ZooKeeper data model