Zookeeer笔记(4)——Java客户端API

1 创建节点

import org.apache.zookeeper.*;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class ZKtestclient {

    private static final String connectString = "192.168.154.131:2181,192.168.154.132:2181,192.168.154.133:2181";
    private static final int sessionTimeout = 2000;

    ZooKeeper zkClient = null;

    @Before
    public void init() throws IOException {

        zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                //收到事件通知后的回调函数
                System.out.println(event.getType() + "---" + event.getPath());
            }
        });
    }


    /*
     * 数据的增删改查
     * */
    public void testCreate() throws KeeperException, InterruptedException {

        /*
         * 参数1:节点路径
         * 参数2:节点数据
         * 参数3:节点权限
         * 参数4:节点类型
         * */
        String nodeCreated = zkClient.create("/IDEA", "helloZK".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

    }

    //获取子节点
    @Test
    public void getChildren() throws KeeperException, InterruptedException {
        List<String> children = zkClient.getChildren("/", true);
        for(String child:children){

            System.out.println(child);
        }
    }
}

发现程序报错$ConnectionLossException
这里写图片描述
发现是private static final int sessionTimeout = 2000;设置时间太短,改成20000后,结果如下
这里写图片描述

2 测试监听

import org.apache.zookeeper.*;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class ZKtestclient {

    private static final String connectString = "192.168.154.131:2181,192.168.154.132:2181,192.168.154.133:2181";
    private static final int sessionTimeout = 200000;

    ZooKeeper zkClient = null;

    @Before
    public void init() throws IOException {

        zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                //收到事件通知后的回调函数
                System.out.println(event.getType() + "---" + event.getPath());
                try{
                    zkClient.getChildren("/",true);
                }catch(Exception e){

                }
            }
        });
    }


    /*
     * 数据的增删改查
     * */
    public void testCreate() throws KeeperException, InterruptedException {

        /*
         * 参数1:节点路径
         * 参数2:节点数据
         * 参数3:节点权限
         * 参数4:节点类型
         * */
        String nodeCreated = zkClient.create("/IDEA", "helloZK".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

    }

    //获取子节点
    @Test
    public void getChildren() throws KeeperException, InterruptedException {
        List<String> children = zkClient.getChildren("/", true);
        for(String child:children){

            System.out.println(child);
        }

        Thread.sleep(Long.MAX_VALUE);
    }
}

这里写图片描述
这里写图片描述

3 判断节点是否存在

 //判断节点是否存在
    @Test
    public void testExist() throws Exception {
        Stat stat = zkClient.exists("/IDEA", false);
        System.out.println(stat==null?"not exist":"exist");
    }

4 获取节点数据

 //获取znode的数据
    @Test
    public void getData() throws KeeperException, InterruptedException {
       byte[] data= zkClient.getData("/IDEA",false,new Stat());
        System.out.println(new String(data));
    }

这里写图片描述

5 删除节点

//删除znode数据
    @Test
    public void deleteZnode() throws Exception {
        //参数2 指定要删除的版本,-1 表示删除所有版本
        zkClient.delete("/IDEA",-1);
    }

6 修改节点数据

//修改数据
    @Test
    public void setData() throws Exception{
        zkClient.setData("/test","set Test".getBytes(),-1);
        byte[] data= zkClient.getData("/test",false,null);
        System.out.println(new String(data));
    }

这里写图片描述

猜你喜欢

转载自blog.csdn.net/u012292754/article/details/81002570