首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

zookeeper惯用API

2013-12-02 
zookeeper常用API+ state + e.getState().name()) } }) /*try { zook.exists(/root, true) } cat

zookeeper常用API
+ "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());
}
}

热点排行