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、总结:
  • 梳理笔记,加强记忆及理解。
  • 巩固基础,勇攀高峰!!!
  • 加油(ง •_•)ง(ง •_•)ง(ง •_•)ง

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值