Zookeeper Tutorial – Tutorialspoint

The ZooKeeper

command line interface (CLI) is used to interact with the ZooKeeper suite for development purposes. It is useful for debugging and working with different options.

To perform ZooKeeper CLI operations, first power on the ZooKeeper server (“bin/zkServer.sh start”) and then the ZooKeeper client (“bin/zkCli.sh”). Once the client starts, you can perform the following operation:

Create znodes Get data View znode

  • to see changes
  • Set data
  • Create

  • children of a znode List of children of a znode

  • Check the status
  • Delete/Delete a znode Now let’s look above the command one by one with an example.

  • Create znodes
  • Create

  • a
  • znode

with

the given path. The flag argument specifies whether the znode created will be ephemeral, persistent, or sequential. By default, all znodes are persistent.

  • Ephemeral znodes (flag: e) will be automatically deleted when a session expires or when the client disconnects

  • .

  • Sequential znodes guarantee that the znode path will be unique

  • .

  • The ZooKeeper set will add the sequence number along with the 10-digit padding to the znode path. For example, the znode /myapp path will be converted to /myapp0000000001 and the next sequence number will be /myapp0000000002. If no indicators are specified, the znode is considered persistent.

Syntax

create /path /data

Sample

create /FirstZnode “Myfirstzookeeper-app” Output [zk: localhost:2181(CONNECTED) 0] create /FirstZnode “Myfirstzookeeper-app” Created /FirstZnode

To create a sequential znode, add the -s flag as shown below.

Create -s /path /data

syntax

Create -s /FirstZnode second-data

example

Output [zk: localhost

:2181(CONNECTED) 2] create -s /FirstZnode “second-data” Created /FirstZnode000000023

To create an ephemeral Znode, add the -e flag as shown below.

Syntax

create -e /path /data

Sample

create -e /SecondZnode “Ephemeral-data”

Output

[zk: localhost:2181(CONNECTED) 2] create -e /SecondZnode “Ephemeral-data” Created /SecondZnode

Remember that when a client connection is lost, the ephemeral znode will be deleted. You can test it by exiting the ZooKeeper CLI and then reopening the CLI.

Get

Data Returns the associated data of the znode

and the metadata of the specified znode. You’ll get information such as when the data was last modified, where it was modified, and information about the data. This CLI is also used to assign clocks to display notifications about data.

Get /path

syntax

Sample get /FirstZnode Output [zk: localhost:2181(CONNECTED) 1] get /FirstZnode “Myfirstzookeeper-app” cZxid = 0x7f ctime = Tue Sep 29 16:15:47 IST 2015 mZxid = 0x7f mtime = Tue Sep 29 16:15:47 IST 2015 pZxid = 0x7f cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 22 numChildren = 0

To access a sequential znode, you must enter the full path of the znode.

Sample

get /FirstZnode0000000023

Output

[zk: localhost:2181(CONNECTED) 1] get /FirstZnode0000000023 “Second-data” cZxid = 0x80 ctime = Tue Sep 29 16:25:47 IST 2015 mZxid = 0x80 mtime = Tue Sep 29 16:25:47 IST 2015 pZxid = 0x80 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 13 numChildren = 0

Watch

Watches displays a notification when the specified znode or znode secondary data changes. You can set a clock only in the get command.

Syntax

get /path [watch] 1

Sample

get /FirstZnode 1

Output

[zk: localhost:2181(CONNECTED) 1] get /FirstZnode 1 “Myfirstzookeeper-app” cZxid = 0x7f ctime = Tue Sep 29 16:15:47 IST 2015 mZxid = 0x7f mtime = Tue Sep 29 16:15:47 IST 2015 pZxid = 0x7f cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 22 numChildren = 0

The output is similar to the get command normal, but will wait for znode changes in the background. <Start here>

Set data

Set the data of the specified znode. After you finish this assembly operation, you can verify the data using the get CLI command.

Syntax

set /path /data

Sample set

/SecondZnode

Updated data output

[zk: localhost:2181(CONNECTED) 1] get /SecondZnode “Data-updated” cZxid = 0x82 ctime = Tue Sep 29 16:29:50 IST 2015 mZxid = 0x83 mtime = Tue Sep 29 16:29:50 IST 2015 pZxid = 0x82 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x15018b47db00000 dataLength = 14 numChildren = 0

If you assigned the watch option in get (as in the previous command), then the output will be similar as shown below −

Output

[zk: localhost:2181(CONNECTED) 1] get /FirstZnode “Mysecondzookeeper-app” OBSERVER: : WatchedEvent state:SyncConnected type:NodeDataChanged path:/FirstZnode cZxid = 0x7f ctime = Tue Sep 29 16:15:47 IST 2015 mZxid = 0x84 mtime = Tue Sep 29 17:14:47 IST 2015 pZxid = 0x7f cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 23 numChildren = 0

Create children / Sub-znode Create children

is similar to creating new znodes. The only difference is that the path of the secondary znode will also have the primary path.

Create /parent/path/subnode/path /data

syntax

Create /FirstZnode/Child1 example firstchildren

Output

[zk: localhost:2181(CONNECTED) 16] create /FirstZnode/Child1 “firstchildren” created /FirstZnode/Child1 [zk: localhost:2181(CONNECTED) 17] create /FirstZnode/Child2 “secondchildren” created /FirstZnode/Child2

List Children

This command is used to enumerate and display the children of a znode.

ls /path

Sample

syntax

ls / MyFirstZnode

Output

[zk: localhost:2181(CONNECTED) 2] ls /MyFirstZnode [mysecondsubnode, myfirstsubnode]

Check Status

Status describes the metadata for a specified znode. It contains details such as timestamp, version number, ACL, data length, and secondary znode.

Syntax

stat /path

Sample

stat /FirstZnode

Output

[zk: localhost:2181(CONNECTED) 1] stat /FirstZnode cZxid = 0x7f ctime = Tue Sep 29 16:15:47 IST 2015 mZxid = 0x7f mtime = Tue Sep 29 17:14:24 IST 2015 pZxid = 0x7f cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 23 numChildren = 0

Remove a Znode Removes a specified znode

and recursively removes all of its children. This would happen only if such a znode is available.

rmr

syntax

/path

rmr

example /FirstZnode

Output

[zk: localhost:2181(CONNECTED) 10] rmr /FirstZnode [zk: localhost:2181(CONNECTED) 11] get /FirstZnode Node does not exist: /FirstZnode

The Delete command (delete /path) is similar to the remove command, except that it only works on non-child znodes.