Database Reference
In-Depth Information
Try creating an empty topmost node named “zk-top,” using this syntax:
[zk: localhost:2181(CONNECTED) 4] create /zk-top ''
Created /zk-top
You can create a subnode, node1, of zk-top as well; you can add the contents cfg1 at the same time:
[zk: localhost:2181(CONNECTED) 5] create /zk-top/node1 'cfg1'
Created /zk-top/node1
To check the contents of the subnode (or any node), you use the get command:
[zk: localhost:2181(CONNECTED) 6] get /zk-top/node1
'cfg1'
The delete command, not surprisingly, deletes a node:
[zk: localhost:2181(CONNECTED) 8] delete /zk-top/node2
The set command changes the context of a node. This command sequence first checks the contents of node1
with get , changes it with set , and then displays the new contents:
[zk: localhost:2181(CONNECTED) 9] get /zk-top/node1
'cfg1'
[zk: localhost:2181(CONNECTED) 10] set /zk-top/node1 'cfg2'
[zk: localhost:2181(CONNECTED) 11] get /zk-top/node1
'cfg2'
The contents of node1 changed from cfg1 to cfg2. Although this is a simple example, it explains the principle.
You can also list the subnodes of a node with the ls command. For example, node1 has no subnodes, but zk-top
contains node1:
[zk: localhost:2181(CONNECTED) 0] ls /zk-top/node1
[]
[zk: localhost:2181(CONNECTED) 1] ls /zk-top
[node1]
You can place watches on the nodes to check whether they change. Watches are one-time events. If the contents
change, then the watch fires and you will need to reset it. To demonstrate, I create a subnode node2 that contains
data2:
[zk: localhost:2181(CONNECTED) 9] create /zk-top/node2 'data2'
[zk: localhost:2181(CONNECTED) 10] get /zk-top/node2
'data2'
 
Search WWH ::




Custom Search