Work with Znodes

NOTE

Before executing the following commands, we recommend you to read about ZooKeeper Znodes and Watches.

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 the create keyword:

    create -s /z_sequential "Test configuration 2"
    NOTE

    ZooKeeper 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 the create 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

When applying the get command to sequential Znodes, use their full names including the 10-digit numbers assigned by ZooKeeper automatically. This rule also applies to the set and rmr commands.

[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, stat, ls, and ls2.

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]
Found a mistake? Seleсt text and press Ctrl+Enter to report it