Differences between revisions 3 and 4
Revision 3 as of 2008-11-13 08:04:07
Size: 1345
Editor: BenjaminReed
Revision 4 as of 2009-09-20 23:54:19
Size: 1345
Editor: localhost
Comment: converted to 1.6 markup
No differences found!

Let's start off by creating a minimal ZooKeeper client:

import java.io.*;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.*;

public class SimpleZoo {
    static class MyWatcher implements Watcher {
        public void process(WatchedEvent event) {
    public static void main(String[] args) throws Exception {
        MyWatcher myWatcher = new MyWatcher();
        ZooKeeper zk = new ZooKeeper(args[0], 10000, myWatcher);

What events do you get when you start?

Lets create a program to create a znode (I'll use "/motd", but you can pick your own name). You'll need to add:

        zk.create("/motd", "hi".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

What happens if you try to create /motd twice?

Create a new simple client to set "/motd":

        zk.setData("/motd", "new data".getBytes(), -1);

Create a new simple client to read "/motd".

        Stat stat = new Stat();
        byte data[] = zk.getData("/motd", true, stat);
        System.out.println(new String(data));

Try changing /data and watch the events you get. What if you change /data twice in a row? Could this be used for messaging?

ZooKeeper/ZooKeeperExercise1 (last edited 2009-09-20 23:54:19 by localhost)