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. 【原创】Windows平台下Git的安装与配置

    一.下载     msysgit是Git for Windows版,其Home Page为:http://msysgit.github.io/ 点击页面中“Download”进入下载列表.可根据个人喜 ...

  2. Cs231n-assignment 1作业笔记

    KNN assignment1 KNN讲解参见: https://blog.csdn.net/u014485485/article/details/79433514?utm_source=blogxg ...

  3. Filebeat插件启动失败,不能直接查找报错原因

    老是在filebeat启动的这一步骤上出错,但是由于filebeat是由systemd启动的,因此原因也经常查不清楚,因此并不能直观的查出错误在哪里,所以今天教给大家两个寻找错误的根源的方法 先看我这 ...

  4. 个人对JS原型链的一些理解(prototype、__proto__)

    前言 在我一开始学习java web的时候,对JS就一直抱着一种只是简单用用的心态,于是并没有一步一步地去学习,当时认为用法与java类似,但是在实际web项目中使用时却比较麻烦,便直接粗略了解后开始 ...

  5. 给Integer对象加锁的错误方式

    package com.thread.test; public class BadLockOnInteger implements Runnable { public static Integer i ...

  6. Log4j配置文件详解及实例

     1 ) . 配置根 Logger ,其语法为:   log4j.rootLogger = [ level ] , appenderName, appenderName, … 其中, level 是日 ...

  7. 超哥笔记--shell 基本命令(4)

    一 linux 命令行的组成结构 自定义命令行结构 PS1变量来控制 \u \W 最后一位工作目录 \w 绝对路径工作目录 \t 显示24h制的时间 \h PS1="[\u@\h \w \t ...

  8. 平滑升级你的Nginx

    1.概述(可以直接跳过看第2部分) Nginx方便地帮助我们实现了平滑升级.其原理简单概括,就是: (1)在不停掉老进程的情况下,启动新进程. (2)老进程负责处理仍然没有处理完的请求,但不再接受处理 ...

  9. HTML5仿手机微信聊天界面

    HTML5仿手机微信聊天界面 这篇文章主要为大家详细介绍了HTML5仿手机微信聊天界面的关键代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下     给大家带来的是HTML5仿手机微信聊天界面, ...

  10. STM32F40G-EVAL_UC/OS III

    micrum官网下载uc/os程序包: 包含文件cotex_M4.h: