Database Reference
In-Depth Information
Now, I use
get
to set a watcher on that node. The first command sets a watcher on the node2 data item “data2.”
When I change the data to “data3” with the next
set
command, the watcher notices the data change and fires, as shown:
[zk: localhost:2181(CONNECTED) 11] get /zk-top/node2 true
'data2'
[zk: localhost:2181(CONNECTED) 12] set /zk-top/node2 'data3'
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/zk-top/node2
In addition to the basic nodes you've been working with, you can create sequential and ephemeral nodes with
the
create
command.
Ephemeral
nodes exist only for the lifetime of the current session, while
sequential
nodes have a
sequence number applied to the node name and will persist. To create a sequential node, use the
-s
option, as shown:
[zk: localhost:2181(CONNECTED) 13] ls /zk-top
[node2, node1]
[zk: localhost:2181(CONNECTED) 14] create -s /zk-top/node3 'data3'
Created /zk-top/node30000000005
[zk: localhost:2181(CONNECTED) 15] ls /zk-top
[node2, node1, node30000000005]
To create an ephemeral node, use the
-e
option:
[zk: localhost:2181(CONNECTED) 16] create -e /zk-top/node4 'data4'
Created /zk-top/node4
[zk: localhost:2181(CONNECTED) 17] ls /zk-top
[node4, node2, node1, node30000000005]
So, node 4 exists in this session under zk-top. It will disappear when you log out of the session (with
quit
) and
start a new one. Notice that the
ls
command at the end of this sequence no longer lists node4:
[zk: localhost:2181(CONNECTED) 18] quit
Quitting...
2014-03-22 14:02:56,572 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x344e6df92ab0005 closed
2014-03-22 14:02:56,572 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@512] - EventThread
shut down
[hadoop@hc1r1m3 ~]$
[hadoop@hc1r1m3 ~]$ zookeeper-client
[zk: localhost:2181(CONNECTED) 0] ls /zk-top
[node2, node1, node30000000005]
As you can see, node 4 is gone; it only existed for the session in which it was created. Note: you can also make
sequential nodes ephemeral with a command like this:
[zk: localhost:2181(CONNECTED) 16] create -s -e /zk-top/node4 'data4'
Search WWH ::
Custom Search