问题重现:
1、级联查看某节点下所有节点及节点值
2、删除一个节点,不管有有没有任何子节点
3、级联创建任意节点
4、清空子节点
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
/**
* 编程思维训练
* 1、级联查看某节点下所有节点及节点值
* 2、删除一个节点,不管有有没有任何子节点
* 3、级联创建任意节点
* 4、清空子节点
*/
public class ZKHomeWork {
private static String HOST_PATH = "hadoop02:2181,hadoop03:2181,hadoop04:2181";
private static int TIME_OUT = 5000;
static ZooKeeper zk = null;
static Map<String,String> map = new HashMap<>();
public static void main(String[] args) throws Exception {
zk = new ZooKeeper(HOST_PATH, TIME_OUT, null);
/*for (Map.Entry<String, String> entry : getChildNodeAndValue("/zk").entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}*/
/*if(rmr("/test/aaa/bbb",zk)){
System.out.println("删除成功");
}*/
/*if(createZNode("/test/aaa/bbb/ccc/ddd/eee","eee",zk)){
System.out.println("创建成功");
}*/
/*if(clearChildNode("/test/aaa",zk)){
System.out.println("删除成功");
}*/
}
/**
* 级联查看某节点下所有节点及节点值
* @param path
* @return
* @throws Exception
*/
public static Map<String, String> getChildNodeAndValue(String path) throws Exception{
List<String> sonNode = sonNode(path);
if(sonNode.size() > 0){
for (String child : sonNode) {
byte[] data = zk.getData(path+"/"+child, false, null);
map.put(path, new String(data));
getChildNodeAndValue(path+"/"+child);
}
}else{
byte[] data = zk.getData(path, false, null);
map.put(path, new String(data));
}
return map;
}
/**
* 查看某个节点下是不是有子节点
* @param path
* @return
* @throws Exception
*/
public static List<String> sonNode(String path) throws Exception{
List<String> children = zk.getChildren(path, null);
return children;
}
/**
* 删除一个节点,不管有有没有任何子节点
* @param path
* @param zk
* @return
* @throws Exception
*/
public static boolean rmr(String path, ZooKeeper zk) throws Exception {
List<String> sonNode = sonNode(path);
boolean flag = false;
String parentPath = "";
if(sonNode.size() > 0){
for (String child : sonNode) {
parentPath = path;
rmr(path+"/"+child,zk);
}
if(sonNode(parentPath).size() == 0){
zk.delete(path, -1);
}
}else{
zk.delete(path, -1);
flag = true;
}
return flag;
}
/**
* 级联创建任意节点
* @param znodePath
* @param data
* @param zk
* @return
* @throws Exception
*/
public static boolean createZNode(String znodePath, String data, ZooKeeper zk) throws Exception {
String[] paths = znodePath.split("/");
String path = "";
for (int i = 1; i < paths.length; i++) {
path = path +"/"+ paths[i];
Stat exists = zk.exists(path, null);
if(exists == null){
if(i == paths.length-1){
zk.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}else{
zk.create(path, "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
}
}
return true;
}
/**
* 清空子节点
* @param znodePath
* @param zk
* @return
* @throws Exception
*/
public static boolean clearChildNode(String znodePath, ZooKeeper zk) throws Exception {
String grandPath = znodePath;
List<String> sonNode = sonNode(znodePath);
boolean flag = false;
String parentPath = "";
if(sonNode.size() > 0){
for (String child : sonNode) {
parentPath = znodePath;
rmr(znodePath+"/"+child,zk);
}
if(!grandPath.equals(parentPath) && sonNode(parentPath).size() == 0){
zk.delete(znodePath, -1);
}
}else{
zk.delete(znodePath, -1);
flag = true;
}
return flag;
}
}