ZooKeeper-API CURD
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的更多相关文章
- ZooKeeper系列4:ZooKeeper API简介及编程
问题导读: 1.ZooKeeper API 共包含几个包? 2.如何使用ZooKeeper API 创建zookeeper应用程序? 1)ZooKeeper API 简介 ZooKeeper AP ...
- 面向服务的体系架构 SOA(三) --- Zookeeper API、zkClient API的使用
zookeeper简单介绍及API使用 1.1 zookeeper简介 zookeeper是一个针对大型分布式系统的可靠的协调系统,提供的功能包括配置维护.名字服务.分布式同步.组服务等.zookee ...
- Hbase记录-ZooKeeper API
Zookeeper API ZooKeeper有一个Java和C绑定的官方API.ZooKeeper社区提供了对于大多数语言(.NET,Python等)的非官方API.使用ZooKeeper的API, ...
- Zookeeper 系列(三)Zookeeper API
Zookeeper 系列(三)Zookeeper API 本节首先介绍 Zookeeper 的 Shell 命令,再对 Java 操作 Zookeeper 的三种方式进行讲解,本节先介绍 Zookee ...
- Zookeeper api增删改查节点
Exists - 检查Znode的存在 ZooKeeper类提供了 exists 方法来检查znode的存在.如果指定的znode存在,则返回一个znode的元数据.exists方法的签名如下: ex ...
- 聊聊、Zookeeper API
今天我们来说说 Zookeeper 客户端启动,整个文章分三个部分:第一部分是 Zookeeper 原生 API 客户端,第二部分是开源客户端 ZkClient,第三部分是开源客户端 Curator. ...
- Zookeeper Api(java)入门与应用(转)
如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...
- zookeeper[3] zookeeper API开发注意事项总结
如下是根据官方接口文档(http://zookeeper.apache.org/doc/r3.4.1/api/org/apache/zookeeper/ZooKeeper.html#register( ...
- Zookeeper Api
如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...
- Zookeeper Api(java)入门与应用
如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...
随机推荐
- 什么是TLB?
TLB:Translation Lookaside Buffer. 根据功能可以译为快表,直译可以翻译为旁路转换缓冲,也可以把它理解成页表缓冲.里面存放的是一些页表文件(虚拟地址到物理地址的转换表). ...
- 【转】Android 增,删,改,查 通讯录中的联系人
一.权限 操作通讯录必须在AndroidManifest.xml中先添加2个权限, <uses-permission android:name="android.permission. ...
- jar包的MANIFEST.MF文件
打包可执行jar包时,MANIFEST.MF总是个让人头疼的东西,经常出现这种那种问题. 一个例子: ================================================= ...
- 手把手教你发布一个Python包
本文主题如下: 编写一个包(Python 源代码),但不是本文的重点. 编译包,观察编译后的文件. 发布包,发布的包可以有多种类型. 如何在 Pypi 中查看已发布的包 注意: 本文编写的包在 Pyt ...
- Set.js--创建无重复值的无序集合
Set 集合,不同于 Array,是一种没有重复值的集合. 以下代码出自于<JavaScript 权威指南(第六版)>P217,注意:这里并不是指 es6 / es2015 中的 Set ...
- [转帖]Go中的下划线
Go中的下划线 https://blog.csdn.net/wanglei9876/article/details/50475864 下划线的作用: 在import 时 是仅引入 init 函数 在正 ...
- Python中的正则表达式教程
本文http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html 正则表达式经常被用到,而自己总是记不全,转载一份完整的以备不时之需. 1. ...
- 使用Spring表达式语言进行装备--SpEL
本文主要想记录最近的两个使用spring框架实现通过配置文件装备Bean,以及使用SpEL装备Bean. 1.使用配置文件装备Bean: 当我们写某些Bean的时候是希望这个Bean当中的属性是可以通 ...
- Python——collections模块(扩展数据类型)
1.namedtuple:利用坐标.空间坐标,扑克牌等指定空间位置 # namedtuple('名字',[list列表属性])from collections import namedtuple Po ...
- IO多路复用和local概念
一.local 在多个线程之间使用threading.local对象,可以实现多个线程之间的数据隔离 import time import random from threading import T ...