Zookeeper JavaApi 增删改查
来源:互联网 发布:小说逆命淘宝 编辑:程序博客网 时间:2024/05/17 06:09
官网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会选择一个连接 * 如果windows上运行,hosts也要配置一致 */private static final String connString="zk1:2181,zk2:2181,zk3:2181";/* * 超时:连接超时,同步时间间隔,zk有节点值变更,在这个时间内同步 */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("监听器:"+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();}}
0 0
- Zookeeper JavaApi 增删改查
- elasticsearch学习javaAPI:es的增删改查
- java访问zookeeper的增删改查
- HBase的javaAPI的增删改查的基本操作实现
- Zookeeper.NET Client(三)【Znode节点增删改查】
- 《ZooKeeper》---原生的API中的增删改查操作
- hibernate增删查改
- XML 增删查改
- ldap 增删改查
- mysql增删改查
- 动态增删改查
- jdom增删改查
- 集合增删查改
- XML 增删改查
- xml增删改查
- lucene增删改查
- 增删改查
- ldap增删改查
- html嵌套规范
- swizzle method
- IDE_Webstorm11 激活
- 感知机与梯度下降算法
- 合唱队,对n个人挑出n-k个人,剩下k个人排成合唱队
- Zookeeper JavaApi 增删改查
- Android群英传——第五章实现滑动的7种方法(一二三)
- scrapy爬虫保存为csv文件的技术分析
- poj1144 割点模板
- shell中if条件字符串、数字比对,[[ ]]和[ ]区别
- java自定义hive sparksql thriftServer连接池
- 公共知识-Linux常用命令
- HTML/CSS学习汇总(1)
- linux的原子操作学习总结