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
- Import java.io.IOException;
- Import Org.apache.zookeeper.CreateMode;
- Import org.apache.zookeeper.KeeperException;
- Import Org.apache.zookeeper.Watcher;
- Import Org.apache.zookeeper.ZooDefs.Ids;
- Import Org.apache.zookeeper.ZooKeeper;
- public class Demo {
- Session time-out, set to match system default time
- private static final int session_timeout=30000;
- Creating an ZooKeeper instance
- ZooKeeper ZK;
- Creating an Watcher instance
- Watcher Wh=new Watcher () {
- public void process (Org.apache.zookeeper.WatchedEvent event)
- {
- System.out.println (Event.tostring ());
- }
- };
- Initializing an ZooKeeper instance
- private void Createzkinstance () throws IOException
- {
- Zk=new ZooKeeper ("localhost:2181", demo. SESSION_TIMEOUT,THIS.WH);
- }
- private void Zkoperations () throws Ioexception,interruptedexception,keeperexception
- {
- System.out.println ("/n1. Create ZooKeeper node (ZNODE:ZOO2, Data: MyData2, Permissions: Open_acl_unsafe, node Type: persistent ");
- Zk.create ("/zoo2", "MyData2". GetBytes (), Ids.open_acl_unsafe, createmode.persistent);
- System.out.println ("/n2. See if the creation was successful: ");
- System.out.println (New String (Zk.getdata ("/zoo2", False,null));
- System.out.println ("/n3. Modify node Data ");
- Zk.setdata ("/zoo2", "shenlan211314". GetBytes (),-1);
- System.out.println ("/n4. See if the modification was successful: ");
- System.out.println (New String (Zk.getdata ("/zoo2", false, null)));
- System.out.println ("/n5. Delete Node ");
- Zk.delete ("/zoo2",-1);
- System.out.println ("/n6. See if the node is deleted: ");
- SYSTEM.OUT.PRINTLN ("Node state: [" +zk.exists ("/zoo2", false) + "]");
- }
- private void Zkclose () throws Interruptedexception
- {
- Zk.close ();
- }
- public static void Main (string[] args) throws Ioexception,interruptedexception,keeperexception {
- Demo Dm=new demo ();
- Dm.createzkinstance ();
- Dm. Zkoperations ();
- Dm. Zkclose ();
- }
- }
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:
- 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