Work with znodes
Step 1. Create a znode
To create a new znode, use the create
command, then define the full path to the new znode, and then write the data to be stored in this znode. Depending on the znode type, additional flags can be required:
-
Persistent. Use the default call of the
create
command without additional flags:create /z_persistent "Test configuration"
The output:
Created /z_persistent
-
Sequential. Use the
-s
flag after thecreate
keyword:create -s /z_sequential "Test configuration 2"
NOTEZooKeeper adds the 10-digit postfix to the original znode name. This change guarantees the uniqueness of the sequential znode name.
The output is listed below:
Created /z_sequential0000000007
-
Ephemeral. Use the
-e
flag after thecreate
keyword:create -e /z_ephemeral "Test configuration 3"
The output:
Created /z_ephemeral
Step 2. Get znode data
To get the data stored in the specified znode, use the get
command with the full path to this znode:
get /z_persistent
You can see that the command output contains not only the data stored in the specified znode, but also its metadata — so-called stat:
Test configuration cZxid = 0x2000000019 ctime = Wed Dec 15 11:15:27 UTC 2021 mZxid = 0x2000000019 mtime = Wed Dec 15 11:15:27 UTC 2021 pZxid = 0x2000000019 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 18 numChildren = 0
Below you can find the examples of getting data for two other znodes.
IMPORTANT
|
[zk: localhost:2181(CONNECTED) 7]get /z_sequential0000000007 Test configuration 2 cZxid = 0x200000001c ctime = Wed Dec 15 11:17:47 UTC 2021 mZxid = 0x200000001c mtime = Wed Dec 15 11:17:47 UTC 2021 pZxid = 0x200000001c cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 20 numChildren = 0 [zk: localhost:2181(CONNECTED) 8]get /z_ephemeral Test configuration 3 cZxid = 0x200000001d ctime = Wed Dec 15 11:18:35 UTC 2021 mZxid = 0x200000001d mtime = Wed Dec 15 11:18:35 UTC 2021 pZxid = 0x200000001d cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x100005560090006 dataLength = 20 numChildren = 0
NOTE
Starting ZooKeeper 3.5, the get /znode command does not output znode data for container nodes.
Use get -s /znode instead.
|
Step 3. Set data to the znode
To assign a new data value to the specified znode, write the set
command, the full path to this znode, and then the data to be stored:
set /z_persistent "The test configuration has been updated with the command set"
The output is similar to the following:
cZxid = 0x2000000019 ctime = Wed Dec 15 11:15:27 UTC 2021 mZxid = 0x200000001e mtime = Wed Dec 15 11:24:46 UTC 2021 pZxid = 0x2000000019 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 60 numChildren = 0
Now, if you run the get
command, you will receive the updated data value of the znode:
get /z_persistent
The output:
The test configuration has been updated with the command set cZxid = 0x2000000019 ctime = Wed Dec 15 11:15:27 UTC 2021 mZxid = 0x200000001e mtime = Wed Dec 15 11:24:46 UTC 2021 pZxid = 0x2000000020 cversion = 2 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 60 numChildren = 2
Step 4. Create znode children
To add child nodes for the specified znode, you can use the standard command create
and define the path to the parent znode instead of the root path /
.
create /z_persistent/child1 "Test child 1"
The examples below create two children for the parent znode z_persistent
:
-
Create the first child:
create /z_persistent/child1 "Test child 1"
The output:
Created /z_persistent/child1
-
Create the second child:
create /z_persistent/child2 "Test child 2"
The output:
Created /z_persistent/child2
Step 5. List znode children
To display the children of the specified znode, use the ls
command with the full path to the parent znode:
ls /z_persistent
The output is similar to the following:
[child2, child1]
You can also get the children of the root znode, using the /
path:
ls /
The result is listed below:
[zookeeper, Arenadata.Hadoop-1.solr.server, z_sequential0000000007, hadoop-ha, z_ephemeral, hbase, z_persistent]
Additionally, you can use the ls2
command to get not only the children of the specified znode, but also its metadata.
ls2 /
The output:
[zookeeper, Arenadata.Hadoop-1.solr.server, hadoop-ha, z_ephemeral, z_sequential0000000007, hbase, z_persistent] cZxid = 0x0 ctime = Thu Jan 01 00:00:00 UTC 1970 mZxid = 0x0 mtime = Thu Jan 01 00:00:00 UTC 1970 pZxid = 0x2000000030 cversion = 17 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 7
Step 6. Get znode metadata
To get metadata of the specified znode (so-called stat), use the stat
command with the full path to this znode. In fact, this command outputs the same information as get
, except for the stored data itself:
stat /z_persistent
The output of running the command for the previously created znodes is shown below:
cZxid = 0x2000000019 ctime = Wed Dec 15 11:15:27 UTC 2021 mZxid = 0x200000001e mtime = Wed Dec 15 11:24:46 UTC 2021 pZxid = 0x2000000020 cversion = 2 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 60 numChildren = 2 [zk: localhost:2181(CONNECTED) 15] stat /z_sequential0000000007 cZxid = 0x200000001c ctime = Wed Dec 15 11:17:47 UTC 2021 mZxid = 0x200000001c mtime = Wed Dec 15 11:17:47 UTC 2021 pZxid = 0x200000001c cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 20 numChildren = 0 [zk: localhost:2181(CONNECTED) 16] stat /z_ephemeral cZxid = 0x200000001d ctime = Wed Dec 15 11:18:35 UTC 2021 mZxid = 0x200000001d mtime = Wed Dec 15 11:18:35 UTC 2021 pZxid = 0x200000001d cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x100005560090006 dataLength = 20 numChildren = 0
Step 7. Watch a znode for changes
Watches allow a client to get notifications about changes in the monitored znodes. To create a new Watch, run the get
command with the additional argument, which is the watcher name.
TIP
You can create a Watch using the following commands: |
get /z_persistent 1
The output is the same as for the get
standard call:
The test configuration has been updated with the command set cZxid = 0x2000000019 ctime = Wed Dec 15 11:15:27 UTC 2021 mZxid = 0x200000001e mtime = Wed Dec 15 11:24:46 UTC 2021 pZxid = 0x2000000020 cversion = 2 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 60 numChildren = 2
Next, run the set
command to change the znode data:
set /z_persistent "Updated"
This time, ZooKeeper prints out not only the regular output of the set
command, but also the notification telling the client about changes applied to the monitored znode:
WATCHER:: WatchedEvent state:SyncConnected type:NodeDataChanged path:/z_persistent cZxid = 0x2000000019 ctime = Wed Dec 15 11:15:27 UTC 2021 mZxid = 0x2000000021 mtime = Wed Dec 15 11:37:02 UTC 2021 pZxid = 0x2000000020 cversion = 2 dataVersion = 2 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 7 numChildren = 2
Remember that each Watch is triggered only once. Next calls of the set
command will not result in sending a notification, until the client creates a new Watch.
[zk: localhost:2181(CONNECTED) 24] set /z_persistent "Updated 2" cZxid = 0x2000000019 ctime = Wed Dec 15 11:15:27 UTC 2021 mZxid = 0x2000000022 mtime = Wed Dec 15 11:37:51 UTC 2021 pZxid = 0x2000000020 cversion = 2 dataVersion = 3 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 9 numChildren = 2
Step 8. Remove the znode
To remove a specified znode permanently, use the rmr
command and then write the path to this znode.
rmr /z_persistent
Now, if you try to get the data from the deleted znode or its children, you will see that these znodes do not exist anymore:
[zk: localhost:2181(CONNECTED) 26] get /z_persistent Node does not exist: /z_persistent [zk: localhost:2181(CONNECTED) 27] get /z_persistent/child1 Node does not exist: /z_persistent/child1
In conclusion, let’s check that ephemeral znodes are removed automatically after the client disconnection. To do this, run the quit
command and start the new client session by running zkCli.sh. Then call the ls /
command to get all children of the root znode. You will find out the following:
-
The persistent znode
/z_persistent
is removed, as we have deleted it in the example above. -
The sequential znode
/z_sequential0000000007
is still available. -
The ephemeral znode
/z_ephemeral
has been automatically removed.
[zk: localhost:2181(CONNECTED) 0] ls /
The output:
[zookeeper, Arenadata.Hadoop-1.solr.server, z_sequential0000000007, hadoop-ha, hbase]