Zookeeper Create, view, modify, and delete Znode from Java

Source: Internet
Author: User
Tags zookeeper zookeeper client

This chapter mainly introduces how to use zookeeper, in fact, through zkcli.cmd we can do some things:
Statement: Reference and transfer from the "http://www.blogjava.net/BucketLi/archive/2010/12/21/341268.html"

    • To operate zookeeper with Zkcli.cmd:

Enter the command under CMD under the D:\Work\zookeeper\zookeeper-3.4.8\bin path:zkcli.sh–server 127.0.0.1:2182, You can then view the Zkclient tool by using the Help command, which provides the following operations commands.

 [zk:127.0.0.1:2181 (CONNECTED) 10] helpzookeeper -server host:port cmd args stat path [watch] set path data [version] LS path [watch] Delquota [-n|-b] path LS2 path [watch] setacl path ACL Setquota -N |-b val path history redo Cmdno printwatches on  |off delete path [version] sync path listquota path RMR path get path [watch] Cr eate [-S] [-e] path data ACL addauth scheme auth quit Getacl Pat H Close Connect Host:port[zk:  127.0.0.1:2181 (CONNECTED)] 

LS (view current node data),
LS2 (view current node data and see the number of updates, etc.),
Create (creates a node),
Get (get a node that contains data and the number of updates),
Set (Modify node)
Delete (remove a node)

Through the above command practice, it is found that zookeeper uses a similar file system tree structure, the data can be hung on a node, the node can be censored. We also found that when a node is changed, the surviving machines in the cluster are updated to the same data (which is found when the data is viewed through Zkcli.cmd connection to any zkserver).

    • Zookeeper The data storage structure

Using the LS/{directory} command to view the ZK data storage structure, it can be found that its data structure is somewhat like the operating system's file structure, as shown in the structure:

(1) Each node is called the Znode in zookeeper, and it has a unique path identifier, such as the/server2 node identity is/app3/server2
(2) Znode can have sub-znode, and Znode can save data, but nodes of ephemeral type cannot have child nodes
(3) The data in the Znode can have more than one version, such as a path to have multiple versions of the data, then query the path of the data will need to bring the version.
(4) Znode can be a temporary node, once created this Znode client and the server lost contact, the Znode will be automatically deleted, Zookeeper client and server communication using long connection, each client and server through the heartbeat to maintain connectivity, this connection status is called Session, if Znode is a temporary node, this session expires, Znode also deleted
(5) Znode directory name can be automatically numbered, such as APP1 already exist, and then created, will be automatically named as App2
(6) znode can be monitored, including changes in the data stored in this directory node, changes in sub-node directories, and so on, once the change can notify the settings monitoring client, this function is zookeeper for the application of the most important features, through this feature can be implemented by the functions including the centralized management of the configuration, Cluster management, distributed locks and so on.

    • Using Java to implement Zkciient connection Zkserver:

1) using eclipse as the IDE for development testing, build a Java project from Maven

MAVEN introduces the Zookeeper jar:

        <Dependency>            <groupId>Org.apache.zookeeper</groupId>            <Artifactid>Zookeeper</Artifactid>            <version>3.4.8</version>        </Dependency>

2) write the test code:

1  Packagecom.dx.zktest;2 3 Importjava.util.List;4 Importjava.io.IOException;5 6 ImportOrg.apache.zookeeper.CreateMode;7 Importorg.apache.zookeeper.KeeperException;8 Importorg.apache.zookeeper.WatchedEvent;9 ImportOrg.apache.zookeeper.Watcher;Ten ImportOrg.apache.zookeeper.ZooDefs.Ids; One ImportOrg.apache.zookeeper.ZooKeeper; A  - /** - * Hello world! the  * -  */ -  Public classzkclient { -      Public Static voidMain (string[] args)throwsIOException, Keeperexception, interruptedexception { +         //Create a Zookeeper instance, the first parameter is the destination server address and port, the second parameter is the session time-out time, the third is the callback method when the node changes -ZooKeeper ZK =NewZooKeeper ("127.0.0.1:2181", 500000,NewWatcher () { +             //monitor all events that are triggered A              Public voidprocess (Watchedevent event) { at                 //dosomething -System.out.println ("Watcher event fire ..."); -             } -         }); -  -         //Create a node root, the data is MyData, do not have ACL permission control, the node is permanent (that is, the client shutdown will not disappear) inZk.create ("/root", "MyData". GetBytes (), Ids.open_acl_unsafe, createmode.persistent); -  to         //Create a Childone znode under root, the data is Childone, do not control ACL permissions, the node is permanent +Zk.create ("/root/childone", "Childone". GetBytes (), Ids.open_acl_unsafe, createmode.persistent); -  the         //gets the name of the child node under the/root node and returns the List<string> *list<string> Rootitems = Zk.getchildren ("/root",true); $System.out.println ("Print/root ...");Panax Notoginseng          for(String item:rootitems) { - System.out.println (item); the         } +          A         //get the data under the/root/childone node and return to byte[] the         byte[] bytes = Zk.getdata ("/root/childone",true,NULL); +System.out.println (NewString (bytes)); -  $         //modify the data under Node/root/childone, the third parameter is version, if 1, that will ignore the modified version of the data, directly change $Zk.setdata ("/root/childone", "Childonemodify". GetBytes (),-1); -  -         //Delete/root/childone This node, the second parameter is version, 1 words are deleted directly, ignoring version theZk.delete ("/root/childone", 1); - Wuyi         //Close Session the zk.close (); -     } Wu}

Monitor server node changes using Zkcli.cmd:

Zookeeper Create, view, modify, and delete Znode from 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.