Zookeeper JavaApi 增删改查
生活随笔
收集整理的這篇文章主要介紹了
Zookeeper JavaApi 增删改查
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
官網(wǎng)API
http://zookeeper.apache.org/doc/r3.4.6/api/index.html
JAR包
\zookeeper-3.3.6\lib\
jline-0.9.94.jar
\zookeeper-3.3.6\lib\
log4j-1.2.15.jar
\zookeeper-3.3.6\
zookeeper-3.3.6.jar
Demo代碼
注意代碼中的注解
package hello.zookeeper.api;import java.util.List;import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooDefs.Ids; import org.junit.Before; import org.junit.Test; import org.apache.zookeeper.ZooKeeper;public class ZkClient {/** 此處必須與zookeeper的zoo.cfg中一樣* zk會(huì)選擇一個(gè)連接* 如果windows上運(yùn)行,hosts也要配置一致*/private static final String connString="zk1:2181,zk2:2181,zk3:2181";/** 超時(shí):連接超時(shí),同步時(shí)間間隔,zk有節(jié)點(diǎn)值變更,在這個(gè)時(shí)間內(nèi)同步*/private static final int sessionTimeout=2000;private ZooKeeper zk=null;@Beforepublic void init() throws Exception{zk=new ZooKeeper(connString, sessionTimeout, new Watcher() {@Overridepublic void process(WatchedEvent event) {System.out.println("監(jiān)聽器:"+event.getType()+"-"+event.getPath());}});}@Testpublic void create() throws Exception{String path="/eclipse";if(zk.exists(path, false)==null){String msg=zk.create(path, "helloworld".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);System.out.println("create:"+msg);}else{System.out.println(path+"已存在!");}}@Testpublic void getChildren() throws Exception{List<String> children = zk.getChildren("/", true);if(children!=null&&children.size()>0){System.out.println("子目錄如下:");for(String s:children){System.out.println(s);}}}@Testpublic void setData() throws Exception{//-1表示刪除所有版本zk.setData("/eclipse", "hello earth !".getBytes(), -1);getData();}@Testpublic void getData() throws Exception{byte[] data = zk.getData("/eclipse", true, null);System.out.println("getData():"+new String(data));}@Testpublic void deleteZnode() throws Exception{zk.delete("/eclipse", -1);getChildren();}}-------------
更多的Java,Angular,Android,大數(shù)據(jù),J2EE,Python,數(shù)據(jù)庫,Linux,Java架構(gòu)師,:
http://www.cnblogs.com/zengmiaogen/p/7083694.html
總結(jié)
以上是生活随笔為你收集整理的Zookeeper JavaApi 增删改查的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Lock的tryLock()方法
- 下一篇: python定时任务是异步的吗_定时任务