小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

ZooKeeper學習之路 (六)ZooKeeper API的簡單使用(二)級聯(lián)刪除與創(chuàng)建

 HK123COM 2019-02-14

目錄

 

正文

編程思維訓練

1、級聯(lián)查看某節(jié)點下所有節(jié)點及節(jié)點值
2、刪除一個節(jié)點,不管有有沒有任何子節(jié)點
3、級聯(lián)創(chuàng)建任意節(jié)點
4、清空子節(jié)點

ZKTest.java

復制代碼
 1 public class ZKTest {
 2 
 3     private static final String CONNECT_STRING = "hadoop1,hadoop2,hadoop3";
 4     private static final int SESSION_TIMEOUT = 5000;
 5     private static ZooKeeper zk = null;
 6 
 7     public static void main(String[] args) throws Exception {
 8         zk = new ZooKeeper(CONNECT_STRING, SESSION_TIMEOUT, null);
 9         
10         //1、級聯(lián)查看某節(jié)點下所有節(jié)點及節(jié)點值 
11         /*Map<String, String> map = new HashMap<>();
12         Map<String, String> maps = ZKUtil.getChildNodeAndValue("/a",zk,map);
13         maps.forEach((key, value) -> System.out.println(key + "\t" + value));*/
14         
15         //2、刪除一個節(jié)點,不管有有沒有任何子節(jié)點 
16         /*boolean flag = ZKUtil.rmr("/x", zk);
17         if(flag) {
18             System.out.println("刪除成功!");
19         }else {
20             System.out.println("刪除失敗");
21         }*/
22         
23         
24         //3、級聯(lián)創(chuàng)建任意節(jié)點 
25         /*boolean createZNode = ZKUtil.createZNode("/x/y/z/bb", "bb", zk);
26         if(createZNode) {
27             System.out.println("創(chuàng)建成功!");
28         }else {
29             System.out.println("創(chuàng)建失敗");
30         }*/
31         
32         
33         //4、清空子節(jié)點
34         boolean clearChildNode = ZKUtil.clearChildNode("/x", zk);
35         if(clearChildNode) {
36             System.out.println("刪除成功!");
37         }else {
38             System.out.println("刪除失敗");
39         }
40         
41     
42         zk.close();
43     }
44 
45     
46 }
復制代碼

ZKUtil.java

復制代碼
  1 public class ZKUtil {
  2     
  3     private ZooKeeper zk;
  4     
  5     public ZKUtil() {}
  6 
  7     public ZooKeeper getZk() {
  8         return zk;
  9     }
 10 
 11     public void setZk(ZooKeeper zk) {
 12         this.zk = zk;
 13     }
 14 
 15     /**
 16      * 級聯(lián)查看某節(jié)點下所有節(jié)點及節(jié)點值
 17      * @throws Exception 
 18      * @throws KeeperException 
 19      */
 20     public static Map<String, String> getChildNodeAndValue(String path,ZooKeeper zk,Map<String, String> map) throws Exception{
 21         
 22         //看看傳入的節(jié)點是否存在
 23         if (zk.exists(path, false) != null) {
 24             //存在的話將該節(jié)點的數(shù)據(jù)存放到map中,key是絕對路徑,value是存放的數(shù)據(jù)
 25             map.put(path, new String(zk.getData(path, false, null)));
 26             //查看該節(jié)點下是否還有子節(jié)點
 27             List<String> list = zk.getChildren(path, false);
 28             if (list.size() != 0) {
 29                 //遍歷子節(jié)點,遞歸調(diào)用自身的方法
 30                 for (String child : list) {
 31                     getChildNodeAndValue( path + "/" + child,zk,map);
 32                 }
 33             }
 34         }
 35         
 36         return map;
 37     }
 38 
 39     /**
 40      * 刪除一個節(jié)點,不管有有沒有任何子節(jié)點
 41      */
 42     public static boolean rmr(String path, ZooKeeper zk) throws Exception {
 43         //看看傳入的節(jié)點是否存在
 44         if((zk.exists(path, false)) != null) {
 45             //查看該節(jié)點下是否還有子節(jié)點
 46             List<String> children = zk.getChildren(path, false);
 47             //如果沒有子節(jié)點,直接刪除當前節(jié)點
 48             if(children.size() == 0) {
 49                 zk.delete(path, -1);
 50             }else {
 51                 //如果有子節(jié)點,則先遍歷刪除子節(jié)點
 52                 for(String child : children) {
 53                     rmr(path+"/"+child,zk);
 54                 }
 55                 //刪除子節(jié)點之后再刪除之前子節(jié)點的父節(jié)點
 56                 rmr(path,zk);
 57             }
 58             return true;
 59         }else {
 60             //如果傳入的路徑不存在直接返回不存在
 61             System.out.println(path+" not exist");
 62             return false;
 63         }
 64         
 65         
 66     
 67     }
 68 
 69     /**
 70      * 級聯(lián)創(chuàng)建任意節(jié)點
 71      * create znodePath data
 72      * create /a/b/c/xx 'xx'
 73      * @throws Exception 
 74      * @throws KeeperException 
 75      
 76      */
 77     public static boolean createZNode(String znodePath, String data, ZooKeeper zk) throws Exception{
 78         
 79         //看看要創(chuàng)建的節(jié)點是否存在
 80         if((zk.exists(znodePath, false)) != null) {
 81             return false;
 82         }else {        
 83             //獲取父路徑
 84             String parentPath = znodePath.substring(0, znodePath.lastIndexOf("/"));
 85             //如果父路徑的長度大于0,則先創(chuàng)建父路徑,再創(chuàng)建子路徑
 86             if(parentPath.length() > 0) {
 87                 createZNode(parentPath, data, zk);
 88                 zk.create(znodePath, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
 89             }else {
 90                 //如果父路徑的長度=0,則直接創(chuàng)建子路徑
 91                 zk.create(znodePath, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
 92             }
 93             return true;
 94         }    
 95     }
 96 
 97     /**
 98      * 清空子節(jié)點
 99      */
100     public static boolean clearChildNode(String znodePath, ZooKeeper zk) throws Exception {
101         
102         List<String> children = zk.getChildren(znodePath, false);
103         
104         for (String child : children) {    
105             String childNode = znodePath + "/" + child;    
106             if (zk.getChildren(childNode, null).size() != 0) {    
107                 clearChildNode(childNode, zk);    
108             }    
109             zk.delete(childNode, -1);    
110         }    
111 
112         return true;
113     }
114 }
復制代碼

 

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多