Java Invoke zookeeper API Demo__api

Source: Internet
Author: User
Tags zookeeper

Java invoke the Zookeeper API demo as follows, the zookeeper of the additions and deletions to check and listen to the example

Note:

1. Using the Thread.Sleep () method is to prevent the implementation of the Java program after the completion of the execution, resulting in no monitoring changes, where the GetChildren () monitoring equivalent to zookeeper under the shell of the LS path watch the same effect.

The 2.zookeeper listener is only executed once, and if the setting is triggered, the next time it is not triggered, it can be triggered again in the process method, so that the listener will always exist.

3.zookeeper to delete the node, can only delete the leaf node, it does not allow us to delete nodes under the node has child nodes, if you need to delete the non-leaf node and the node below it, this need to use a recursive way to delete.

Package com.lijie.zk1;

Import java.util.List;
Import Org.apache.zookeeper.CreateMode;
Import org.apache.zookeeper.WatchedEvent;
Import Org.apache.zookeeper.Watcher;
Import Org.apache.zookeeper.ZooDefs.Ids;
Import Org.apache.zookeeper.ZooKeeper;

Import Org.apache.zookeeper.data.Stat; /** * * * @author Lijie * */public class Zksimple {private static final String connectstring = "Hadoop01:2181,ha

    Doop02:2181,hadoop03:2181 ";

    private static final int sessiontimeout = 2000;

    private static Zookeeper ZK = null;

        public static void Main (string[] args) throws Exception {//Call create//create ();

        Gets the child node//getchildren ();

        Determine whether there are//isexist ();

        Obtain Znode data//getdata ();

        Delete//delete ();
    Modify SetData ();  /** * Get Zookeeper instance * @return * @throws Exception/public static zookeeper Getzookeeper () Throws Exception {ZK = new Zookeeper (connEctstring, Sessiontimeout, New Watcher () {@Override public void process (Watchedevent event) {

                The callback function System.out.println ("event type" + event.gettype () + ", path" + Event.getpath ()) after receiving the watch notification;
                Because the listener listens only once, this can be monitored all the time and only listens to the "/" directory try {zk.getchildren ("/", true); catch (Exception e) {//TODO auto-generated catch block E.printsta
                Cktrace ();
        }
            }
        });
    return to ZK; /** * Create Data * @throws Exception */public static void Create () throws Exception {Zookeep
        Er ZK = getzookeeper (); Create a node, return to create a good path, and upload the data can be any type, need to convert to byte[]//Parameter 1 path, parameter 2 content, parameter 3 permission, parameter 4 type String Znodepath = Zk.create ("/
        MyTest "," Hello Zookeeper ". GetBytes (), Ids.open_acl_unsafe, createmode.ephemeral);
    SYSTEM.OUT.PRINTLN ("The path returned is:" + Znodepath); }

    /** * To determine if Znode exists * @throws Exception/public static void Isexist () throws Exception {Zookeepe
        R ZK = Getzookeeper ();
        Stat exists = zk.exists ("/lijie", false);
        if (exists = = null) {SYSTEM.OUT.PRINTLN ("does not exist");
        else {System.out.println ("exist");
        /** * Get child node * @throws Exception/public static void GetChildren () throws Exception {
        Zookeeper ZK = Getzookeeper ();
        Gets the child node list<string> children = Zk.getchildren ("/", true);
        for (string string:children) {System.out.println ("child node:" + string);
    //block, Test listener, and only Monitor "/" Directory Thread.Sleep (Long.max_value); /** * Get znode Data * @throws Exception * * public static void GetData () throws Exception {Z
        Ookeeper ZK = Getzookeeper ();
        byte[] data = Zk.getdata ("/lijie/test", False, New Stat ()); System.out.println (new String (data)); /** * Delete data * @throws Exception/public static void Delete () throws Exception {Zookeep
        Er ZK = getzookeeper ();
    The second parameter is version,-1 to delete all versions//it does not support deletion of nodes below and sub nodes, can only recursively delete zk.delete ("/hehe",-1); /** * Modify Znode value * @throws Exception * * public static void SetData () throws Exception {Z

        Ookeeper ZK = Getzookeeper ();

        Modifies the value of the Znode zk.setdata ("/lijie", "Modify Data". GetBytes (),-1);

    Test whether to modify the success System.out.println (New String (Zk.getdata ("/lijie", false, null));
 }
}

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.