首页 > 代码库 > zookeeper生成节点、删除节点 For Java
zookeeper生成节点、删除节点 For Java
源码地址https://github.com/Bellonor/myhadoop2.x/tree/master/myhadoop2.x/src/main/java/com/jamesfen/zookeeper
各个jar包依赖见maven 的pom文件
https://github.com/Bellonor/myhadoop2.x/tree/master/myhadoop2.x/src/main/java/com/jamesfen/zookeeper
package com.jamesfen.zookeeper; import java.io.IOException; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.ZooKeeper; public class ZkNodeOperation { public static void main(String[] args) throws IOException, KeeperException, InterruptedException { //String zkServer = "192.168.58.13:2181"; ZooKeeper zk = new ZooKeeper("192.168.58.11:" + 2181, 3000, new Watcher() { // 监控所有被触发的事件 public void process(WatchedEvent event) { System.out.println("已经触发了" + event.getType() + "事件!"); } }); // 创建一个目录节点 zk.create("/panguoyuan-dir", "panguoyuan-data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); // 创建一个子目录节点 zk.create("/panguoyuan-dir/childone", "childone".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); System.out.println(new String(zk .getData("/panguoyuan-dir", false, null))); // 取出子目录节点列表 System.out.println(zk.getChildren("/panguoyuan-dir", true)); // 修改子目录节点数据 zk.setData("/panguoyuan-dir/childone", "modifyDataOne".getBytes(), -1); System.out.println("目录节点状态:[" + zk.exists("/panguoyuan-dir", true) + "]"); // 创建另外一个子目录节点 zk.create("/panguoyuan-dir/childtwo", "childtwo".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); System.out.println(new String(zk.getData("/panguoyuan-dir/childtwo", true, null))); // 删除子目录节点 zk.delete("/panguoyuan-dir/childtwo", -1); zk.delete("/panguoyuan-dir/childone", -1); // 删除父目录节点 zk.delete("/panguoyuan-dir", -1); // 关闭zk连接 zk.close(); } }
zookeeper生成节点、删除节点 For Java
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。