1. 程式人生 > >Zookeeper之常用API詳解

Zookeeper之常用API詳解

1、建立ZNode

①命令列方式

  • 在根目錄下建立“FZnode"節點並存儲資料”First Znode":
       create /FZnode "First Znode"
  • 在根目錄下建立短暫的Znode節點
       create -e /SZnode "Secondary Znode"
  • 建立短暫有序列的Znode節點
       create -e -s /EZnode "Ephemeral_SEQUENTIAL"
  • 建立持久有序列的Znode節點
       create -s /PZnode "PERSISTENT_SEQUENTIAL"

②JAVA呼叫API

    /**
     * 建立ZNode節點
     * @throws Exception
     */
    @Test
    public void create() throws Exception{
        //用於停止(等待)主程序,直到客戶端與Zookeeper集合連線。
        final CountDownLatch cd = new CountDownLatch(1);
        ZooKeeper zk = new ZooKeeper("centos75:2181,centos76:2181,centos77:2181",5000,new Watcher(){
            //watcher返回Zookeeper連線狀態
            public void process(WatchedEvent watchedEvent) {
                System.out.println(watchedEvent.getState());
                //連線建立,回撥process介面時,其watchedEvent.getState() 為 Event.KeeperState.SyncConnected
                if(watchedEvent.getState() == Event.KeeperState.SyncConnected){
                    //喚醒wait中的執行緒
                    cd.countDown();
                }
            }
        });
        cd.await();
        /**
         * 1、Znode節點路徑
         * 2、Znode節點內容
         * 3、Znode建立節點的訪問控制列表
         * 4、Znode狀態,有四種狀態
         */
        zk.create("/APIjava",new String("test API").getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
    }
2、獲取ZNode資料 ①、命令列方式
      get /FZnode
②、JAVA呼叫API
    /**
     * 獲取Znode資料
     */
    @Test
    public void getData() throws Exception {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        ZooKeeper zk = new ZooKeeper("centos75:2181,centos76:2181,centos77:2181", 5000, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                if(watchedEvent.getState()==Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                }
            }
        });
        countDownLatch.await();
        byte[] data = zk.getData("/apijava", false, new Stat());
        System.out.println(new String(data));
    }
3、給ZNode設定內容 ①、命令列方式
      set /FZnode "Set Data"
②、JAVA呼叫API
    /**
     * 給Znode設定資料
     * @throws Exception
     */
    @Test
    public void setData() throws  Exception{
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        ZooKeeper zk = new ZooKeeper("centos75:2181,centos76:2181,centos77:2181", 5000, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                if(watchedEvent.getState() == Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                }
            }
        });
        countDownLatch.await();
        /**
         * 1表示當前Znode版本,每當資料更改時,Zookeeper會更新Znode的版本號.
         */
        zk.setData("/apijava",new String("API SET").getBytes(),1);
    }
4、列出ZNode下的的子節點 ①、命令列方式
      ls /FZnode
②、JAVA呼叫API
    /**
     * 獲取Znode所有的子節點
     */
    @Test
    public void getChild()throws Exception{
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        ZooKeeper zk = new ZooKeeper("centos75:2181,centos76:2181,centos77:2181", 5000, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                if(watchedEvent.getState() == Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                }
            }
        });
        countDownLatch.await();
        List<String> children = zk.getChildren("/apijava", false);
        for (int i = 0; i < children.size(); i++) {
            System.out.println(children.get(i));
        }
    }
5、刪除ZNode節點 ①、命令列方式
       rmr /FZnode
②、JAVA呼叫API
    /**
     * 刪除指定Znode,並且該Znode沒有子項
     * @throws Exception
     */
    @Test
    public void delete() throws  Exception{
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        ZooKeeper zk = new ZooKeeper("centos75:2181,centos76:2181,centos77:2181", 5000, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                if(watchedEvent.getState() == Event.KeeperState.SyncConnected  ){
                    countDownLatch.countDown();
                }
            }
        });
        countDownLatch.await();
        zk.delete("/APIjava0000000012",0);
    }
6、判斷ZNode是否存在:
    /**
     * 檢查Znode是否存在
     */
    @Test
    public void exists() throws Exception {
        final CountDownLatch  countDownLatch = new CountDownLatch(1);
        ZooKeeper zk = new ZooKeeper("centos75:2181,centos76:2181,centos77:2181", 5000, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                if(watchedEvent.getState() == Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                }
            }
        });
        countDownLatch.await();
        Stat stat = zk.exists("/apijava", false);
        if(stat == null){
            System.out.println("Znode節點不存在");
        }else {
            System.out.println("Znode節點存在,並且版本是:"+stat.getVersion());
        }
    }
7、總結:
  • 梳理筆記,加強記憶及理解。
  • 鞏固基礎,勇攀高峰!!!
  • 加油(ง •_•)ง(ง •_•)ง(ง •_•)ง