ZooKeeper's data model and its API support the following nine basic operations:
Let's look at the ZooKeeper operations mentioned in the preceding table using ZooKeeper Java shell:
ThisIsTheRootNode
as its data:[zk: localhost(CONNECTED) 0] create /root "ThisIsTheRootNode" Created /root
root
:[zk: localhost(CONNECTED) 1] get /root "ThisIsTheRootNode" …… …… …… ……
child-1
for root
with ThisIsChild-1
as its data:[zk: localhost(CONNECTED) 2] create /root/child-1 "ThisIsChild-1" Created /root/child-1
child-2
for root
with ThisIsChild-2
as its data:[zk: localhost(CONNECTED) 3] create /root/child-2 "ThisIsChild-2" Created /root/child-2
root
:[zk: localhost(CONNECTED) 4] ls /root [child-2, child-1]
root
:[zk: localhost(CONNECTED) 5] getAcl /root 'world,'anyone : cdrwa
root
is not allowed as root
has 2 child znodes:[zk: localhost(CONNECTED) 6] delete /root Node not empty: /root
child-1
:[zk: localhost(CONNECTED) 7] delete /root/child-1
child-2
:[zk: localhost(CONNECTED) 8] delete /root/child-2
root
:[zk: localhost(CONNECTED) 9] ls2 /root [] …… …… …… ……
root
:[zk: localhost(CONNECTED) 10] delete /root
Apart from the operations described so far, ZooKeeper also supports batch updates to znodes with an operation called multi. This batches together multiple primitive operations into a single unit. A multi operation is also atomic in nature, which means that either all the updates succeed or the whole bunch of updates fails in its entirety.
ZooKeeper does not allow partial writes or reads of the znode data. When setting the data of a znode or reading it, the content of the znode is replaced or read entirely. Update operations in ZooKeeper, such as a delete
or setData
operation, have to specify the version number of the znode that is being updated. The version number can be obtained by using the exists()
call. The update
operation will fail if the specified version number does not match the one in the znode. Also, another important thing to note is that updates in ZooKeeper are non-blocking operations.
The read
and write
operations in ZooKeeper are shown in the following image:
From the preceding image, we need to take note of the following two crucial aspects of these operations:
18.119.120.159