1.ZK Cluster Java Simple Operation 1.0 Package directory structure lib taken from Zookpeeper official compression package (based on zk3.4.5 test pass)
1.1 Abstractzookeeper.java
Package Com.ccy.zk;import Java.io.ioexception;import Java.util.concurrent.countdownlatch;import Org.apache.log4j.logger;import Org.apache.zookeeper.watchedevent;import Org.apache.zookeeper.watcher;import Org.apache.zookeeper.watcher.event.keeperstate;import org.apache.zookeeper.zookeeper;/** * * <p> * Title: Abstractzookeeper.java * Package COM.CCY.ZK * </p> * <p> * Description:todo * <p> * @author Tom.cai * @created 2015-4-13 pm 9:48:50 * @version V1.0 * */public class Abstractzookeeper implements watcher {protected Logger Logger = Logger.getlogger (Abstractzookeeper.class); Cache time private static final int session_time = 2000; protected ZooKeeper ZooKeeper; Protected Countdownlatch countdownlatch=new Countdownlatch (1); Connect ZK cluster public void Connect (String hosts) throws IOException, interruptedexception{zooKeeper = new Z Ookeeper (Hosts,session_time,this); Countdownlatch.await (); }//ZK handles @Override public void process (Watchedevent event) {if (Event.getstate () ==keepersta Te. syncconnected) {Countdownlatch.countdown (); }}//close cluster public void close () throws interruptedexception{Zookeeper.close (); } }
1.2 Zookeeperoperator.java (test Class)
Package Com.ccy.zk;import Java.util.list;import Org.apache.log4j.logger;import Org.apache.zookeeper.CreateMode; Import Org.apache.zookeeper.keeperexception;import Org.apache.zookeeper.zoodefs.ids;import org.apache.zookeeper.data.stat;/** * * <p> * Title:ZooKeeperOperator.java * Package COM.CCY.ZK * </p> * & lt;p> * Description:todo * <p> * @author tom.cai * @created 2015-4-13 pm 9:49:00 * @version V1.0 * */public CLA SS Zookeeperoperator extends Abstractzookeeper {private static Logger Logger = Logger.getlogger (zookeeperoperator.class ); /** * * Create Znode * */public void Create (String path,byte[] data) throws Keeperexception, interruptedexception{/** * This is used to create is a persistent node: persistent indicates that the node will not be deleted due to a break in the connection * ephemeral means the znode would be deleted upon the client's disconnect. */this.zooKeeper.create (path, data, Ids.open_acl_unsafe, createmode.persistent);} /** * * Get node information * */public void Getchild (String path) throws Keeperexception, interruptedexception{Try{list<string> ;List=this.zookeeper.getchildren (path, False), if (List.isEmpty ()) {Logger.info (path+ "no Nodes"); System.out.println (path+ "no Nodes");} Else{for (String child:list) {System.out.println ("node:" +child);}}} catch (Keeperexception.nonodeexception e) {//Todo:handle exception throw E; }}/** * * Get node Data * */public byte[] GetData (String path) throws Keeperexception, interruptedexception {return T His.zooKeeper.getData (path, false,null); }/** * Modify node Data */public Stat setDate (String path,byte[] data,int version) throws Keeperexception, Interruptedexception{re Turn this.zooKeeper.setData (path, data, version);} /** * Delete node */public void Deletenode (final String path,int version) throws Interruptedexception, keeperexception{This.zook Eeper.delete (path, version);} public static void Main (string[] args) {try {zookeeperoperator zkoperator = new Zookeeperoperator (); Zkoperator.connect ("192.168.80.100"); Action 1: Create a persistent node//zkoperator.create ("/test1 "," ZK ". GetBytes ()); Zkoperator.create ("/test1/test11", "ZK". GetBytes ()); Zkoperator.create ("/test1/test12", "ZK". GetBytes ()); Action 2: Remove the original node//byte[] b = Zkoperator.getdata ("/test1"); System.out.println (New String (b)); Action 3: Set the node data//zkoperator.setdate ("/test1", "Zk1". GetBytes (), 1); Action 4: Delete node operation Zkoperator.deletenode ("/test1/test12", 0); Get child node//Zkoperator.getchild ("/test1"); Zkoperator.close (); } catch (Exception e) {e.printstacktrace (); } }}
My personal website: http://www.caicongyang.com;
My csdn blog address: Http://blog.csdn.net/caicongyang;
Zookeeper cluster Java operations (zookeeper3.4.5)