Source Address Https://github.com/Bellonor/myhadoop2.x/tree/master/myhadoop2.x/src/main/java/com/jamesfen/zookeeper
Each jar package depends on the pom file that is seen by Maven
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, Interruptedexce ption {//string zkserver = "192.168.58.13:2181"; ZooKeeper ZK = new ZooKeeper ("192.168.58.11:" + 2181, 3000,new watcher () {//monitor all triggered events public void process (Watchedevent ev ENT) {System.out.println ("has triggered" + event.gettype () + "event!) ");}});/ /Create a directory node Zk.create ("/panguoyuan-dir", "Panguoyuan-data". GetBytes (), Ids.open_acl_unsafe, createmode.persistent); /Create a subdirectory node zk.create ("/panguoyuan-dir/childone", "Childone". GetBytes (), Ids.open_acl_unsafe, createmode.persistent ); System.out.println (New String (Zk.getdata ("/panguoyuan-dir", false, null)));//Remove the subdirectory node list System.out.println ( Zk.getchildren("/panguoyuan-dir", true)); /Modify subdirectory node data zk.setdata ("/panguoyuan-dir/childone", "Modifydataone". GetBytes (),-1); SYSTEM.OUT.PRINTLN ("Directory node state: [" + zk.exists ("/panguoyuan-dir", True) + "]");//Create another subdirectory node 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)));//delete sub-directory node Zk.delete ("/ Panguoyuan-dir/childtwo ",-1); Zk.delete ("/panguoyuan-dir/childone ",-1);//delete Parent directory node Zk.delete ("/panguoyuan-dir ",-1) ;//close ZK connection Zk.close ();}}
Zookeeper build node, delete node for Java