編程思維訓練
1、級聯查看某節點下所有節點及節點值
2、刪除一個節點,不管有有沒有任何子節點
3、級聯創建任意節點
4、清空子節點
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、級聯查看某節點下所有節點及節點值 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、刪除一個節點,不管有有沒有任何子節點 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、級聯創建任意節點 25 /*boolean createZNode = ZKUtil.createZNode("/x/y/z/bb", "bb", zk); 26 if(createZNode) { 27 System.out.println("創建成功!"); 28 }else { 29 System.out.println("創建失敗"); 30 }*/ 31 32 33 //4、清空子節點 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 * 級聯查看某節點下所有節點及節點值 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 //看看傳入的節點是否存在 23 if (zk.exists(path, false) != null) { 24 //存在的話將該節點的數據存放到map中,key是絕對路徑,value是存放的數據 25 map.put(path, new String(zk.getData(path, false, null))); 26 //查看該節點下是否還有子節點 27 List<String> list = zk.getChildren(path, false); 28 if (list.size() != 0) { 29 //遍歷子節點,遞歸調用自身的方法 30 for (String child : list) { 31 getChildNodeAndValue( path + "/" + child,zk,map); 32 } 33 } 34 } 35 36 return map; 37 } 38 39 /** 40 * 刪除一個節點,不管有有沒有任何子節點 41 */ 42 public static boolean rmr(String path, ZooKeeper zk) throws Exception { 43 //看看傳入的節點是否存在 44 if((zk.exists(path, false)) != null) { 45 //查看該節點下是否還有子節點 46 List<String> children = zk.getChildren(path, false); 47 //如果沒有子節點,直接刪除當前節點 48 if(children.size() == 0) { 49 zk.delete(path, -1); 50 }else { 51 //如果有子節點,則先遍歷刪除子節點 52 for(String child : children) { 53 rmr(path+"/"+child,zk); 54 } 55 //刪除子節點之后再刪除之前子節點的父節點 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 * 級聯創建任意節點 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 //看看要創建的節點是否存在 80 if((zk.exists(znodePath, false)) != null) { 81 return false; 82 }else { 83 //獲取父路徑 84 String parentPath = znodePath.substring(0, znodePath.lastIndexOf("/")); 85 //如果父路徑的長度大於0,則先創建父路徑,再創建子路徑 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,則直接創建子路徑 91 zk.create(znodePath, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 92 } 93 return true; 94 } 95 } 96 97 /** 98 * 清空子節點 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 }