编程思维训练
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 }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/9034.html