You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by Apache Wiki <wi...@apache.org> on 2008/11/11 21:18:44 UTC
[Hadoop Wiki] Update of "ZooKeeper/ZooKeeperExercise1" by BenjaminReed
Dear Wiki user,
You have subscribed to a wiki page or wiki category on "Hadoop Wiki" for change notification.
The following page has been changed by BenjaminReed:
http://wiki.apache.org/hadoop/ZooKeeper/ZooKeeperExercise1
New page:
Let's start off by creating a minimal ZooKeeper client:
{{{
import java.io.*;
import org.apache.zookeeper.*;
public class SimpleZoo {
static class MyWatcher implements Watcher {
public void process(WatchedEvent event) {
System.err.println(event);
}
}
public static void main(String[] args) throws Exception {
MyWatcher myWatcher = new MyWatcher();
ZooKeeper zk = new ZooKeeper(args[0], 10000, myWatcher);
Thread.sleep(10000);
zk.close();
}
}
}}}
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(), 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", newdata, -1);
}}}
Create a new simple client to read "/motd".
{{{
Stat stat = new Stat();
byte data[] = zk.getData("/motd", true, stat);
}}}
Try changing /data and watch the events you get. What if you change /data twice in a row? Could this be used for messaging?