zookeeper常用API

zookeeper常用的api,刚刚接触,写博客备忘

package com.my.zookeeper;

import java.io.IOException;
import java.util.List;

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.Ids;
import org.apache.zookeeper.ZooKeeper;

public class ZooKeeperAPI {
static ZooKeeper zook = null;
static {
try {
//创建一个Zookeeper实例,第一个参数为目标服务器地址和端口,
//第二个参数为Session超时时间,
//第三个为节点变化时的回调方法
zook = new ZooKeeper("192.168.229.53:2181", 30000, new Watcher(){
public void process(WatchedEvent e) {
System.out.println("path=" + e.getPath()
+ "state =" + e.getState().name());
}
});

/*try {
zook.exists("/root", true);
} catch (KeeperException e2) {
e2.printStackTrace();
} catch (InterruptedException e2) {
e2.printStackTrace();
}
try {
zook.exists("/root", new Watcher(){
public void process(WatchedEvent e) {
System.out.println("path=" + e.getPath()
+ "state =" + e.getState().name());
}
});
} catch (KeeperException e1) {
e1.printStackTrace();
} catch (InterruptedException e1) {
e1.printStackTrace();
}*/
} catch (IOException e) {
e.printStackTrace();
}
}

void showAPI() {
try {
////创建一个节点root,数据是mydata,不进行ACL权限控制
//节点为永久性的(即客户端shutdown了也不会消失)
zook.create("/root", "mydata".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
//在root下面创建一个childone znode,数据为childone,
//不进行ACL权限控制,节点为永久性的
zook.create("/root/childone", "childone".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}

try {
//取得/root节点下的子节点名称,返回List<String>
List<String> childNodeList = zook.getChildren("/root", true);
System.out.println(childNodeList.size());
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}

//取得/root/childone节点下的数据,返回byte[]
try {
byte[] childData = zook.getData("/root/childone", true, null);
System.out.println(childData.length);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}

try {
//修改节点/root/childone下的数据,第三个参数为版本,
//如果是-1,那会无视被修改的数据版本,直接改掉
zook.setData("/root/childone", "childonemodify".getBytes(), -1);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}

try {
//删除/root/childone这个节点,第二个参数为版本,-1的话直接删除,无视版本
zook.delete("/root/childone", -1);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (KeeperException e) {
e.printStackTrace();
}

//关闭session
try {
zook.close();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public static void main(String[] args) throws KeeperException, InterruptedException, IOException {
//取得gateway-schedule节点下的子节点名称,返回List<String>
List<String> childNodeList = zook.getChildren("/gateway-schedule", true);
for(String s : childNodeList) {
System.out.println("s = " + s);
}
System.out.println(childNodeList.size());
//zook.create("/root", "mydata".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
//zook.create("/root/childone", "child3".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
//zook.create("/root/childtwo", "childtwo".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
//zook.create("/root/child3", "child3".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
//zook.setData("/root/childtwo", "childtwomodify".getBytes(), -1);

/*byte[] childData = zook.getData("/root/childone", true, null);
System.out.println("childData.length="+childData.length + ",childData=" + new String(childData));*/
//zook.delete("/root/childtwo", -1);

showAllChild();

System.in.read();
}

/**
* show all first nodes
* @throws KeeperException
* @throws InterruptedException
*/
static void showAllChild() throws KeeperException, InterruptedException {
//
List<String> firstNodeList = zook.getChildren("/", true);
for(String s : firstNodeList) {
System.out.println("s = " + s);
}
System.out.println(firstNodeList.size());
}
}

猜你喜欢

转载自sugongp.iteye.com/blog/1740118