May 26, 2021 Zookeeper
The ZooKeeper Command Line Interface (CLI) is used to interact with the ZooKeeper collection for development.
It helps debug and resolve different options.
create /path /data
create /FirstZnode “Myfirstzookeeper-app"
[zk: localhost:2181(CONNECTED) 0] create /FirstZnode “Myfirstzookeeper-app" Created /FirstZnode
To create a sequential node, add flag: -s, as shown below.
create -s /path /data
create -s /FirstZnode second-data
[zk: localhost:2181(CONNECTED) 2] create -s /FirstZnode “second-data" Created /FirstZnode0000000023
To create a temporary node, add flag: -e, as shown below.
create -e /path /data
create -e /SecondZnode “Ephemeral-data"
[zk: localhost:2181(CONNECTED) 2] create -e /SecondZnode “Ephemeral-data" Created /SecondZnode
Remember that when the client disconnects, the temporary node is deleted. Y ou can try by exiting the ZooKeeper CLI and then reopening the CLI.
It returns the associated data for znode and the metadata for the specified znode. Y ou'll get information, such as when the data was last modified, where it was modified, and information about the data. /b11> This CLI is also used to assign monitors to display data-related notifications.
get /path
get /FirstZnode
[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 sequential nodes, you must enter the full path of znode.
get /FirstZnode0000000023
[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
The monitor displays a notification when the specified znode or znode's child data changes. Y ou can only set watch in the get command.
get /path [watch] 1
get /FirstZnode 1
[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 a normal get command, but it waits in the background for znode to change. Start here. .
Set the data that specifies znode. /b10> After you complete this setup, you can use the get CLI command to examine the data.
set /path /data
set /SecondZnode Data-updated
[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 assign the watch option in the get command, as in the previous command, the output will look something like this.
[zk: localhost:2181(CONNECTED) 1] get /FirstZnode “Mysecondzookeeper-app" WATCHER: : 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
Creating a child node is similar to creating a new znode. /b10> The only difference is that the path of the child znode will also have a parent path.
create /parent/path/subnode/path /data
create /FirstZnode/Child1 firstchildren
[zk: localhost:2181(CONNECTED) 16] create /FirstZnode/Child1 “firstchildren" created /FirstZnode/Child1 [zk: localhost:2181(CONNECTED) 17] create /FirstZnode/Child2 “secondchildren" created /FirstZnode/Child2
This command is used to list and display znode's children.
ls /path
ls /MyFirstZnode
[zk: localhost:2181(CONNECTED) 2] ls /MyFirstZnode [mysecondsubnode, myfirstsubnode]
The status describes the metadata for the specified znode. /b10> It contains timestamps, version numbers, ACLs, data lengths, and subznode.
stat /path
stat /FirstZnode
[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 the specified znode and recursively all of its child nodes. /b10> This will only happen if such znode is available.
rmr /path
rmr /FirstZnode
[zk: localhost:2181(CONNECTED) 10] rmr /FirstZnode [zk: localhost:2181(CONNECTED) 11] get /FirstZnode Node does not exist: /FirstZnode
The delete/path command is similar to the remove command, except that it applies only to znode without child nodes.