ZooKeeper Java API

pom.xml 依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>root</artifactId>
<groupId>jhxxb</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../root/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>zookeeper</artifactId> <dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<!-- 指定jdk -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

测试代码

public class ZooKeeperTest {
// 集群地址
// private String connectString = "192.168.8.138:2181,192.168.8.136:2181,192.168.8.140:2181";
private String connectString = "127.0.0.1:2181";
private int sessionTimeout = 5000;
private ZooKeeper zk; @Before
public void init() throws IOException {
BasicConfigurator.configure();
zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) { }
});
} @After
public void close() throws InterruptedException {
zk.close();
} @Test
public void create() throws KeeperException, InterruptedException {
// 创建节点
System.out.println(zk.create("/zhongguo", "hubei".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
} @Test
public void getChildren() throws KeeperException, InterruptedException {
// 获取节点
System.out.println(zk.getChildren("/", false));
} @Test
public void getData() throws KeeperException, InterruptedException {
// 获取节点数据
System.out.println(new String(zk.getData("/zhongguo", false, zk.exists("/zhongguo", false))));
} @Test
public void exists() throws KeeperException, InterruptedException {
// 判断节点是否存在
Stat stat = zk.exists("/zhongguo", false);
System.out.println(stat == null ? "not exist" : "exist");
} @Test
public void setData() throws KeeperException, InterruptedException {
// 修改子目录节点数据
System.out.println(zk.setData("/zhongguo", "beijing".getBytes(), -1));
} @Test
public void delete() throws Exception {
// 删除空节点目录
//zk.delete("/zhongguo", -1);
// 删除父节点目录
rmr("/dubbo");
} /**
* 递归删除,zookeeper 只允许删除叶子节点(空节点)
*/
public void rmr(String path) throws Exception {
// 获取路径下的节点
List<String> children = zk.getChildren(path, false);
for (String pathCd : children) {
// 获取父节点下面的子节点路径
String newPath = "";
// 递归调用,判断是否是根节点
if (path.equals("/")) {
newPath = "/" + pathCd;
} else {
newPath = path + "/" + pathCd;
}
rmr(newPath);
}
// 删除节点,并过滤 zookeeper 节点和 / 节点
if (path != null && !path.trim().startsWith("/zookeeper") && !path.trim().equals("/")) {
zk.delete(path, -1);
// 打印删除的节点路径
System.out.println("删除节点:" + path);
}
}
}

http://zookeeper.apache.org/doc/r3.4.14/api/index.html

ZooKeeper-API CURD的更多相关文章

  1. ZooKeeper系列4:ZooKeeper API简介及编程

    问题导读: 1.ZooKeeper API 共包含几个包? 2.如何使用ZooKeeper API 创建zookeeper应用程序? 1)ZooKeeper API 简介   ZooKeeper AP ...

  2. 面向服务的体系架构 SOA(三) --- Zookeeper API、zkClient API的使用

    zookeeper简单介绍及API使用 1.1 zookeeper简介 zookeeper是一个针对大型分布式系统的可靠的协调系统,提供的功能包括配置维护.名字服务.分布式同步.组服务等.zookee ...

  3. Hbase记录-ZooKeeper API

    Zookeeper API ZooKeeper有一个Java和C绑定的官方API.ZooKeeper社区提供了对于大多数语言(.NET,Python等)的非官方API.使用ZooKeeper的API, ...

  4. Zookeeper 系列(三)Zookeeper API

    Zookeeper 系列(三)Zookeeper API 本节首先介绍 Zookeeper 的 Shell 命令,再对 Java 操作 Zookeeper 的三种方式进行讲解,本节先介绍 Zookee ...

  5. Zookeeper api增删改查节点

    Exists - 检查Znode的存在 ZooKeeper类提供了 exists 方法来检查znode的存在.如果指定的znode存在,则返回一个znode的元数据.exists方法的签名如下: ex ...

  6. 聊聊、Zookeeper API

    今天我们来说说 Zookeeper 客户端启动,整个文章分三个部分:第一部分是 Zookeeper 原生 API 客户端,第二部分是开源客户端 ZkClient,第三部分是开源客户端 Curator. ...

  7. Zookeeper Api(java)入门与应用(转)

    如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...

  8. zookeeper[3] zookeeper API开发注意事项总结

    如下是根据官方接口文档(http://zookeeper.apache.org/doc/r3.4.1/api/org/apache/zookeeper/ZooKeeper.html#register( ...

  9. Zookeeper Api

    如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...

  10. Zookeeper Api(java)入门与应用

    如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...

随机推荐

  1. c/c++ 重载运算符 基本概念

    重载运算符 基本概念 问题:对于int,float可以进行算数运算,但是对于一个自定义的类的对象进行算术运算,就不知道具体怎么运算了. 所以有了自定义运算符的概念. 1,自定义运算符其实就是一个以op ...

  2. Arch Linux安装Firefox 火狐中文版

    很多人刚安装好系统之后,刚开始内置的浏览器是火狐的英文版,很多时候因为需要账号同步的原因需要国内版本的火狐浏览器,这个时候我们应该怎么操作呢? 其实也非常的简单 首先我们 输入命令 pacman -S ...

  3. C# -- 使用委托 delegate 执行异步操作

    C# -- 使用委托 delegate 执行异步操作 委托是一种安全地封装方法的类型,它与 C 和 C++ 中的函数指针类似. 与 C 中的函数指针不同,委托是面向对象的.类型安全的和保险的. 委托的 ...

  4. MySql 学习之路-聚合函数

    下面是mysql 数据库中经常用到的聚合函数的简单实例 -- 创建学生表 create table student ( id int primary key auto_increment commen ...

  5. ZooKeeper Dynamic Reconfiguration (dynamicConfigFile) ZooKeeper动态配置

    有人翻译的地址:https://www.cnblogs.com/dupang/p/5649843.html ZooKeeper Dynamic Reconfiguration Overview Cha ...

  6. redis分页

    模仿的https://www.cnblogs.com/dee0912/p/4612183.html 第一步连接redis后进行添加数据 require_once '../redis/redis.php ...

  7. vmware centos7 minimal 配置共享文件夹

    使用的是VMware安装CentOS7 minimal版,系统镜像是CentOS-7-x86_64-DVD-1708.iso. 宿主机系统为win10,CentOS7 minimal过程省略,可参考h ...

  8. 【vue】移动端demo资料

    http://imzjh.com/inew/#/(移动端demo) https://github.com/liangxiaojuan/eleme(饿了么git地址) https://github.co ...

  9. JavaScript代码组织结构良好的5个特点

    JavaScript代码组织结构良好的5个特点,随着JavaScript项目的成长,如果你不小心处理的话,他们往往会变得难以管理.我们发现自己常常陷入的一些问题: 当在创建新的页面时发现,很难重用或测 ...

  10. python基础语法、数据结构、字符编码、文件处理 练习题

    考试范围 '''1.python入门:编程语言相关概念2.python基础语法:变量.运算符.流程控制3.数据结构:数字.字符串.列表.元组.字典.集合4.字符编码5.文件处理''' 考试内容 1.简 ...