//Create a connection to the serverZooKeeper ZK =NewZooKeeper ("localhost:" +Client_port, Clientbase.connection_timeout,NewWatcher () {//monitor all events that are triggered Public voidprocess (Watchedevent event) {System.out.println ("The + event.gettype () +" event has been triggered! "); } }); //Create a folder nodeZk.create ("/testrootpath", "Testrootdata". GetBytes (), Ids.open_acl_unsafe, createmode.persistent); //Create a sub-folder nodeZk.create ("/testrootpath/testchildpathone", "Testchilddataone". GetBytes (), ids.open_acl_unsafe,createmode.persistent); System.out.println (NewString (Zk.getdata ("/testrootpath",false,NULL))); //Remove subfolders Node ListSystem.out.println (Zk.getchildren ("/testrootpath",true)); //Changing subfolder node dataZk.setdata ("/testrootpath/testchildpathone", "Modifychilddataone". GetBytes (),-1); System.out.println ("Folder node Status: [" +zk.exists ("/testrootpath",true)+"]"); //Create another sub-folder nodeZk.create ("/testrootpath/testchildpathtwo", "Testchilddatatwo". GetBytes (), ids.open_acl_unsafe,createmode.persistent); System.out.println (NewString (Zk.getdata ("/testrootpath/testchildpathtwo",true,NULL))); //To Delete a sub-folder nodeZk.delete ("/testrootpath/testchildpathtwo", 1); Zk.delete ("/testrootpath/testchildpathone",-1); //Delete parent folder nodeZk.delete ("/testrootpath", 1); //Close ConnectionZk.close ();
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
ZooKeeper Main Operation Demo sample