java調用zookeeper API demo__api

來源:互聯網
上載者:User

java調用zookeeper API的Demo如下,對zookeeper的增刪改查以及監聽執行個體

注意:

1.使用Thread.sleep()這個方法是防止查看完成後java程式就執行結束了,導致監聽不到變化,其中這裡的getChildren()監聽相當於zookeeper在shell下的ls path watch一樣的效果。

2.zookeeper的監聽只會被執行一次,如果觸發了設定的監聽,那麼下次就不會觸發,可以在process方法裡再次觸發,這樣監聽就會一直存在。

3.zookeeper對節點的刪除,只能刪除葉子節點,它不允許我們刪除的節點下面有子節點的node,如果需要刪除非葉子節點以及它下面的節點,這裡需要使用遞迴的方式刪除。

package com.lijie.zk1;import java.util.List;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.ZooDefs.Ids;import org.apache.zookeeper.ZooKeeper;import org.apache.zookeeper.data.Stat;/** *  * @author LiJie * */public class ZkSimple {    private static final String connectString = "hadoop01:2181,hadoop02:2181,hadoop03:2181";    private static final int sessionTimeout = 2000;    private static ZooKeeper zk = null;    public static void main(String[] args) throws Exception {        //調用create        //create();        //擷取子節點        //getChildren();        //判斷是否存在        //isExist();        //擷取znode資料        //getData();        //刪除        //delete();        //修改        setData();    }    /**     * 擷取zookeeper執行個體     * @return     * @throws Exception     */    public static ZooKeeper getZookeeper() throws Exception {        zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {            @Override            public void process(WatchedEvent event) {                // 收到watch通知後的回呼函數                System.out.println("事件類型" + event.getType() + ",路徑" + event.getPath());                //因為監聽器只會監聽一次,這樣可以一直監聽,且只監聽"/"目錄                try {                    zk.getChildren("/", true);                } catch (Exception e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        });        return zk;    }    /**     * 建立資料     * @throws Exception     */    public static void create() throws Exception {        ZooKeeper zk = getZookeeper();        //建立一個節點,返回建立好的路徑 ,且上傳的資料可以為任意類型,需要轉換成byte[]        //參數1 路徑,參數2 內容,參數3 許可權,參數4 類型        String znodePath = zk.create("/mytest", "hello zookeeper".getBytes(), Ids.OPEN_ACL_UNSAFE,            CreateMode.EPHEMERAL);        System.out.println("返回的路徑 為:" + znodePath);    }    /**     * 判斷znode是否存在     * @throws Exception     */    public static void isExist() throws Exception {        ZooKeeper zk = getZookeeper();        Stat exists = zk.exists("/lijie", false);        if (exists == null) {            System.out.println("不存在");        } else {            System.out.println("存在");        }    }    /**     * 擷取子節點     * @throws Exception     */    public static void getChildren() throws Exception {        ZooKeeper zk = getZookeeper();        //擷取子節點        List<String> children = zk.getChildren("/", true);        for (String string : children) {            System.out.println("子節點:" + string);        }        //阻塞,測試監聽器,且只監聽"/"目錄        Thread.sleep(Long.MAX_VALUE);    }    /**     * 擷取znode資料     * @throws Exception     */    public static void getData() throws Exception {        ZooKeeper zk = getZookeeper();        byte[] data = zk.getData("/lijie/test", false, new Stat());        System.out.println(new String(data));    }    /**     * 刪除資料     * @throws Exception     */    public static void delete() throws Exception {        ZooKeeper zk = getZookeeper();        //第二個參數為version,-1表示刪除所有版本        //它不支援刪除的節點下面還有子節點,只能遞迴刪除        zk.delete("/hehe", -1);    }    /**     * 修改znode的值     * @throws Exception     */    public static void setData() throws Exception {        ZooKeeper zk = getZookeeper();        //修改znode的值        zk.setData("/lijie", "modify data".getBytes(), -1);        //測試是否修改成功        System.out.println(new String(zk.getData("/lijie", false, null)));    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.