package cn.bigdata;
import java.util.List;
import org.I0Itec.zkclient.DataUpdater;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.CreateMode;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestZKclient {
private ZkClient zkClient = null;
/**
* 创建zookeeper连接
*/
@Before
public void connection() {
// zookeeper地址和超时时间
zkClient = new ZkClient("hadoop05:2181,hadoop06:2181,hadoop07:2181", 2000);
}
/**
* 关闭zookeeper连接
*/
@After
public void close() {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
zkClient.close();
}
/**
* 创建单个节点
*/
@Test
public void testCreateZnode() {
String create = zkClient.create("/zkClient", "abc", CreateMode.PERSISTENT);
System.out.println("创建节点:" + create);
}
/**
* 创建多个节点
*/
@Test
public void testCreateMoreZnode() {
zkClient.createPersistent("/zkClient/zkClient1/zkClient1-1", true);
System.out.println("创建多层节点成功");
}
/**
* 删除节点
*/
@Test
public void testDeleteZnode() {
boolean delete = zkClient.delete("/zkClient");
System.out.println("删除节点:" + delete);
}
/**
* 递归删除某个节点及其子节点 删除一个节点,和原生api一样,只能从最底层节点一个一个删除
*/
@Test
public void testDeleteRecursiveZnode() {
boolean deleteRecursive = zkClient.deleteRecursive("/zkClient");
System.out.println("递归删除节点:" + deleteRecursive);
}
/**
* 更新节点
*/
@Test
public void testUpdateZnode() {
zkClient.updateDataSerialized("/zkClient", new DataUpdater<String>() {
@Override
public String update(String currentData) {
// 返回之前znode的data
System.out.println("currentData:" + currentData);
// 设置新的data
return "bbbbbbb";
}
});
}
/**
* 查询节点
*/
@Test
public void testGetZnode() {
List<String> children = zkClient.getChildren("/");
for (String string : children) {
System.out.println(string);
}
}
/**
* 查询创建时间
*/
@Test
public void testCreationTime() {
long creationTime = zkClient.getCreationTime("/zkClient");
System.out.println("/zkClient的创建时间:" + creationTime);
}
/**
* 查询节点内容
*/
@Test
public void testGetData() {
String readData = zkClient.readData("/zkClient", true);
System.out.println("/zkClient的节点内容:" + readData);
}
/**
* 查询子节点 统计子节点个数
*/
@Test
public void testChild() {
int countChildren = zkClient.countChildren("/zkClient");
System.out.println("/zkClient共有" + countChildren + "个子节点!");
List<String> children = zkClient.getChildren("/zkClient");
for (String string : children) {
System.out.println(string);
}
}
/**
* 注册节点更改监听 利用watch机制做订阅,使用异步操作处理节点
*/
@Test
public void testDataChangesListener() {
zkClient.subscribeDataChanges("/zkClient", new IZkDataListener() {
@Override
public void handleDataDeleted(String dataPath) throws Exception {
System.out.println("删除节点" + dataPath + "成功");
}
@Override
public void handleDataChange(String dataPath, Object data) throws Exception {
System.out.println("节点名称:" + dataPath + "-->修改后的值:" + data);
}
});
for (int i = 0; i < 5; i++) {
zkClient.updateDataSerialized("/zkClient", new DataUpdater<String>() {
@Override
public String update(String currentData) {
return currentData + 1;
}
});
}
}
/**
* 注册子节点改变监听
*/
@Test
public void testChildChangesListener() throws InterruptedException {
zkClient.subscribeChildChanges("/zkClient", new IZkChildListener() {
@Override
public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
System.out.println(parentPath + "子节点被修改!");
for (String string : currentChilds) {
System.out.println("现在为" + string);
}
}
});
for (int i = 0; i < 5; i++) {
zkClient.create("/zkClient/test", "abc", CreateMode.EPHEMERAL_SEQUENTIAL);
Thread.sleep(100);
}
}
}
使用zkClient操作zookeeper
最新推荐文章于 2024-08-24 07:36:09 发布
