Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Zookeeper : create,list,update zknodes using java
package com.rajkrrsingh.zk;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
public class ZookeeperRecipe {
private ZooKeeper zk=null;
public ZooKeeper connnect(String hosts,int timeOut) throws IOException, InterruptedException{
final CountDownLatch connectedSignal = new CountDownLatch(1);
this.zk = new ZooKeeper(hosts,timeOut,new Watcher(){
@Override
public void process(WatchedEvent event) {
if(event.getState() == Watcher.Event.KeeperState.SyncConnected){
connectedSignal.countDown();
}
}
});
System.out.println("Before connectedSignal.await();");
connectedSignal.await();
System.out.println("after connectedSignal.await();");
return zk;
}
public void createGroup(String groupName) throws KeeperException, InterruptedException{
String path ="/"+groupName;
zk.create(path, null /* data */, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
public void creatingGroupMembersWithData(String groupName,String member,byte[] data) throws Exception{
String path="/"+groupName+"/"+member+"-"; // this will be sq
System.out.println("created child node with path "+zk.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL_SEQUENTIAL));
}
public void list(String zknode) throws Exception{
String path="/"+zknode;
List<String> children = zk.getChildren(path, new Watcher(){
@Override
public void process(WatchedEvent event) {
if(event.getType()==Event.EventType.NodeChildrenChanged){
System.out.println("node children updated");
}
}});
if(children.isEmpty()){
System.out.println("no children in the zknode");
return;
}
System.out.println("Childrens : "+children+" : "+children.size());
}
private void delete(String zknode) throws Exception {
String path ="/"+zknode;
List<String> childNodes = zk.getChildren(path, false);
//remove the childNodes first
for(String childNode : childNodes){
zk.delete(path+"/"+childNode, -1);
}
zk.delete(path, -1);
System.out.println("Done deleting");
}
public static void main(String[] args) throws Exception{
ZookeeperRecipe zkr = new ZookeeperRecipe();
zkr.connnect("hostname:5181", 5000);
System.out.println("Connected");
System.out.println("Going to Create zk group");
zkr.createGroup("demo-group");
System.out.println("Creating group member zknodes with data");
zkr.creatingGroupMembersWithData("demo-group", "childnode", "data1".getBytes());
System.out.println("Created child node in the group");
System.out.println("listing zknode --> childrens");
zkr.list("demo-group");
System.out.println("Deleting a zknode");
zkr.delete("demo-group");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.