package com.zookeeper;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
public class ZookeeperDemo {
private static String connectString = "bigdata01:2181,bigdata02:2181,bigdata03:2181";
private static int sessionTimeout = 2000;
private ZooKeeper zkClient = null;
@Before
public void init() throws Exception {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
System.out.println(watchedEvent.getType() + "--" + watchedEvent.getPath());
System.out.println("连接成功");
try {
zkClient.getChildren("/", true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Test
public void create() throws Exception{
String nodeCreated = zkClient.create("/huang", "lan".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(nodeCreated);
}
@Test
public void exist() throws Exception {
Stat stat = zkClient.exists("/atguigu1", false);
System.out.println(stat == null ? "not exist" : "exist");
}
@Test
public void setData() throws Exception{
zkClient.setData("/atguigu1","jinlian1".getBytes(),0);
}
@Test
public void getDate() throws Exception{
byte[] zkClientData = zkClient.getData("/atguigu1", null, null
);
System.out.println(zkClientData);
}
@Test
public void delete() throws KeeperException, InterruptedException {
zkClient.delete("/atguigu/a1", 1);
}
@Test
public void lsAll() throws Exception {
ls("/");
}
public void ls(String path) throws Exception {
List<String> list = zkClient.getChildren(path, null);
if (list == null || list.isEmpty()) {
return;
}
for (String s : list) {
if (path.equals("/")) {
ls(path + s);
} else {
ls(path + "/" + s);
}
}
}
@Test
public void watchChange1() throws Exception {
Stat stat = new Stat();
byte[] data = zkClient.getData("/atguigu", new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getType() == Event.EventType.NodeDataChanged) {
System.out.println("节点数据改变了!");
}
}
}, stat);
System.out.println(new String(data));
Thread.sleep(50000);
}
@Test
public void watchChange2() throws Exception {
final Stat stat = new Stat();
final String path = "/atguigu";
byte[] data = zkClient.getData(path, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getType() == Event.EventType.NodeDataChanged) {
try {
byte[] res = zkClient.getData(path, this,stat);
System.out.println("节点数据改变了 " + new String(res));
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
}
}
}
}, stat);
System.out.println(new String(data));
Thread.sleep(50000);
}
@Test
public void watchDelete() throws Exception {
String path = "/atuguigu/a3";
Stat stat = new Stat();
byte[] data = zkClient.getData(path, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getType() == Event.EventType.NodeDeleted) {
System.out.println("节点数据删除了!");
}
}
}, stat);
System.out.println("****" + new String(data));
Thread.sleep(Long.MAX_VALUE);
}
}
版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://thexing.blog.csdn.net/article/details/111195413,作者:张国荣家的弟弟,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。