Zookeeper的java客户端基本功能操作
1.创建节点
2.更新节点
3.获取节点数据
4.获取子节点
5.删除节点
package com.zk.demo;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Before;
import org.junit.Test;
public class ZookeeperClientDemo {
ZooKeeper zk;
@Before
public void init() throws Exception {
// 构造一个连接zookeeper的客户端对象
zk = new ZooKeeper("master:2181,slave1:2181,slave2:2181", 2000, null);
}
@Test
public void testCreate() throws IOException, KeeperException, InterruptedException {
// 参数1:要创建的节点路径 参数2:数据 参数3:访问权限 参数4:节点类型
String create = zk.create("/hello-eclipse", "hello eclipse".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(create);
zk.close();
}
@Test
public void testUpdate() throws Exception, KeeperException, InterruptedException {
// 参数1:节点路径 参数2:数据 参数3:所要修改的版本,-1代表任何版本
zk.setData("/hello-eclipse", "hellohello".getBytes("utf-8"), -1);
zk.close();
}
@Test
public void testGet() throws KeeperException, InterruptedException, UnsupportedEncodingException {
// 参数1:节点路径 参数2:是否要监听 参数3:所要获取的数据的版本,null表示最新版本
byte[] data = zk.getData("/hello-eclipse", false, null);
System.out.println(new String(data,"utf-8"));
zk.close();
}
@Test
public void testListChildren() throws KeeperException, InterruptedException {
// 参数1:节点路径 参数2:是否要监听
// 注意:返回的结果中只有子节点名字,不带全路径
List<String> children = zk.getChildren("/cc", false);
for (String child : children) {
System.out.println(child);
}
zk.close();
}
@Test
public void testRm() throws InterruptedException, KeeperException {
zk.delete("/hello-eclipse", -1);
zk.close();
}
}