So far, we discussed the theory behind Zookeeper and have seen a Java example with Zookeeper’s programmatic API. In this lesson, we’ll exercise different commands offered by the zkCli.sh utility that comes with the Zookeeper download. We fire up the Zookeeper server and then invoke the command line client to connect to the server. The various commands are listed in the widget below with an explanation for each. You can connect to the terminal below and execute the commands to see their outcome.
xxxxxxxxxx
# Start the Zookeeper server
zkServer.sh start
# Connect to the Zookeeper server
zkCli.sh -server 127.0.0.1:2181
# To see all the available commands, write help or any
# other string. Note, help isn't a command offered by the
# CLI but incorrect usage triggers a listing of all available commands.
help
# Print version
version
# Perform a listing of the root znode
ls /
# By default, the zookeeper node has two subnodes config and quota
ls /zookeeper
# Create a parent znode
create /MyZNode "Hello World"
# Perform a listing of the root again to see MyZNode
ls /
# Get data and stats for MyZNode. Omitting the -s flag will just
# retrieve the MyZNode data
get -s /MyZNode
# Create an ephemeral and sequential znode
create -e -s /SeqEmpNode "This is a sequential ephemeral znode"
# Perform a listing of the root path to see the just created znode
ls /
# Retrieve data for the SeqEmpNode0000000016
get /SeqEmpNode0000000016 # Your znode may have a different sequence number, use that instead.
# Add a watch on /MyZNode
addWatch /MyZNode
# Update the ZNode and see the watch trigger. A message will print on the
# console indicating the watch has been triggered.
set /MyZNode "Updated znode"
# See the updated data and note the value of the field dataVersion
get -s /MyZNode
# Delete MyZNode
delete -v 1 /MyZNode
# Remove watches
removewatches /MyZNode
# Print history
history
# Close connection
close
# Quit the zkCli console
quit