Native API for Zookeeper

Source: Internet
Author: User
Tags iterable session id wrapper zookeeper
The reason is called "native", is to and next talk of curator difference. The version described is also in 3.5.1.
The API has a total of 5 packages: Org.apache.zookeeper org.apache.zookeeper.client org.apache.zookeeper.data Org.apache.zookeeper.server Org.apache.zookeeper.server.quorum the last two are the ZK services used to apply the built-in standalone or quorum mode, I personally disagree with this usage or encourage the use of dedicated ZK clusters, so the two packets skip over. The remaining 3 bags, Org.apache.zookeeper is the big head, we first look at the simple, then look at the big head.
1 Org.apache.zookeeper.client There is only one class Fourletterwordmain under this package, which is used to execute the four-word command. The three constructors are simple: static string Send4letterword (string host, int port, string cmd), static string Send4letterword (string host, int port, string cmd, Boolean secure) static string Send4letterword (string host, int port, string cmd, boolean secure, int Timeout) to say a few words about the four-word command, these commands are for the client to check the ZK cluster state. Commonly used are the following: Conf: Lists the basic configuration parameters used by the ZK server Cons: Lists statistics for each connection on the ZK server Crst: Resets the counter for each connection to 0 dump: Lists the currently active session information and the time-out for those sessions. Can only run on leader Server Envi: List the parameters of the ZK server JVM mntr: List the ZK server statistics, more detailed than the STAT command, leader Server will also list the additional parameter information only for leader Ruok: Provides information on whether the ZK server is OK, just a simple connectivity Test stat: Provides the state information of the ZK server and the current connection information, as well as the role of the server (Leader,follower,observer), and the maximum known zxid. SRVR: Same as stat information, just omit connection information WCHC: Lists the details of the monitoring point set by the ZK server, grouped by session WCHP: Lists the details of the monitoring point set by the ZK server, according to the Znode group Wchs: Lists brief information about the monitoring point set by the ZK server
2 Org.apache.zookeeper.data There are a total of 4 classes under this package: ACL Id Stat statpersisted As the name implies, the package is a number of data packaging class, although there are many methods under these classes can be adjusted, but the basic is get/set, meaning The meaning is more obvious, skipping over.
3 Org.apache.zookeeper the interface under the package: AsyncCallback asynccallback.aclcallback asynccallback.children2callback Asy Nccallback.childrencallback Asynccallback.create2callback Asynccallback.datacallback AsyncCallback.MultiCallba CK asynccallback.statcallback Asynccallback.stringcallback asynccallback.voidcallback KeeperException.Code Deprecated Watcher Watcher.event zoodefs.ids Zoodefs.opcode zoodefs.perms where the AsyncCallback interface is used for asynchronous operations callback function, and the asynccallback.* interface is a concrete implementation of the interface. Another important interface is the watcher. The rest is used to define constants or parameters, not important.
The class under the package: Serveradminclient watchedevent zoodefs ZooKeeper Zookeepermain wherein, Serveradminclient provides a four-word command for the encapsulation of the client, not important. Watchedevent is used for watcher handling events, which are mentioned in watcher. Zoodefs is not important for defining constants and parameters. Zookeeper is the main class of the client class, which is very important. Zookeepermain provides a command-line client implementation that is useful in some scenarios and is generally important.
3.1 Asynccallback.datacallback take this interface as an example to look at the use of asynccallback.*, the rest are similar. There is only one method that the interface needs to implement to handle the data returned by an asynchronous request: void Processresult (int rc, String path, Object ctx, byte[] data, stat stat) Several parameters have the following meanings: RC : The return code of the call, if the success is KeeperException.Code.OK, if the exception is a variety of exception code path: to the asynchronous operation of the Znode namespace path, to the data interface, it is necessary to obtain the data Znode path CTX: The context object passed to the asynchronous operation, of course, does not pass data: Here is the byte array returned after getting the data. For other types of asynccallback.* interfaces with the same name method, the parameter type of this item may change, such as create2callback in the argument is a string name,children2callback the parameter is list< String> Children Stat: The data for this path. For other types of asynccallback.* interfaces with the same name method, the parameter may not exist
3.2 Watcher The method that the interface needs to implement is also only one, for processing when the watcher is triggered: the void process (Watchedevent event) parameter has only one, but the Watchedevent class has the following methods, which is worth a said: String GetPath (): Gets the namespace path where the event occurred Watcher.Event.KeeperState GetState (): Used to describe the connection state between the client and the ZK cluster when the event occurred, with the following values, authfailed, Connectedreadonly,disconnected,expired,saslauthenticated,syncconnected. The meaning is more obvious, the client code can do different processing of different states Watcher.Event.EventType GetType (): Used to describe the type of event, there are several values, childwatchremoved, Datawatchremoved,nodechildrenchanged,nodecreated,nodedatachanged,nodedeleted,none.    The first few chapters on the watcher mechanism of ZK have introduced the vast majority of event types Org.apache.zookeeper.proto.WatcherEvent getwrapper (): Used to serialize Watchedevent String ToString (): Meaning clearly
3.3 ZooKeeper This class has 6 constructors, but the main difference is whether it is read-only, whether the connection session and password are specified, but the difference is small: ZooKeeper (String connectstring, int sessiontimeout , Watcher Watcher) ZooKeeper (String connectstring, int sessiontimeout, Watcher Watcher, Boolean canbereadonly) ZooKeeper ( String connectstring, int sessiontimeout, Watcher Watcher, Boolean canbereadonly, Org.apache.zookeeper.client.HostProvider ahostprovider) Zookeeper (String connectstring, int sessiontimeout, watcher Watcher, Long SessionId, byte[] sessionpasswd) ZooKeeper (String connectstring, int sessiontimeout, Watcher Watcher, long s Essionid, byte[] sessionpasswd, Boolean canbereadonly) ZooKeeper (String connectstring, int sessiontimeout, watcher Watcher, Long SessionId, byte[] sessionpasswd, Boolean canbereadonly, Org.apache.zookeeper.client.HostProvider Ahostprovider)
    The following methods are more, but are not common, the main methods are synchronous and asynchronous two versions, so look at a lot: void    addauthinfo (String scheme, byte[] auth) : Call the method under the Clientcnxn class to add authentication information for this connection thread void    close (): Close this client connection string    Create (String path, byte[] data, List<acl> ACL, Createmode createmode): Synchronous Building Method void    Create (String path, byte[] data, list<acl> a CL, Createmode Createmode, Asynccallback.create2callback CB, Object CTX): Asynchronous Build Directory Method void    Create (String path, b yte[] Data, list<acl> ACL, Createmode createmode, Asynccallback.stringcallback CB, Object CTX): Another asynchronous way to build a directory string& nbsp   Create (String path, byte[] data, list<acl> ACL, Createmode createmode, stat stat): Synchronize the Build directory method, and return the directory statistics by the way void& nbsp   Delete (string path, int version): Synchronized Delete directory method void    Delete (string path, int version, asynccallback.voidcal Lback CB, Object CTX): Asynchronous Delete Directory method stat    exists (String path, Boolean watch): Synchronous inspection Directory exists method, if watch is set to True, Use the default Watchmanager to set a watcher void for this connection    exists (String path, Boolean watch, Asynccallback.statcallback CB, Object CTX): Asynchronous version of previous method stat    E Xists (string path, Watcher Watcher): Synchronous validation of the directory exists method, and the previous method is different, with the specified watcher implementation to set watcher void    exists (string Path, Watcher Watcher, Asynccallback.statcallback CB, Object CTX): Asynchronous version of the previous method list<acl>    Getacl (String Path, stat stat): Gets the ACL information for the directory, if stat is not empty, the returned statistics will be put into stat (the following stat parameter is the meaning, no longer repeat) void    getacl (String path, Stat Stat, Asynccallback.aclcallback CB, Object CTX): Asynchronous version of the previous method list<string>    GetChildren (String path , Boolean Watch): Synchronized Get subdirectory method, if watch is set to True, use the default Watchmanager to set a watcher void    GetChildren for this connection (String Path, Boolean watch, Asynccallback.children2callback CB, Object CTX): Asynchronous version of the previous method void    GetChildren (String Path, Boolean watch, Asynccallback.childrencallback CB, Object CTX): Another asynchronous version of the last method list<string>    GetChildren (String path, Boolean watch, stat stat): Synchronized Get subdirectory method List<string>    GetChildren (String path, Watcher Watcher): Synchronous Get subdirectory method, set watcher watcher with the specified void  implementation   GetChildren (String path, Watcher Watcher, Asynccallback.children2callback CB, Object CTX): Asynchronous version of the previous method void  & nbsp GetChildren (String path, Watcher Watcher, Asynccallback.childrencallback CB, Object CTX): Another asynchronous version of the previous method list<string >    GetChildren (String path, Watcher Watcher, stat stat): Synchronized Get subdirectory method void    GetConfig (boolean wat CH, asynccallback.datacallback CB, Object CTX): Asynchronous version of the next method byte[]    GetConfig (Boolean watch, stat stat) : Gets the recently submitted configuration information synchronously, and if watch is set to True, use the default Watchmanager to set a watcher void    GetConfig for this connection (Watcher watcher, Asynccallback.datacallback CB, Object CTX): Asynchronous version of the next method byte[]    GetConfig (Watcher Watcher, stat stat) : Gets the recently submitted configuration information synchronously, sets watcher void    GetData with the specified watcher implementation (String path, Boolean watch, Asynccallback.datacallback CB, Object CTX): Asynchronous version of the next method byte[]    GetData (String PatH, Boolean watch, stat stat): Synchronize the method of getting directory data, if watch is set to True, use the default Watchmanager to set a watcher void    getData for this connection ( String path, Watcher Watcher, Asynccallback.datacallback CB, Object CTX): Asynchronous version of the next method byte[]    GetData (String pa Th, Watcher Watcher, stat Stat): Synchronous method of acquiring directory data, set watcher with the specified watcher implementation org.apache.zookeeper.client.zookeepersaslclient    Getsaslclient (): Call the method under the Clientcnxn class to get a SASL authenticated client, Useful when ZK cluster has Kerberos authentication long    GetSessionID (): Get session ID byte[]    GETSESSIONPASSWD (): Get the session password int    Getsessiontimeout (): Get timeout for session zookeeper.states    GetState (): Get the status of this connection org.apache.zookeeper.testable    gettestable (): Get zookeepertestable encapsulation of this connection, no use list< org.apache.zookeeper.opresult>    multi (iterable<org.apache.zookeeper.op> OPS): Atomically performs multi-step ZK operation void     multi (iterable<org.apache.zookeeper.op> OPS, Asynccallback.multicallback CB, Object CTX) : Asynchronous version of the previous method void    Reconfig (LIST&LT String> joiningservers, list<string> leavingservers, list<string> newmembers, Long fromConfig, Asynccallback.datacallback CB, Object CTX): Asynchronous version of the next method byte[]    Reconfig (list<string> joiningservers, List<string> leavingservers, list<string> newmembers, long fromconfig, stat stat): Add/Remove more than one server in the current cluster, The list of servers is list<string> void    reconfig (String joiningservers, String leavingservers, String newmembers, Long Fromconfig, Asynccallback.datacallback CB, Object CTX): Asynchronous version of the next method byte[]    Reconfig (String Joiningservers, String leavingservers, String newmembers, long fromconfig, stat stat): Adds/Removes more than one server in the current cluster, The list of servers is a comma-delimited string void    register (Watcher Watcher): Specifies a default watcher for the current connection, The default watcher generated by the Watchmanager will be replaced when constructed void    removeallwatches (String path, Watcher.watchertype Watchertype, Boolean local): Removes all watcher of the specified type for the specified path, and allows the client to remove watcher locally if the client and ZK server are not connected if local is set to True void    Removeallwatches (String path, WaTcher. Watchertype Watchertype, Boolean local, Asynccallback.voidcallback CB, Object CTX): Asynchronous version of previous method void    Removewatches (String path, Watcher Watcher, Watcher.watchertype Watchertype, Boolean local): Removes a specified type of watcher on the specified path void    removewatches (String path, Watcher Watcher, Watcher.watchertype Watchertype, Boolean local, Asynccallback.voidcallback CB, Object CTX): Asynchronous version of the previous method stat    SetACL (String path, list<acl> ACL, int vers ION): Set ACL void    SETACL (String path, list<acl> ACL, int version, Asynccallback.statcallback CB, Object C TX): Asynchronous version of the previous method stat    SetData (string path, byte[] data, int version): Set data void    SetData (string path , byte[] data, int version, Asynccallback.statcallback CB, Object CTX): Asynchronous version of the previous method void    sync (String path, Asyn Ccallback.voidcallback CB, Object CTX): For the specified path, forces the server on which this connection resides and leader to synchronize information, asynchronously calls string    toString (): Nothing to say. org.apache.zookeeper.transaction    Transaction (): Returns a transaction builder,transaction type is multi operation of the thin wrapper void    updateserverlist (String connectstring) : Enable this client to update ZK's connection string
3.4 Zookeepermain This class can be seen as a wrapper for the Zookeeper class, only for command-line connection. There are two constructors, and the latter is directly constructed with the Zookeeper class, which is more commonly used. Zookeepermain (string[] args) zookeepermain (ZooKeeper zk) method not much: static Boolean Createquota (ZooKeeper ZK, String Pat H, long bytes, int numnodes): Sets the quota for a path to ZK, bytes represents the size limit of the stored data, numnodes the limit for the number of child nodes static Boolean Delquota (ZooKeeper ZK, String Path, Boolean bytes, Boolean numnodes): Delete quota, Bytes set to True for delete size limit, NumNodes set to True for delete child nodes limit void ExecuteLine (String line ): Execute one-line command static list<string> getcommands (): Returns a list of commands Boolean getprintwatches (): Returns a Boolean value that controls whether the message is printed when the watcher is triggered atic void Main (string[] args): The entry allows it to run the static void Printmessage (String msg) as a command line: Simply print out the MSG

These are the main interfaces and classes. Enumeration and description of exception types are omitted. The sample code is also not affixed, to see the code can refer to http://wujiu.iteye.com/blog/2207872, is a good example of asynchronous connection code. In short, to tune the API is still very easy, to understand the principle is the most difficult.

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.