ZooKeeper Combat (iv)-zookeeper API usage

Source: Internet
Author: User
Tags zookeeper

This article mainly describes how to use the Zookeeper official API to manipulate zookeeper. maven Dependency

<dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper< /artifactid>
            <version>3.4.8</version>
        </dependency>
ZooKeeper API Constructing Zookeeper Instances

We mainly use the Zookeeper class to access the zookeeper and perform a series of operations.

String hostport = "localhost:2181,localhost:2182,localhost:2183";
int sessiontimeout =;
String Path = "/test/app1";

ZooKeeper ZK = new ZooKeeper (Hostport, Sessiontimeout, New Watcher () {

            @Override public
            void Process ( Watchedevent event) {
                String path = Event.getpath ();
                System.out.println ("Watch:" +event.gettype () + ", Path:" +path);

            }
        });
New Znode Node
public void Create () {

        String data = "Hello world!";

        SYSTEM.OUT.PRINTLN ("Create Data:" +data);

        try {
            String result = zk.create (path, data.getbytes (), Ids.open_acl_unsafe, createmode.persistent);
            SYSTEM.OUT.PRINTLN (result);
        } catch (Keeperexception | Interruptedexception e) {
            e.printstacktrace ();
        }
    }
get znode node data
    public void Get () {

        String result = null;  
        try {  
            byte[] bytes = zk.getdata (path, NULL, NULL);  
            result = new String (bytes);  
       } catch (Exception e) {  
            e.printstacktrace ();
       }
       SYSTEM.OUT.PRINTLN ("Read data:" +result);
    }
set znode node data
public void Set () {

        String data = "Hello ricky!";

        SYSTEM.OUT.PRINTLN ("Set data:" +data);
        try {
            zk.setdata (path, data.getbytes (),-1);
        } catch (Keeperexception | Interruptedexception e) {
            e.printstacktrace ();
        }
    }
Delete Znode
public void Delete () {

        try {
            zk.delete (path,-1);
        } catch (Interruptedexception | Keeperexception e) {
            e.printstacktrace ();
        }
    }
Close ZooKeeper
public void Close () {

        try {
            zk.close ();
        } catch (Interruptedexception e) {
            e.printstacktrace ();
        }
    }

The complete code is as follows:

Package com.ricky.codelab.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 Zkcruddemo {private String Hostport = "localhost:2181,localhost:2182,localhost:2183";
    private int sessiontimeout = 3000;

    Private String Path = "/test/app1";

    Private ZooKeeper ZK;
        public static void Main (string[] args) {try {new Zkcruddemo (). Test ();
        } catch (IOException e) {e.printstacktrace (); 

            }} public void Test () throws ioexception{ZK = new ZooKeeper (Hostport, Sessiontimeout, New Watcher () { @Override public void Process (Watchedevent event) {String path = Event.getpath (
                ); System.out.println ("Watch:" +event.gettype () + ", Path:" +path);

}
        });
Create ();
Get ();
        Set ();

        Get ();
        Delete ();

        Get ();
    Close ();

        } public void Create () {String data = "Hello world!";

        SYSTEM.OUT.PRINTLN ("Create Data:" +data);
            try {String result = zk.create (path, data.getbytes (), Ids.open_acl_unsafe, createmode.persistent);
        SYSTEM.OUT.PRINTLN (result); } catch (Keeperexception |
        Interruptedexception e) {e.printstacktrace ();  
        }} public void Get () {String result = null;  
            try {byte[] bytes = zk.getdata (path, NULL, NULL);  
       result = new String (bytes);
       } catch (Exception e) {e.printstacktrace ();
    } System.out.println ("Get Data:" +result);

        } public void Set () {String data = "Hello ricky!";
        SYSTEM.OUT.PRINTLN ("Set data:" +data);
           try { Zk.setdata (Path, data.getbytes (),-1); } catch (Keeperexception |
        Interruptedexception e) {e.printstacktrace ();
        }} public void Delete () {try {zk.delete (path,-1); } catch (Interruptedexception |
        Keeperexception e) {e.printstacktrace ();
        }} public void Close () {try {zk.close ();
        } catch (Interruptedexception e) {e.printstacktrace ();
 }
    }
}


Reference:
https://zookeeper.apache.org/doc/trunk/javaExample.html
http://zookeeper.apache.org/doc/ r3.4.8/api/org/apache/zookeeper/zookeeper.html

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.