11.7 setData方法
zookeeper类提供setData方法来修改附着在指定znode的数据。setData方法:
setData(String path, byte[] data, int version)<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>
参数解释说明:
path-----------znode路径
data-----------存储在指定znode路径的数据
version-------znode当前版本。只要数据发生改变,zookeeper就会更新znode的版本号
创建一个新的Java应用程序帮助理解ZooKeeperAPI的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());
}
}
}
一旦编译并运行,使用zookeeperCLI zkCli.sh检查指定znode数据变化:
启动客服端:bin/zkCli.sh start
调用get:get /MyFirstZnode
11.8 getChildren 方法
zookeeper类提供的getChildren方法来获取指定znode的所有子节点。getChildren方法:
getChildren(String path, Watcher watcher)<span style="font-family: Arial, Helvetica, sans-serif; font-weight: bold; background-color: rgb(255, 255, 255);"> </span><span style="font-family: Arial, Helvetica, sans-serif; font-weight: bold; background-color: rgb(255, 255, 255);"> </span>
参数解释说明:
path----------znode路径
watcher----调用类型为watcher函数。当指定znode被删除或znode以下的children被创建/删除时,zookeeper ensemble会通知。只会通知一次
代码: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创建两个子节点:
启动客服端:bin/zkCli.sh start
调用create:
create/MyFirstZnode/myfirstsubnode Hi
create /MyFirstZnode/mysecondsubmode Hi
现在,编译和运行程序的输出上面创建的znodes:myfirstsubnode 、mysecondsubnode
11.9 删除Znode
zookeeper类提供delete方法来删除指定znode。Delete方法:
delete(String path, int version)
参数解释说明:
path------znode路径
version---当前znode的版本
创建一个新的Java应用程序帮助理解ZooKeeperAPI的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
}
}
}