package com.zookeeper.zkutil;
import java.util.List;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
/**
* 描述: ZooKeeper的简单API测试方法
*/
public class ZKUtil {
/**
* 获取ZooKeeper链接
* @return
* @throws Exception
*/
public static ZooKeeper getZKConnection(String connectString, int sessionTimeout) throws Exception {
return new ZooKeeper(connectString, sessionTimeout, null);
}
/**
* 创建节点
* @throws Exception
*/
public static String createZKNode(String path, String value, ZooKeeper zk) throws KeeperException, Exception {
String create = zk.create(path, value.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
return create;
}
/**
* 查看节点数据
* @throws Exception
*/
public static String getZNodeData(String path, ZooKeeper zk) throws Exception {
return new String(zk.getData(path, false, null));
}
/**
* 修改节点数据
* @throws Exception
*/
public static boolean updateZNodeData(String path, String value, ZooKeeper zk) throws Exception{
zk.setData(path, value.getBytes(), -1);
String newData = new String(zk.getData(path, false, null));
if(newData.equals(value)){
return true;
}else{
return false;
}
}
/**
* 判断节点是否存在
* @throws Exception
*/
public static boolean existsZNode(String path, ZooKeeper zk) throws Exception {
return (zk.exists(path, false) != null) ? true : false;
}
/**
* 查看节点的子节点列表
* @throws Exception
*/
public static List<String> getChildrenZNodes(String path, ZooKeeper zk) throws Exception {
return zk.getChildren(path, false);
}
}
API的稍微复杂应用...
package com.zookeeper.zkutil;
import java.util.List;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;
/**
* 1、级联查看某节点下所有节点及节点值
* 2、删除一个节点,不管有有没有任何子节点
* 3、级联创建任意节点
* 4、清空子节点
*/
public class ZKUtil2 {
/**
* 级联查看某节点下所有节点及节点值
*/
public static void getAllZNodeAndValue(String path, ZooKeeper zk){
}
/**
* 删除一个节点,不管有有没有任何子节点
*/
public static boolean rmr(String path, ZooKeeper zk) throws Exception {
List<String> children = zk.getChildren(path, false);
if (children.size() == 0) {
// 删除节点
zk.delete(path, -1);
} else {
// 要删除这个有子节点的父节点,那么就需要先删除所有子节点,
// 然后再删除该父节点,完成对该节点的级联删除
// 删除有子节点的父节点下的所有子节点
for (String nodeName : children) {
rmr(path + "/" + nodeName, zk);
}
// 删除该父节点
rmr(path, zk);
}
return true;
}
/**
* 级联创建任意节点
*/
public static boolean createZNode(String znodePath, String data, ZooKeeper zk) throws Exception {
// 首先判断该节点是否存在,如果存在,则不创建, return false
if (zk.exists(znodePath, null) != null) {
return false;
} else {
try {
// 直接创建,如果抛异常,则捕捉异常,然后根据对应的异常如果是发现没有父节点,那么就创建父节点
zk.create(znodePath, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} catch (KeeperException e) {
// 截取父节点
String parentPath = znodePath.substring(0, znodePath.lastIndexOf("/"));
// 创建父节点
createZNode(parentPath, parentPath, zk);
try {
// 父节点创建好了之后,创建该节点
zk.create(znodePath, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} catch (KeeperException e1) {
e1.printStackTrace();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return true;
}
/**
* 清空子节点
*/
public static boolean clearChildNode(String znodePath, ZooKeeper zk) throws Exception {
List<String> children = zk.getChildren(znodePath, null);
for (String child : children) {
String childNode = znodePath + "/" + child;
if (zk.getChildren(childNode, null).size() != 0) {
clearChildNode(childNode, zk);
}
zk.delete(childNode, -1);
}
return true;
}
}
本文深入探讨了ZooKeeper的API使用方法,包括连接建立、节点操作、数据读写等核心功能,并提供了级联查看、删除和创建节点的高级应用实例。
174

被折叠的 条评论
为什么被折叠?



