import org.apache.zookeeper.*;
public class ZooKeeperExample implements Watcher {
private ZooKeeper zooKeeper;
public ZooKeeperExample() {
try {
this.zooKeeper = new ZooKeeper("localhost:2181", 10000, this);
} catch (Exception e) {
e.printStackTrace();
}
}
public void createNode(String path, byte[] data) throws KeeperException, InterruptedException {
zooKeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
public byte[] getNodeData(String path) throws KeeperException, InterruptedException {
return zooKeeper.getData(path, false, null);
}
public void process(WatchedEvent event) {
System.out.println(event);
}
public static void main(String[] args) {
ZooKeeperExample example = new ZooKeeperExample();
try {
example.createNode("/myNode", "Hello, ZooKeeper!".getBytes());
byte[] data = example.getNodeData("/myNode");
System.out.println("Node data: " + new String(data));
} catch (Exception e) {
e.printStackTrace();
}
}
}