- 级联创建任意节点
- 级联查看某节点下所有节点及节点值
- 级联删除一个节点
- 清空子节点
1. 级联创建任意节点
/**
* @Description: 编程思维训练:1.级联创建任意节点
* @author Jed
* @date 2017年12月19日
*/
public class ZookeeperExerciseTest1 {
private static final String CONNECT_STRING = "hadoop01:2181,hadoop02:2181,hadoop03:2181,hadoop04:2181";
private static final int SESSION_TIMEOUT = 5000;
public static void main(String[] args) throws Exception {
ZooKeeper zk = new ZooKeeper(CONNECT_STRING, SESSION_TIMEOUT, null);
createZNode("/a/b/c/d", "hello", zk);
zk.close();
}
/**
* 级联创建任意节点
*/
public static boolean createZNode(String path, String data, ZooKeeper zk) throws Exception {
// 首先判断该节点是否存在,如果存在,则不创建
if (zk.exists(path, null) != null) {
return false;
} else {
try {
// 直接创建,如果抛异常,则捕捉异常
// 根据对应的异常进行判断,如果是发现没有父节点,那么就创建父节点
zk.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} catch (KeeperException exception) {
// 截取父节点
String parentPath = path.substring(0, path.lastIndexOf("/"));
// 创建父节点
createZNode(parentPath, parentPath, zk);
try {
// 父节点创建好了之后,创建该节点
zk.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return true;
}
}
2. 级联查看某节点下所有节点及节点值
/**
* @Description: 编程思维训练:2.级联查看某节点下所有节点及节点值
* @author Jed
* @date 2017年12月19日
*/
public class ZookeeperExerciseTest2 {
private static final String CONNECT_STRING = "hadoop01:2181,hadoop02:2181,hadoop03:2181,hadoop04:2181";
private static final int SESSION_TIMEOUT = 5000;
public static void main(String[] args) throws Exception {
ZooKeeper zk = new ZooKeeper(CONNECT_STRING, SESSION_TIMEOUT, null);
showAllNodes("/a", zk);
zk.close();
}
/**
* 级联查看某节点下所有节点及节点值
*/
public static void showAllNodes(String path, ZooKeeper zk) throws Exception {
String data = new String(zk.getData(path, null, null));
System.out.println(path + "\t" + data);
List<String> children = zk.getChildren(path, null);
if (children.size() != 0) {
for(String node : children) {
showAllNodes(path + "/" + node, zk);
}
}
}
}
结果:
/a /a
/a/b /a/b
/a/b/c /a/b/c
/a/b/c/d hello
3. 级联删除一个节点
/**
* @Description: 编程思维训练:3.级联删除一个节点
* @author Jed
* @date 2017年12月19日
*/
public class ZookeeperExerciseTest3 {
private static final String CONNECT_STRING = "hadoop01:2181,hadoop02:2181,hadoop03:2181,hadoop04:2181";
private static final int SESSION_TIMEOUT = 5000;
public static void main(String[] args) throws Exception {
ZooKeeper zk = new ZooKeeper(CONNECT_STRING, SESSION_TIMEOUT, null);
rmr("/a", zk);
zk.close();
}
/**
* 级联删除一个节点
*/
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;
}
}
4. 清空子节点
/**
* @Description: 编程思维训练:4.清空子节点
* @author Jed
* @date 2017年12月19日
*/
public class ZookeeperExerciseTest4 {
private static final String CONNECT_STRING = "hadoop01:2181,hadoop02:2181,hadoop03:2181,hadoop04:2181";
private static final int SESSION_TIMEOUT = 5000;
public static void main(String[] args) throws Exception {
ZooKeeper zk = new ZooKeeper(CONNECT_STRING, SESSION_TIMEOUT, null);
clearChildNode("/a", zk);
zk.close();
}
/**
* 清空子节点
*/
public static boolean clearChildNode(String path, ZooKeeper zk) throws Exception {
List<String> children = zk.getChildren(path, null);
for (String child : children) {
String childNode = path + "/" + child;
if (zk.getChildren(childNode, null).size() != 0) {
clearChildNode(childNode, zk);
}
zk.delete(childNode, -1);
}
return true;
}
}