You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@zookeeper.apache.org by neptune <op...@gmail.com> on 2010/02/16 14:47:49 UTC

How do I get added child znode?

Hi all, I'm kimhj.

I have a question. I registered a Watcher on a parent znode("/foo").
I create child znode("/foo/bar1") using a zookeeper console.
Test program received Children changed event. But there is no API getting
added znode.
ZooKeeper.getChildren() method returns all children in a parent node.

public class ZkTest implements Watcher {
  ZooKeeper zk;
  public void test() {
    zk = new ZooKeeper("127.0.0.1:2181", 10 * 1000, this);
    zk.create("/foo", false);
    zk.getChild("/foo", this);
  }

  public void process(WatchedEvent event) {
    if(event.getType() == Event.EventType.NodeChildrenChanged) {
      *List<String> children = zk.getChildren(event.getPath(), this);*
    }
  }
}

Thanks.

Re: How do I get added child znode?

Posted by Patrick Hunt <ph...@apache.org>.
neptune wrote:
> I have a question. I registered a Watcher on a parent znode("/foo").
> I create child znode("/foo/bar1") using a zookeeper console.
> Test program received Children changed event. But there is no API getting
> added znode.
> ZooKeeper.getChildren() method returns all children in a parent node.

getChildren is the correct way to access. When you get a notification 
you don't know if one thing changed, or many. You are just getting a 
notification that something has changed since you originally set the 
watch. You need to determine what has changed. Typically it works like this:

1) getchildren(parent, watch)
2) process the result of get children call
3) wait for notification
4) when you get notification from 3), call getchildren(parent, watch) 
again, notice that you are both getting the children and resetting the 
watch. Without this step (test and set) you might miss notifications of 
change.
5) compare the data from 4 with the data from 1 - process any changes 
(added/removed nodes).

See the recipes page http://bit.ly/938bnt for real world examples.

Patrick

Re: How do I get added child znode?

Posted by Mahadev Konar <ma...@yahoo-inc.com>.
Hi Kim,
  The zookeeper api does not provide an api to get the znode that was added
or deleted. You will have to compare the last set of children and new set of
children to see which one was added or deleted.

Thanks
mahadev


On 2/16/10 5:47 AM, "neptune" <op...@gmail.com> wrote:

> Hi all, I'm kimhj.
> 
> I have a question. I registered a Watcher on a parent znode("/foo").
> I create child znode("/foo/bar1") using a zookeeper console.
> Test program received Children changed event. But there is no API getting
> added znode.
> ZooKeeper.getChildren() method returns all children in a parent node.
> 
> public class ZkTest implements Watcher {
>   ZooKeeper zk;
>   public void test() {
>     zk = new ZooKeeper("127.0.0.1:2181", 10 * 1000, this);
>     zk.create("/foo", false);
>     zk.getChild("/foo", this);
>   }
> 
>   public void process(WatchedEvent event) {
>     if(event.getType() == Event.EventType.NodeChildrenChanged) {
>       *List<String> children = zk.getChildren(event.getPath(), this);*
>     }
>   }
> }
> 
> Thanks.