ZooKeeper Series 4:zookeeper API Introduction and programming

Source: Internet
Author: User

Questions Guide:How many packages are included in the 1.ZooKeeper API? 2. How do I create an zookeeper application using the zookeeper API?




s) ZooKeeper API Introduction The ZooKeeper API contains a total of 5 packages, namely: Org.apache.zookeeper, Org.apache.zookeeper.data, Org.apache.zookeeper.server, Org.apache.zookeeper.server.quorum and Org.apache.zookeeper.server.upgrade. Where Org.apache.zookeeper contains the Zookeeper class, it weThe class file that is most commonly used when programming.

This class is ZooKeeperthe main class file for the client library. If you want to use the ZooKeeper service, the application must first create an ZooKeeper instance, which is the class you need to use. Once the client and the ZooKeeper service establish a connection, the ZooKeeper system will assign an ID value to this connection, and the client will periodically send a heartbeat to the server to maintain the session's connection. As long as the connection is valid, the client can invoke the ZooKeeper API to do the appropriate processing. It provides some of the main methods shown in Table 1: Table 1:zookeeper API Description
Function Describe
Create Create a node in the local directory tree
Delete Delete a node
Exists To test if a destination node exists locally
Get/set data Read/write data from the target node
Get/set ACL Get/Set Target node access control list information
Get children Retrieving a list on a child node
Sync Waiting for the data to be transferred

2) Use of the ZooKeeper API Here, the author is a simple example of how to use the ZooKeeper API to write your own application, see Listing 1: Code listing 1:zookeeper API usage

  1. Import java.io.IOException;
  2. Import Org.apache.zookeeper.CreateMode;
  3. Import org.apache.zookeeper.KeeperException;
  4. Import Org.apache.zookeeper.Watcher;
  5. Import Org.apache.zookeeper.ZooDefs.Ids;
  6. Import Org.apache.zookeeper.ZooKeeper;
  7. public class Demo {
  8. Session time-out, set to match system default time
  9. private static final int session_timeout=30000;
  10. Creating an ZooKeeper instance
  11. ZooKeeper ZK;
  12. Creating an Watcher instance
  13. Watcher Wh=new Watcher () {
  14. public void process (Org.apache.zookeeper.WatchedEvent event)
  15. {
  16. System.out.println (Event.tostring ());
  17. }
  18. };
  19. Initializing an ZooKeeper instance
  20. private void Createzkinstance () throws IOException
  21. {
  22. Zk=new ZooKeeper ("localhost:2181", demo. SESSION_TIMEOUT,THIS.WH);
  23. }
  24. private void Zkoperations () throws Ioexception,interruptedexception,keeperexception
  25. {
  26. System.out.println ("/n1. Create ZooKeeper node (ZNODE:ZOO2, Data: MyData2, Permissions: Open_acl_unsafe, node Type: persistent ");
  27. Zk.create ("/zoo2", "MyData2". GetBytes (), Ids.open_acl_unsafe, createmode.persistent);
  28. System.out.println ("/n2. See if the creation was successful: ");
  29. System.out.println (New String (Zk.getdata ("/zoo2", False,null));
  30. System.out.println ("/n3. Modify node Data ");
  31. Zk.setdata ("/zoo2", "shenlan211314". GetBytes (),-1);
  32. System.out.println ("/n4. See if the modification was successful: ");
  33. System.out.println (New String (Zk.getdata ("/zoo2", false, null)));
  34. System.out.println ("/n5. Delete Node ");
  35. Zk.delete ("/zoo2",-1);
  36. System.out.println ("/n6. See if the node is deleted: ");
  37. SYSTEM.OUT.PRINTLN ("Node state: [" +zk.exists ("/zoo2", false) + "]");
  38. }
  39. private void Zkclose () throws Interruptedexception
  40. {
  41. Zk.close ();
  42. }
  43. public static void Main (string[] args) throws Ioexception,interruptedexception,keeperexception {
  44. Demo Dm=new demo ();
  45. Dm.createzkinstance ();
  46. Dm. Zkoperations ();
  47. Dm. Zkclose ();
  48. }
  49. }
Copy Code

This class contains two primary ZooKeeper functions, createzkinstance () and Zkoperations (). where the Createzkinstance () function is responsible for initializing the ZooKeeper instance ZK. The ZooKeeper class has two constructors, which we initialize with "ZooKeeper (String connectstring, int sessiontimeout, Watcher watcher)." Therefore, we need to provide the initialization required, the connection string information, the session timeout time, and a watcher instance. 17 lines to 23 lines of code is a watcher instance constructed by the program that can output events that occur.

The Zkoperations () function is a series of operations that we define for a node. It includes: Creating ZooKeeper nodes (33 lines to 34 lines of code), viewing nodes (36 lines to 37 lines of code), modifying node data (39 lines to 40 lines of code), viewing modified node data (42 lines to 43 lines of code), deleting nodes (45 lines to 46 lines of code), checking See if the node exists (48 lines to 49 lines of code). Also, it is important to note that when creating a node, you need to provide the name, data, permissions, and node type of the node. Additionally, when you use the EXISTS function, a null value is returned if the node does not exist. For more detailed information about the ZooKeeper API, readers can view the ZooKeeper API documentation as follows:

    1. Http://hadoop.apache.org/zookeeper/docs/r3.3.1/api/index.html
Article turned from: http://www.aboutyun.com/thread-9311-1-1.html

ZooKeeper Series 4:zookeeper API Introduction and programming

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.