ZooKeeper级联删除javaAPI

来源:互联网 发布:菜鸟教程vue.js 编辑:程序博客网 时间:2024/06/11 11:19
package cn.ssy.zk.api;import java.util.List;import org.apache.zookeeper.ZooKeeper;public class ZKAPIDemoTest {    private static String connectString ="hadoop02:2181";    private static int sessionTimeout=5000;    public static void main(String[] args) throws Exception {        //创建节点        ZooKeeper zk = new ZooKeeper(connectString, sessionTimeout, null);        rmr("/zk01");        zk.close();    }    public static boolean rmr(String path) throws Exception{            ZooKeeper zk = new ZooKeeper(connectString, sessionTimeout, null);            String childrenPath = null;            List<String> children = zk.getChildren(path, false);            if (children.size()!=0){                for(String child: children){                    childrenPath=path+"/"+child;                    if(childrenPath.isEmpty()){                    zk.delete(childrenPath, -1);                    }else rmr(childrenPath);                    System.out.println(childrenPath);                }            }            zk.delete(path, -1);                        return false;    }}
原创粉丝点击