与ZooKeeper集合进行交互的应用程序称为 ZooKeeper客户端或简称客户端。 Znode是ZooKeeper集合的核心组件,ZooKeeper API提供了一小组方法使用ZooKeeper集合来操纵znode的所有细节。
本章中最重要的一组ZooKeeper API。ZooKeeper API的核心部分是ZooKeeper类。它提供了在其构造函数中连接ZooKeeper集合的选项,并具有以下方法:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
<!--<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
-->
</dependencies>
ZooKeeper类通过其多种构造函数提供connect功能。如下 : ZooKeeper(String connectionString, int sessionTimeout, Watcher watcher)
这里用watcher结合countDownLatch做了一个计数,阻塞主线程直至连接上并减计数器countDownLatch,当连接建立成功后才进行打印当前连接状态.
private static void getZkConection() {
CountDownLatch countDownLatch=new CountDownLatch(1);
String connection="ip:port";
try {
zooKeeper=new ZooKeeper(connection, 4000, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getState()==Event.KeeperState.SyncConnected){
countDownLatch.countDown();
}
}
});
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void closeZkConnection(){
try {
zooKeeper.close();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
ZooKeeper类提供了在ZooKeeper集合中创建一个新的znode的create方法。 create 方法的签名如下: create(String path, byte[] data, List<ACL> acl, CreateMode createMode)
private static void createZkMode() {
try {
zooKeeper.create("/test2", "JavaApiTest".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
结果
ZooKeeper类提供了 exists 方法来检查znode的存在。如果指定的znode存在,则返回一个znode的元数据。exists方法的签名如下:
exists(String path, boolean watcher)
private static void checkZnode(String s) {
try {
Stat stat = zooKeeper.exists(s, true);
if (stat==null){
System.out.println("结点为空");
}else {
System.out.println("结点存在,且当前结点的版本为:"+stat.getVersion()+"创建时间为:"+stat.getCtime());
}
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
结果
ooKeeper类提供 getData 方法来获取附加在指定znode中的数据及其状态。 getData 方法的签名如下:
getData(String path, Watcher watcher, Stat stat)
private static void getZnodeData(String path) {
try {
byte[] data = zooKeeper.getData(path, null, null);
System.out.print("该结点值为"+new String(data));
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
ZooKeeper类提供 setData 方法来修改指定znode中附加的数据,并且也可以返回一个修改后的stat值。 setData 方法的签名如下:
setData(String path, byte[] data, int version)
private static void setZnode(String path) {
try {
Stat stat = zooKeeper.exists(path, false);
if (stat==null){
System.out.println("Znode does not exist");
}else {
System.out.println("老版本为"+stat.getVersion());
Stat newStat = zooKeeper.setData(path, "newValue".getBytes(), stat.getVersion());
System.out.println("新版本为"+newStat.getVersion());
getZnodeData(path);
}
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
delete(String path, int version)
private static void deleteZnode(String path)
{
try {
Stat stat = zooKeeper.exists(path, false);
if (stat==null){
System.out.println("Znode does not exist");
}else {
zooKeeper.delete(path,stat.getVersion());
}
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
传的path为"test2,已经没了"
ZooKeeper类提供 getChildren 方法来获取特定znode的所有子节点。 getChildren 方法的签名如下:
getChildren(String path, Watcher watcher)
private static void getZnodeChild(String path) throws KeeperException, InterruptedException {
Stat stat = zooKeeper.exists(path, null);
if (stat==null){
System.out.println("Znode does not exist");
}else {
List<String> list = zooKeeper.getChildren(path, null);
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}