Basic operation of Zookeeper Znode

Source: Internet
Author: User

Basic operation of Zookeeper Znode


ZK's Client API,

  Create a given directory node  path,  and set it to data,createmode  identifies four forms of directory nodes,//  are  persistent: Persistent directory node, The data stored in this directory node is not lost;// persistent_sequential: Sequential AutoNumber directory node, which automatically adds  1 based on the number of nodes currently near, and then returns to the Directory section name that the client has successfully created; / ephemeral: Temporary directory node, once the client and server port that created this node is the  session  timeout, this node will be automatically deleted;// ephemeral_ Sequential: Temporary AutoNumber node String create (String path, byte data[], list<acl> acl,  createmode createmode) Void   create (string path, byte data[],  LIST&LT;ACL&GT;&NBSP;ACL,&NBSP;CREATEMODE&NBSP;CREATEMODE,&NBSP;STRINGCALLBACK&NBSP;CB,&NBSP;OBJECT&NBSP;CTX)/ /  delete  path  corresponding directory node,version  for  -1  can match any version,//  also deletes all data for this directory node Void delete ( string path, int version) Void delete (string path, int version,  VOIDCALLBACK&NBSP;CB,&NBSP;OBJECT&NBSP;CTX)//give  path  set data, you can specify the version number of this data, if  version  is  -1   can match any version stat setdata (String path, byte data[], int version) Void setdata (String path, byte &NBSP;DATA[],&NBSP;INT&NBSP;VERSION,&NBSP;STATCALLBACK&NBSP;CB,&NBSP;OBJECT&NBSP;CTX)//  to reset access rights to a directory node, It is important to note that the directory node permissions in  Zookeeper  do not have transitivity,//  the permissions of the parent directory node cannot be passed to the subdirectory node. The directory node  ACL  consists of two parts:perms  and  id. perms  has  all, READ, WRITE, CREATE, DELETE, admin  several//and  id  identifies the list of identities that access the directory nodes, by default there are the following two kinds:// Anyone_id_unsafe = new id ("World",  "anyone")   and  auth_ids = new id ( "Auth",  "")  //  indicates that anyone can access and the creator has access. Stat setacl (string path, list<acl> acl, int version) Void setACL ( String path, list<acl> acl, int version, statcallback cb, object &NBSP;CTX)//  determine if a  path  exists and set whether to monitor this directory node,//  here  watcher  is created  zookeeper The  watcher,exists method specified in the   instance also has an overloaded method,//  can specify a specific  watcherstat&nbsP;exists (String path, watcher watcher) stat exists (string path, boolean  Watch) void exists (string path, watcher watcher, statcallback cb, object &NBSP;CTX) void exists (STRING&NBSP;PATH,&NBSP;BOOLEAN&NBSP;WATCH&NBSP;&NBSP;,&NBSP;STATCALLBACK&NBSP;CB, &NBSP;OBJECT&NBSP;CTX)//  Gets the data stored by this  path  corresponding directory node, and information such as the version of the data can be specified by  stat ,//  You can also set whether to monitor the status of this directory node data byte[] getdata (String path, watcher watcher, stat stat) Byte[] getdata (String path, boolean watch  , stat stat) void    getdata (string path, watcher watcher, datacallback cb, object  CTX) Void   getdata (string path, boolean watch  , datacallback &NBSP;CB,&NBSP;OBJECT&NBSP;CTX)//  gets all sub-directory nodes under the specified  path ,//  same   The GetChildren method also has an overloaded method that can set the status of a specific  watcher  monitoring child node list<stRing> getchildren (String path, watcher watcher) List<string> getchildren ( string path, boolean watch  ) Void  getchildren (String path,  WATCHER&NBSP;WATCHER,&NBSP;CHILDRENCALLBACK&NBSP;CB,&NBSP;OBJECT&NBSP;CTX) Void  getchildren (String &NBSP;PATH,&NBSP;BOOLEAN&NBSP;WATCH&NBSP;&NBSP;,&NBSP;CHILDRENCALLBACK&NBSP;CB,&NBSP;OBJECT&NBSP;CTX) List< String> getchildren (String path, watcher watcher, stat stat) List<String > getchildren (String path, boolean watch  , stat stat) void  GetChildren (STRING&NBSP;PATH,&NBSP;WATCHER&NBSP;WATCHER,&NBSP;CHILDREN2CALLBACK&NBSP;CB,&NBSP;OBJECT&NBSP;CTX) Void getchildren (STRING&NBSP;PATH,&NBSP;BOOLEAN&NBSP;WATCH&NBSP;&NBSP;,&NBSP;CHILDREN2CALLBACK&NBSP;CB, &NBSP;OBJECT&NBSP;CTX)

Description

Each one is synchronized or asynchronous, the addition of the specified watcher or the default watcher is divided into 4. The default watcher can be specified in ZooKeeper ZK = new ZooKeeper (serverlist, Sessiontimeout, watcher). If the Read method that contains Boolean watch passes in true, the default watcher is registered as watch for the event of interest. If False is passed in, no watch is registered.

There are several main createmode:

Persistent (persistent, will not disappear with the close/expire of the client session compared to ephemeral)

Persistent_sequential

Ephemeral (short, life cycle depends on the client session, the corresponding session Close/expire after its znode will disappear)

Ephemeral_sequential (sequential meaning order)


AsyncCallback asynchronous callback, depending on the type of operation, are divided into several categories:

Stringcallback

Voidcallback

Statcallback

Datacallback (GetData request)

Childrencallback

Children2callback


As shown below,

package com.usfot;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;import  Org.apache.zookeeper.zookeeper;import java.io.ioexception;/** * created by liyanxin  on 2015/3/17. */public class zookeeperdemo2 {    public  static void main (string args[])  throws IOException, KeeperException,  Interruptedexception {        zookeeper zk = new  zookeeper ("127.0.0.1:2181",  300000, new watcher ()  {             //  monitor all events that are triggered              public void process (watchedevent event)  {                system.out.println ("Status:"  + event.getstate ()  +  "| type:"  + event.gettype ()  +                           "| Wrapper: " + event.getwrapper ()  + " | Path: " + event.getpath ());             }         });        //  Create a directory node         zk.create ("/testrootpath",  "TestRootData". GetBytes (), zoodefs.ids.open_acl_unsafe,                 createmode.persistent);         //   Create a sub-directory node         zk.create ("/Testrootpath/testchildpathone ", " Testchilddataone ". GetBytes (),                 zoodefs.ids.open_acl_unsafe, createmode.persistent) ;         system.out.println (New string (Zk.getData ("/testRootPath" ,  false, null));        //  Remove the subdirectory node list          system.out.println (Zk.getchildren ("/testrootpath",  true));         //  modifying sub-directory node data          Zk.setdata ("/testrootpath/testchildpathone",  "Modifychilddataone". GetBytes (),  -1);    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Directory node Status: ["  + zk.exists ("/testrootpath",  true)  +  "]");        //  Create another sub-directory node          zk.Create ("/testrootpath/testchildpathtwo",  "Testchilddatatwo". GetBytes (),                 ZooDefs.Ids.OPEN_ACL_UNSAFE,  createmode.persistent);         system.out.println (New String ( Zk.getdata ("/testrootpath/testchildpathtwo",  true, null)));         //  Delete sub-directory node         zk.delete ("/testrootpath/ Testchildpathtwo ",  -1);         zk.delete ("/testRootPath/ Testchildpathone ",  -1);        //  Delete parent directory node          zk.delete ("/testrootpath",  -1);         //  Close Connection         zk.close ();     }}

Start the client, as follows,

[Zk: localhost:2181 (CONNECTED)  7] ls /[testrootpath, mynode, zookeeper, zk_ test0000000005, zk_test][zk: localhost:2181 (CONNECTED)  8] get / testrootpathtestrootdataczxid = 0x700000033ctime = tue mar 17 15:26:08  cst 2015mzxid = 0x700000033mtime = tue mar 17 15:26:08 cst  2015pZxid = 0x700000036cversion = 2dataVersion = 0aclVersion =  0ephemeralowner = 0x0datalength = 12numchildren = 2[zk: localhost:2181 ( CONNECTED)  10] get /testRootPath/testChildPathOnemodifyChildDataOnecZxid =  0x700000034ctime = tue mar 17 15:26:08 cst 2015mzxid =  0x700000035mtime = tue mar 17 15:26:09 cst 2015pzxid =  0x700000034cversion = 0dataversion = 1aclversion  = 0ephemeralowner = 0x0datalength = 18numchildren = 0[zk: localhost : 2181 (CONNECTED)  12] ls /testrootpath[testchildpathtwo, testchildpathone]

You can see the data that has been synced in ZK.

Reference: http://agapple.iteye.com/blog/1111377

http://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/


===============================end===============================


Basic operation of Zookeeper Znode

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.