Exists - 檢查Znode的存在
ZooKeeper類提供了 exists 方法來檢查znode的存在。如果指定的znode存在,則返回一個znode的元數據。exists方法的簽名如下:
exists(String path, boolean watcher)
-
path- Znode路徑
-
watcher - 布爾值,用於指定是否監視指定的znode
讓我們創建一個新的Java應用程序來檢查ZooKeeper API的“exists”功能。創建文件“ZKExists.java”。在main方法中,使用“ZooKeeperConnection”對象創建ZooKeeper對象“zk”。然后,使用自定義“path”調用“zk”對象的“exists”方法。完整的列表如下:
編碼:ZKExists.java
import java.io.IOException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.data.Stat;
public class ZKExists {
private static ZooKeeper zk;
private static ZooKeeperConnection conn;
// Method to check existence of znode and its status, if znode is available.
public static Stat znode_exists(String path) throws
KeeperException,InterruptedException {
return zk.exists(path, true);
}
public static void main(String[] args) throws InterruptedException,KeeperException {
String path = "/MyFirstZnode"; // Assign znode to the specified path
try {
conn = new ZooKeeperConnection();
zk = conn.connect("localhost");
Stat stat = znode_exists(path); // Stat checks the path of the znode
if(stat != null) {
System.out.println("Node exists and the node version is " +
stat.getVersion());
} else {
System.out.println("Node does not exists");
}
} catch(Exception e) {
System.out.println(e.getMessage()); // Catches error messages
}
}
}
一旦編譯和執行應用程序,你將獲得以下輸出。
Node exists and the node version is 1.
getData方法
ZooKeeper類提供 getData 方法來獲取附加在指定znode中的數據及其狀態。 getData 方法的簽名如下:
getData(String path, Watcher watcher, Stat stat)
-
path - Znode路徑。
-
watcher - 監視器類型的回調函數。當指定的znode的數據改變時,ZooKeeper集合將通過監視器回調進行通知。這是一次性通知。
-
stat - 返回znode的元數據。
讓我們創建一個新的Java應用程序來了解ZooKeeper API的 getData 功能。創建文件 ZKGetData.java 。在main方法中,使用 ZooKeeperConnection 對象創建一個ZooKeeper對象 zk 。然后,使用自定義路徑調用zk對象的 getData 方法。
下面是從指定節點獲取數據的完整程序代碼:
編碼:ZKGetData.java
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.data.Stat;
public class ZKGetData {
private static ZooKeeper zk;
private static ZooKeeperConnection conn;
public static Stat znode_exists(String path) throws
KeeperException,InterruptedException {
return zk.exists(path,true);
}
public static void main(String[] args) throws InterruptedException, KeeperException {
String path = "/MyFirstZnode";
final CountDownLatch connectedSignal = new CountDownLatch(1);
try {
conn = new ZooKeeperConnection();
zk = conn.connect("localhost");
Stat stat = znode_exists(path);
if(stat != null) {
byte[] b = zk.getData(path, new Watcher() {
public void process(WatchedEvent we) {
if (we.getType() == Event.EventType.None) {
switch(we.getState()) {
case Expired:
connectedSignal.countDown();
break;
}
} else {
String path = "/MyFirstZnode";
try {
byte[] bn = zk.getData(path,
false, null);
String data = new String(bn,
"UTF-8");
System.out.println(data);
connectedSignal.countDown();
} catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
}
}, null);
String data = new String(b, "UTF-8");
System.out.println(data);
connectedSignal.await();
} else {
System.out.println("Node does not exists");
}
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
一旦編譯和執行應用程序,你將獲得以下輸出
My first zookeeper app
應用程序將等待ZooKeeper集合的進一步通知。使用ZooKeeper CLI zkCli.sh 更改指定znode的數據。
cd /path/to/zookeeper bin/zkCli.sh >>> set /MyFirstZnode Hello
現在,應用程序將打印以下輸出並退出。
Hello
setData方法
ZooKeeper類提供 setData 方法來修改指定znode中附加的數據。 setData 方法的簽名如下:
setData(String path, byte[] data, int version)
-
path- Znode路徑
-
data - 要存儲在指定znode路徑中的數據。
-
version- znode的當前版本。每當數據更改時,ZooKeeper會更新znode的版本號。
現在讓我們創建一個新的Java應用程序來了解ZooKeeper API的 setData 功能。創建文件 ZKSetData.java。在main方法中,使用 ZooKeeperConnection 對象創建一個ZooKeeper對象 zk 。然后,使用指定的路徑,新數據和節點版本調用 zk 對象的 setData 方法。
以下是修改附加在指定znode中的數據的完整程序代碼。
編碼:ZKSetData.java
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import java.io.IOException;
public class ZKSetData {
private static ZooKeeper zk;
private static ZooKeeperConnection conn;
// Method to update the data in a znode. Similar to getData but without watcher.
public static void update(String path, byte[] data) throws
KeeperException,InterruptedException {
zk.setData(path, data, zk.exists(path,true).getVersion());
}
public static void main(String[] args) throws InterruptedException,KeeperException {
String path= "/MyFirstZnode";
byte[] data = "Success".getBytes(); //Assign data which is to be updated.
try {
conn = new ZooKeeperConnection();
zk = conn.connect("localhost");
update(path, data); // Update znode data to the specified path
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
編譯並執行應用程序后,指定的znode的數據將被改變,並且可以使用ZooKeeper CLI zkCli.sh 進行檢查。
cd /path/to/zookeeper bin/zkCli.sh >>> get /MyFirstZnode
getChildren方法
ZooKeeper類提供 getChildren 方法來獲取特定znode的所有子節點。 getChildren 方法的簽名如下:
getChildren(String path, Watcher watcher)
-
path - Znode路徑。
-
watcher - 監視器類型的回調函數。當指定的znode被刪除或znode下的子節點被創建/刪除時,ZooKeeper集合將進行通知。這是一次性通知。
編碼:ZKGetChildren.java
import java.io.IOException;
import java.util.*;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.data.Stat;
public class ZKGetChildren {
private static ZooKeeper zk;
private static ZooKeeperConnection conn;
// Method to check existence of znode and its status, if znode is available.
public static Stat znode_exists(String path) throws
KeeperException,InterruptedException {
return zk.exists(path,true);
}
public static void main(String[] args) throws InterruptedException,KeeperException {
String path = "/MyFirstZnode"; // Assign path to the znode
try {
conn = new ZooKeeperConnection();
zk = conn.connect("localhost");
Stat stat = znode_exists(path); // Stat checks the path
if(stat!= null) {
//“getChildren" method- get all the children of znode.It has two
args, path and watch
List <String> children = zk.getChildren(path, false);
for(int i = 0; i < children.size(); i++)
System.out.println(children.get(i)); //Print children's
} else {
System.out.println("Node does not exists");
}
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
在運行程序之前,讓我們使用ZooKeeper CLI zkCli.sh 為 /MyFirstZnode 創建兩個子節點。
cd /path/to/zookeeper bin/zkCli.sh >>> create /MyFirstZnode/myfirstsubnode Hi >>> create /MyFirstZnode/mysecondsubmode Hi
現在,編譯和運行程序將輸出上面創建的znode。
myfirstsubnode mysecondsubnode
刪除Znode
ZooKeeper類提供了 delete 方法來刪除指定的znode。 delete 方法的簽名如下:
delete(String path, int version)
-
path - Znode路徑。
-
version - znode的當前版本。
讓我們創建一個新的Java應用程序來了解ZooKeeper API的 delete 功能。創建文件 ZKDelete.java 。在main方法中,使用 ZooKeeperConnection 對象創建一個ZooKeeper對象 zk 。然后,使用指定的路徑和版本號調用 zk 對象的 delete 方法。
刪除znode的完整程序代碼如下:
編碼:ZKDelete.java
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.KeeperException;
public class ZKDelete {
private static ZooKeeper zk;
private static ZooKeeperConnection conn;
// Method to check existence of znode and its status, if znode is available.
public static void delete(String path) throws KeeperException,InterruptedException {
zk.delete(path,zk.exists(path,true).getVersion());
}
public static void main(String[] args) throws InterruptedException,KeeperException {
String path = "/MyFirstZnode"; //Assign path to the znode
try {
conn = new ZooKeeperConnection();
zk = conn.connect("localhost");
delete(path); //delete the node with the specified path
} catch(Exception e) {
System.out.println(e.getMessage()); // catches error messages
}
}
}
