Work with znodes

This article walks you through major interaction steps with ZooKeeper znodes. All commands mentioned below are intended to run in the ZooKeeper CLI shell. To open the shell, use the /usr/lib/zookeeper/bin/zkCli.sh script.

NOTE

Before executing the following commands, 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_sequential0000000004
  • 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 data stored in the specified znode, use the get command with the full path to this znode:

get /z_persistent

The output:

Test configuration

To view not only the data stored in the specified znode, but also its metadata (stat), append the -s flag:

get -s /z_persistent

The output:

Test configuration
cZxid = 0x40000000f
ctime = Mon Jun 22 07:35:48 UTC 2026
mZxid = 0x40000000f
mtime = Mon Jun 22 07:35:48 UTC 2026
pZxid = 0x40000000f
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 deleteall commands.

[zk: localhost:2181(CONNECTED) 0] get -s /z_sequential0000000004
Test configuration 2
cZxid = 0x400000010
ctime = Mon Jun 22 07:35:57 UTC 2026
mZxid = 0x400000010
mtime = Mon Jun 22 07:35:57 UTC 2026
pZxid = 0x400000010
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 20
numChildren = 0
[zk: localhost:2181(CONNECTED) 16] get -s /z_ephemeral
Test configuration 3
cZxid = 0x40000001e
ctime = Mon Jun 22 08:22:12 UTC 2026
mZxid = 0x40000001e
mtime = Mon Jun 22 08:22:12 UTC 2026
pZxid = 0x40000001e
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x100000041f80008
dataLength = 20
numChildren = 0

Step 3. Set data to a znode

To assign new data to the specified znode, use the set command, specify the full path to the znode, and then the data to be stored:

set /z_persistent "The test configuration has been updated with the command set"

Now, if you run the get command, you will receive the updated data value of the znode:

get -s /z_persistent

The output:

The test configuration has been updated with the command set
cZxid = 0x40000000f
ctime = Mon Jun 22 07:35:48 UTC 2026
mZxid = 0x40000001f
mtime = Mon Jun 22 08:26:32 UTC 2026
pZxid = 0x40000000f
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 60
numChildren = 0

Step 4. Create znode children

To add child nodes for a 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:

[arenadata, hadoop-ha, z_ephemeral, z_persistent, z_sequential0000000004, zookeeper]

Additionally, you can use the -s flag to get not only the children of the specified znode, but also its metadata:

ls -s /

The output:

[arenadata, hadoop-ha, z_persistent, z_sequential0000000004, zookeeper]
cZxid = 0x0
ctime = Thu Jan 01 00:00:00 UTC 1970
mZxid = 0x0
mtime = Thu Jan 01 00:00:00 UTC 1970
pZxid = 0x400000023
cversion = 9
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 5

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 -s, except for the stored data itself:

stat /z_persistent

The output of running the command for the previously created znodes is shown below:

[zk: localhost:2181(CONNECTED) 65] stat /z_persistent
cZxid = 0x40000000f
ctime = Mon Jun 22 07:35:48 UTC 2026
mZxid = 0x40000001f
mtime = Mon Jun 22 08:26:32 UTC 2026
pZxid = 0x400000021
cversion = 2
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 60
numChildren = 2
[zk: localhost:2181(CONNECTED) 38] stat /z_sequential0000000004
cZxid = 0x400000010
ctime = Mon Jun 22 07:35:57 UTC 2026
mZxid = 0x400000010
mtime = Mon Jun 22 07:35:57 UTC 2026
pZxid = 0x400000010
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 20
numChildren = 0
[zk: localhost:2181(CONNECTED) 39] stat /z_ephemeral
cZxid = 0x400000024
ctime = Mon Jun 22 08:49:41 UTC 2026
mZxid = 0x400000024
mtime = Mon Jun 22 08:49:41 UTC 2026
pZxid = 0x400000024
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x100000041f80009
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, use the -w flag with the get, stat, and ls commands.

Examples of setting a watch:

get -w /z_persistent (1)
stat -w /z_persistent (2)
ls -w /z_persistent (3)
1 Sets a watch to track znode data changes.
2 Sets a watch to track stat/metadata changes.
3 Sets a watch to track znode children changes.
NOTE

A ZooKeeper watch triggers only once. When a znode is modified, the attached watch fires a notification and gets destroyed. To receive notifications on further znode updates, you have to create a new watch.

When setting a watch, the output is the same as for the regular command, for example:

[zk: localhost:2181(CONNECTED) 2] get /z_persistent -w
The test configuration has been updated with the command set

Next, run the set command to change the znode data:

set /z_persistent "Updated"

This time, ZooKeeper returns 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

Step 8. Remove a znode

To remove a specified znode permanently, use the deleteall command and then specify the path to this znode:

deleteall /z_persistent

After running this command, the specified znode and all its children are not available anymore:

[zk: localhost:2181(CONNECTED) 5] get /z_persistent
Node does not exist: /z_persistent
[zk: localhost:2181(CONNECTED) 6] 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 a 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 not available, as it has been deleted in the example above.

  • The sequential znode /z_sequential0000000004 is still available.

  • The ephemeral znode /z_ephemeral has been automatically removed.

ls /

The output:

[arenadata, hadoop-ha, z_sequential0000000004, zookeeper]
Found a mistake? Seleсt text and press Ctrl+Enter to report it