第3章 Zookeeper 客户端API操作
作者:互联网
3.3 客户端API操作
前提:保证hadoop102、hadoop103、hadoop104服务器上Zookeeper集群服务端启动。
zk.sh start
3.3.1 IDEA环境搭建
1)创建一个工程:zookeeper
2)添加pom文件
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.7</version>
</dependency>
</dependencies>
3)拷贝log4j.properties文件到项目根目录
需要在项目的src/main/resources目录下,新建一个文件,命名为“log4j.properties”,在文件中填入。
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/log4j.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
4)创建包名com.wolffy.zookeeper
5)创建类名称zkClient
3.3.2 创建ZooKeeper客户端
public class ZkClient {
private String connectString;
private int sessionTimeout;
private ZooKeeper zkClient;
/
* 获取客户端对象
*
* @throws IOException
*/
@Before
public void init() throws IOException {
connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
int sessionTimeout = 10000;
//参数解读 1集群连接字符串 2连接超时时间 单位:毫秒 3当前客户端默认的监控器
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
}
});
}
/
* 关闭客户端对象
* @throws InterruptedException
*/
@After
public void close() throws InterruptedException {
zkClient.close();
}
}
3.3.3 获取子节点列表,不监听
/**
* 获取子节点列表,不监听
*
* @throws IOException
* @throws KeeperException
* @throws InterruptedException
*/
@Test
public void ls() throws IOException, KeeperException, InterruptedException {
//用客户端对象做各种操作
List<String> children = zkClient.getChildren("/", false);
System.out.println(children);
}
3.3.4 获取子节点列表,并监听
/**
* 获取子节点列表,并监听
*
* @throws KeeperException
* @throws InterruptedException
*/
@Test
public void lsAndWatch() throws KeeperException, InterruptedException {
List<String> children = zkClient.getChildren("/", new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println(event);
}
});
System.out.println(children);
//因为设置了监听,所以当前线程不能结束
Thread.sleep(Long.MAX_VALUE);
}
3.3.5 创建子节点
/**
* 创建子节点
*
* @throws KeeperException
* @throws InterruptedException
*/
@Test
public void create() throws KeeperException, InterruptedException {
//参数解读 1节点路径 2节点存储的数据
//3节点的权限(使用Ids选个OPEN即可) 4节点类型 短暂 持久 短暂带序号 持久带序号
String path = zkClient.create("/wolffy", "QQ:2902839937".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
//创建临时节点
//String path = zkClient.create("/wolffy2", "QQ:2902839937".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
System.out.println(path);
//创建临时节点的话,需要线程阻塞
//Thread.sleep(10000);
}
3.3.6 判断Znode是否存在
/**
* 判断Znode是否存在
*
* @throws Exception
*/
@Test
public void exist() throws Exception {
Stat stat = zkClient.exists("/wolffy", false);
System.out.println(stat == null ? "not exist" : "exist");
}
3.3.7 获取子节点存储的数据,不监听
/**
* 获取子节点存储的数据,不监听
*
* @throws KeeperException
* @throws InterruptedException
*/
@Test
public void get() throws KeeperException, InterruptedException {
//判断节点是否存在
Stat stat = zkClient.exists("/wolffy", false);
if (stat == null) {
System.out.println("节点不存在...");
return;
}
byte[] data = zkClient.getData("/wolffy", false, stat);
System.out.println(new String(data));
}
3.3.8 获取子节点存储的数据,并监听
/**
* 获取子节点存储的数据,并监听
*
* @throws KeeperException
* @throws InterruptedException
*/
@Test
public void getAndWatch() throws KeeperException, InterruptedException {
//判断节点是否存在
Stat stat = zkClient.exists("/wolffy", false);
if (stat == null) {
System.out.println("节点不存在...");
return;
}
byte[] data = zkClient.getData("/wolffy", new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println(event);
}
}, stat);
System.out.println(new String(data));
//线程阻塞
Thread.sleep(Long.MAX_VALUE);
}
3.3.9 设置节点的值
/**
* 设置节点的值
*
* @throws KeeperException
* @throws InterruptedException
*/
@Test
public void set() throws KeeperException, InterruptedException {
//判断节点是否存在
Stat stat = zkClient.exists("/wolffy", false);
if (stat == null) {
System.out.println("节点不存在...");
return;
}
//参数解读 1节点路径 2节点的值 3版本号
zkClient.setData("/wolffy", "wolffy".getBytes(), stat.getVersion());
}
3.3.10 删除空节点
/**
* 删除空节点
*
* @throws KeeperException
* @throws InterruptedException
*/
@Test
public void delete() throws KeeperException, InterruptedException {
//判断节点是否存在
Stat stat = zkClient.exists("/small", false);
if (stat == null) {
System.out.println("节点不存在...");
return;
}
zkClient.delete("/small", stat.getVersion());
}
3.3.11 删除非空节点,递归实现
/**
* 封装一个方法,方便递归调用
*/
public void deleteAll(String path, ZooKeeper zk) throws KeeperException, InterruptedException {
//判断节点是否存在
Stat stat = zkClient.exists(path, false);
if (stat == null) {
System.out.println("节点不存在...");
return;
}
//先获取当前传入节点下的所有子节点
List<String> children = zk.getChildren(path, false);
if (children.isEmpty()) {
//说明传入的节点没有子节点,可以直接删除
zk.delete(path, stat.getVersion());
} else {
//如果传入的节点有子节点,循环所有子节点
for (String child : children) {
//删除子节点,但是不知道子节点下面还有没有子节点,所以递归调用
deleteAll(path + "/" + child, zk);
}
//删除完所有子节点以后,记得删除传入的节点
zk.delete(path, stat.getVersion());
}
}
//测试deleteAll
@Test
public void testDeleteAll() throws KeeperException, InterruptedException {
deleteAll("/wolffy", zkClient);
}
标签:stat,Zookeeper,InterruptedException,节点,API,zkClient,log4j,throws,客户端 来源: https://www.cnblogs.com/niuniu2022/p/16342404.html