Zookeeper build node, delete node for Java

Source: Internet
Author: User
Tags zookeeper

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.