Articles Catalogue
Use of Zookeeper03's Java API
Java program operation Zookeeper
1. Create java projects and import related jar packages
The main jar package is in the main directory
The dependent jar packages required by the project are available in the lib directory of zookeeper's decompressed files
maven project coordinates
<dependencies> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> <type>pom</type> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
2. Simple use of API
2.1 Configuring Zookeeper objects
ps: Make sure you set the timeout a little longer, otherwise the connection will succeed, but the operation may fail, and keep looking for no reason.
import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import org.junit.Before; import org.junit.Test; import java.io.IOException; /** * @author: cjw * @date 2019/8/8 10:33 * Functions: */ public class Test01 { // The server address of zookeeper // private String connectString = "192.168.197.131:2181,192.168.197.132:2181,192.168.197.133:2181"; private String connectString = "192.168.197.131:2181,192.168.197.132:2181"; // Connection timeout, long enough (must) private int sessionTimeout = 20000; private ZooKeeper zk = null; /** * Setting zookeeper object * @throws IOException */ @Before public void setZookeeper() throws IOException { zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() { /** * Event-triggered callback method */ @Override public void process(WatchedEvent event) { } }); System.out.println("---"+zk); } /** * Function: Test Connection Successful */ @Test public void Test(){ ZooKeeper.States state = zk.getState(); System.out.println(state.isAlive()); System.out.println(state.toString()); System.out.println(state.name()); } }
Test () run screenshot
Common API operations
Adding Nodes
/** * create Method parameters * First parameter path * The second parameter value bytes * Access control of the third parameter to the node * The type of the fourth parameter node is a temporary permanent serial number * @throws KeeperException * @throws InterruptedException */ @Test public void create() throws KeeperException, InterruptedException { String s = zk.create("/node5", "cjw".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); System.out.println(s); }
Access control to nodes
option | Meaning |
---|---|
ZooDefs.Ids.OPEN_ACL_UNSAFE | Open ACL is unsafe |
ZooDefs.Ids.ANYONE_ID_UNSAFE | Any ID is not secure |
ZooDefs.Ids.AUTH_IDS | Authentication id |
ZooDefs.Ids.CREATOR_ALL_ACL | Creator All ACL |
ZooDefs.Ids.READ_ACL_UNSAFE | It's not safe to read ACL |
Judging whether a node exists or not
/** * Judging whether a node exists or not * @throws InterruptedException * @throws KeeperException */ @Test public void exit() throws KeeperException, InterruptedException { // Setting true calls the listener in zk Stat exists = zk.exists("/node5", true); if(exists == null) { System.out.println("The node does not exist!"); } else { System.out.println("The node exists!"); System.out.println(exists.getDataLength()); } }
Getting child nodes
/** * Getting child nodes * @throws InterruptedException * @throws KeeperException */ @Test public void getchildrens() throws KeeperException, InterruptedException { List<String> children = zk.getChildren("/", true); for (int i = 0; i < children.size(); i++) { String s = children.get(i); System.out.println(s); } }
Get the content of the node
/** * Get the content of the node * @throws InterruptedException * @throws KeeperException */ @Test public void getData() throws KeeperException, InterruptedException { byte[] data = zk.getData("/node3", true, null); System.out.println(new String(data)); }
Modify node content
/** * Modify node content * version -1 Automatic Maintenance * @throws InterruptedException * @throws KeeperException */ @Test public void update() throws KeeperException, InterruptedException { Stat stat = zk.setData("/node3", "Relativity".getBytes(), -1); System.out.println(stat.getVersion()); }
Delete Nodes
/** * Delete Nodes * Non-empty nodes cannot be deleted * @throws InterruptedException * @throws KeeperException */ @Test public void remove() throws KeeperException, InterruptedException { zk.delete("/node3", 2); }
3. Use of listeners
The listener is a necessary parameter for some methods. It is enough to implement an internal class of Watcher interface.
Connect listeners
When creating a connection, you can customize a listener. Convenient for later use
/** * Setting zookeeper object * @throws IOException */ @Before public void setZookeeper() throws IOException { zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() { /** * Event-triggered callback method */ @Override public void process(WatchedEvent event) { } }); System.out.println("---"+zk); }
Getting child node listeners
When the number of nodes is changed, the listener is triggered.
/** * Listening events: Triggered when the number of child nodes changes (only once) */ @Test public void getchildrens2() throws KeeperException, InterruptedException { List<String> children = zk.getChildren("/", new Watcher() { @Override public void process(WatchedEvent event) { System.out.println("Change in the number of subnodes!"); } }); for (int i = 0; i < children.size(); i++) { String s = children.get(i); System.out.println(s); } Thread.sleep(Long.MAX_VALUE); }
A listener for getting node data
/** * Listening Event: Triggered when the node data sends a change (only once) */ @Test public void getdata() throws KeeperException, InterruptedException { byte[] data = zk.getData("/name2", new Watcher() { @Override public void process(WatchedEvent event) { System.out.println("Node data sends changes!"); } }, null); System.out.println(new String(data)); Thread.sleep(Long.MAX_VALUE); }
Pack Project
Pack Project Extraction Code: qxj7 Environment:idea