zookeeper java api(2)
阿新 • • 發佈:2018-11-15
這裡介紹其他的API對zookeeper的操作。
- 同步方式獲取子節點資料
public static void getChildrenSync() throws KeeperException, InterruptedException { List<String> childrenList = zkClient().getChildren("/", true); for(String child:childrenList){ System.out.println(child); } }
方法getChildren的第二個引數同樣為watch,當節點的子節點列表發生變化時,zk伺服器會向我們推送型別為NodeChildrenChanged的事件。
- 非同步方式獲取子節點資料
public static void main(String[] args) throws Exception { ZooKeeper zooKeeper = new ZooKeeper("127.0.0.1:2182", 5000, new CreateNode()); countDownLatch.await(); zooKeeper.getChildren("/",true,new MyStringCallBackList(),"獲取/下面的子節點"); Thread.sleep(10000); } static class MyStringCallBackList implements AsyncCallback.Children2Callback{ @Override public void processResult( final int rc, final String path, final Object ctx, final List<String> children, final Stat stat) { System.out.println(rc); System.out.println(path); System.out.println(ctx); for(String child : children) { System.out.println(child); } System.out.println(stat); } }
- 檢視一個節點是否存在(同步)
/** * 同步的方式檢視一個節點是否存在 * @throws KeeperException * @throws InterruptedException */ public static void existSync() throws KeeperException, InterruptedException { Stat stat = zkClient().exists("/poype_node2", true); if(stat!=null){ System.out.println("節點存在"+stat); } }
exists方法的watch引數比較特別,如果將其指定為true,那麼代表你對該節點的建立事件、節點刪除事件和該節點的資料內容改變事件都感興趣,所以會同時響應三種事件型別。請看process方法中對事件的處理:
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
if(watchedEvent.getType() == Event.EventType.None && null == watchedEvent.getPath()) {
existAsync();
} else if(watchedEvent.getType() == Event.EventType.NodeCreated) {
System.out.println("監控到了該節點被建立");
existAsync();
} else if(watchedEvent.getType() == Event.EventType.NodeDataChanged) {
System.out.println("監控到了該節點的資料內容發生變化");
existAsync();
} else if(watchedEvent.getType() == Event.EventType.NodeDeleted) {
System.out.println("監控到了該節點被刪除");
existAsync();
}
}
}
- 檢視一個節點是否存在(非同步)
private static void existAsync() throws IOException {
getZKClient().exists("/poype_node2", true, new AsyncCallback.StatCallback() {
@Override
public void processResult(int resultCode, String path, Object ctx, Stat stat) {
System.out.println(resultCode);
System.out.println(path);
System.out.println(ctx);
System.out.println(stat);
}
}, "非同步檢視一個節點是否存在");
}
public static ZooKeeper getZKClient() throws IOException {
return new ZooKeeper("127.0.0.1:2182", 5000, new CreateNode());
}
- 修改節點資料(同步)
public static void setDataSync() throws KeeperException, InterruptedException {
Stat stat = zkClient().setData("/poype_node2", "9888328".getBytes(), 1);
System.out.println(stat);
}
setData方法有三個引數,前兩個引數分別是節點的路徑和要修改的資料值,最後一個引數是version欄位。在呼叫setData方法修改節點資料內容時,只有當version引數的值與節點狀態資訊中的dataVersion值相等時,資料修改才能成功,否則會丟擲BadVersion異常。這是為了防止丟失資料的更新,在ZooKeeper提供的API中,所有的寫操作(例如後面要提到的delete)都有version引數。
- 非同步修改節點的資料內容:
public static ZooKeeper getZKClient() throws IOException {
return new ZooKeeper("127.0.0.1:2182", 5000, new CreateNode());
}
public static void setDataAsync() throws IOException {
getZKClient().setData("/poype_node2", "poype5211314".getBytes(), 3, new AsyncCallback.StatCallback() {
@Override
public void processResult(int resultCode, String path, Object ctx, Stat stat) {
System.out.println(resultCode);
System.out.println(path);
System.out.println(ctx);
System.out.println(stat.getVersion());
}
}, "非同步設定一個節點的資料");
}
-
刪除一個節點(同步)
public static void deleteSync() throws KeeperException, InterruptedException {
zkClient().delete("/poype_node2",0);
}
- 刪除一個節點(非同步)
-
private void deleteAsync() { zooKeeper.delete("/poype_node", 3, new AsyncCallback.VoidCallback() { public void processResult(int resultCode, String path, Object ctx) { System.out.println(resultCode); System.out.println(path); System.out.println(ctx); } }, "非同步刪除一個節點"); }