ZooKeeper-java client access-getting started

Source: Internet
Author: User

In the previous article, we briefly introduced ZooKeeper deployment and node management.


This section describes how to manage nodes through the java client.


I don't want to introduce them one by one. I believe you can understand them from the comments of the Code.

First, create a basic class for zookeeper, which contains the basic node management methods, as follows:

Package com. zktest. newtest; import java. io. IOException; import java. util. list; import java. util. concurrent. countDownLatch; import org. apache. zookeeper. createMode; import org. apache. zookeeper. keeperException; import org. apache. zookeeper. watchedEvent; import org. apache. zookeeper. watcher; import org. apache. zookeeper. watcher. event. keeperState; import org. apache. zookeeper. zooDefs. ids; import org. apache. zookeeper. zooKeeper; import org. apache. zookeeper. data. stat; public class BaseZookeeper implements Watcher {public ZooKeeper zooKeeper; private static final int SESSION_TIME_OUT = 2000; private CountDownLatch countDownLatch = new CountDownLatch (1 ); /*** connect zookeeper ** @ param host * @ throws IOException * @ throws handle */public void connectZookeeper (String host) throws IOException, InterruptedException {zooKeeper = new ZooKeeper (host, SESSION_TIME_OUT, this); countDownLatch. await (); System. out. println ("zookeeper connect OK");}/*** implements the watcher interface method. After the connection to zookeeper is successful, zookeeper notifies watcher through this method.
* If a successful connection event is received, countDown the thread to continue other tasks. * // @ Overridepublic void process (WatchedEvent event) {if (event. getState () = KeeperState. syncConnected) {System. out. println ("watcher received event"); countDownLatch. countDown () ;}}/*** create a node based on the path, and set the node data * @ param path * @ param data * @ return * @ throws KeeperException * @ throws InterruptedException */public String createNode (String path, byte [] data) throws KeeperException, interruptedException {return this. zooKeeper. create (path, data, Ids. OPEN_ACL_UNSAFE, CreateMode. PERSISTENT);}/*** obtain all child nodes by path ** @ param path * @ return * @ throws KeeperException * @ throws InterruptedException */public List GetChildren (String path) throws KeeperException, InterruptedException {return this. zooKeeper. getChildren (path, false);} public Stat setData (String path, byte [] data, int version) throws KeeperException, InterruptedException {return this. zooKeeper. setData (path, data, version );} /*** obtain node data by path ** @ param path * @ return * @ throws KeeperException * @ throws InterruptedException */public byte [] getData (Str Ing path) throws KeeperException, InterruptedException {return this. zooKeeper. getData (path, false, null );} /*** delete a node ** @ param path * @ param version * @ throws InterruptedException * @ throws KeeperException */public void deletNode (String path, int version) throws InterruptedException, keeperException {this. zooKeeper. delete (path, version);}/*** close the zookeeper connection ** @ throws InterruptedException */public v Oid closeConnect () throws InterruptedException {if (null! = ZooKeeper) {zooKeeper. close ();}}}

Create a client for testing. The Code is as follows:

Package com. spg. zktest. newtest; import java. io. IOException; import java. util. arrays; import java. util. list; import org. apache. zookeeper. keeperException; public class Client {/*** @ param args * @ throws InterruptedException * @ throws IOException * @ throws KeeperException */public static void main (String [] args) throws IOException, interruptedException, KeeperException {BaseZookeeper baseZookeeper = new BaseZookeeper (); String host = "192.168.183.128: 2181"; // connect zookeeperbaseZookeeper. connectZookeeper (host); System. out. println ("-------- connect zookeeper OK -----------"); // create a node byte [] data = {1, 2, 3, 4, 5}; String result = baseZookeeper. createNode ("/test", data); System. out. println (result); System. out. println ("-------- create node OK -----------"); // obtain the List of all nodes in a path
 
  
Children = baseZookeeper. getChildren ("/"); for (String child: children) {System. out. println (child);} System. out. println ("-------- get children OK -----------"); // obtain node Data byte [] nodeData = baseZookeeper. getData ("/test"); System. out. println (Arrays. toString (nodeData); System. out. println ("-------- get node data OK -----------"); // update node data = "test data ". getBytes (); baseZookeeper. setData ("/test", data, 0); System. out. println ("-------- set node data OK -----------"); nodeData = baseZookeeper. getData ("/test"); System. out. println (Arrays. toString (nodeData); System. out. println ("-------- get node new data OK -----------"); baseZookeeper. closeConnect (); System. out. println ("-------- close zookeeper OK -----------");}}
 


The result is as follows:

1: nodes under the root directory


2: view the test node Information



OK. The test result ends here.

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.