Zookeeper学习————java API(节点创建、授权、修改、删除、获取节点(子节点)数据、判断节点是否存在)

1.java API

 ZooKeeper zooKeeper;
 CountDownLatch countDownLatch=new CountDownLatch(1);
1.1.创建连接
public void createConn() throws IOException, InterruptedException {
    //1.connectString: 服务器ip、port
    //2.sessionTimeout: 客户端与服务端的会话超时时间,单位:ms
    //3.watcher: 监听器对象
    zooKeeper = new ZooKeeper("127.0.0.1:2181", 10000, new Watcher() {
        @Override
        public void process(WatchedEvent watchedEvent) {
            //创建连接成功
            if (watchedEvent.getState()==Event.KeeperState.SyncConnected){
                System.out.println("zookeeper连接创建成功!");
                //countDownLatch.countDown();
            }
        }
    });
    //countDownLatch.await();
}
1.2.新增节点
1.2.1.同步创建
public void test1() throws KeeperException, InterruptedException, IOException {
    //1.path: 节点路径
    //2.data: 节点数据
    //3.acl: acl权限列表:OPEN_ACL_UNSAFE、CREATOR_ALL_ACL、READ_ACL_UNSAFE
    //4.createMode: 节点类型:PERSISTENT、PERSISTENT_SEQUENTIAL、EPHEMERAL、EPHEMERAL_SEQUENTIAL
    zooKeeper.create("/test/node1","abc".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
1.2.2.异步创建
public void test1() throws KeeperException, InterruptedException, IOException {
    zooKeeper.create("/test/node1", "abc".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new AsyncCallback.StringCallback() {
            @Override
        public void processResult(int i, String path, Object ctx, String name) {
            //返回状态:0--成功
            System.out.println(i);
            //节点路径
            System.out.println(path);
            //上下文参数
            System.out.println(ctx);
            //节点路径
            System.out.println(name);
        }
    },"Object ctx");
}
1.2.2.只读权限(acl)
public void test2() throws KeeperException, InterruptedException {
    //1.ZooDefs.Ids.READ_ACL_UNSAFE  world:anyone:r
    zooKeeper.create("/test/node2","abc".getBytes(), ZooDefs.Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT);
}
1.2.3.world授权模式
public void test3() throws KeeperException, InterruptedException {
    //world授权模式

    //1.权限列表
    List<ACL> aclList=new ArrayList<>();
    //2.权限模式(scheme)、授权对象(id)
    Id id=new Id("world","anyone");
    //3.权限设置:
    //        int READ = 1;
    //        int WRITE = 2;
    //        int CREATE = 4;
    //        int DELETE = 8;
    //        int ADMIN = 16;
    //        int ALL = 31;
    aclList.add(new ACL(ZooDefs.Perms.READ,id));
    aclList.add(new ACL(ZooDefs.Perms.WRITE,id));
    aclList.add(new ACL(ZooDefs.Perms.CREATE,id));
    aclList.add(new ACL(ZooDefs.Perms.DELETE,id));
    aclList.add(new ACL(ZooDefs.Perms.ADMIN,id));

    zooKeeper.create("/test/node3","abc".getBytes(), aclList, CreateMode.PERSISTENT);
}
1.2.4.ip授权模式
public void test4() throws KeeperException, InterruptedException {
    //ip授权模式

    //1.权限列表
    List<ACL> aclList=new ArrayList<>();
    //2.权限模式(scheme)、授权对象(id)
    Id id=new Id("ip","192.168.31.220");
    //3.权限设置:
    //        int ALL = 31;
    aclList.add(new ACL(ZooDefs.Perms.ALL,id));

    zooKeeper.create("/test/node4","abc".getBytes(), aclList, CreateMode.PERSISTENT);
}
1.2.5.auth授权模式
public void test5() throws KeeperException, InterruptedException {
    //auth授权模式

    //1.添加授权用户
    zooKeeper.addAuthInfo("digest","wzb:123456".getBytes());

    zooKeeper.create("/test/node5","abc".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
}
1.2.6.auth授权模式2
public void test6() throws KeeperException, InterruptedException {
    //auth授权模式

    //1.添加授权用户
    zooKeeper.addAuthInfo("digest","wzb:123456".getBytes());

    //2.权限列表
    List<ACL> aclList=new ArrayList<>();
    //3.权限模式(scheme)、授权对象(id)
    Id id=new Id("auth","wzb");
    //4.权限设置:
    //        int ALL = 31;
    aclList.add(new ACL(ZooDefs.Perms.ALL,id));

    zooKeeper.create("/test/node4","abc".getBytes(), aclList, CreateMode.PERSISTENT);
}
1.2.7.digest授权模式
public void test7() throws KeeperException, InterruptedException {
    //digest授权模式

    //1.权限列表
    List<ACL> aclList=new ArrayList<>();
    //2.权限模式(scheme)、授权对象(id)
    Id id=new Id("digest","wzb:/jkGdS5slDCObV2mWEt7Yf2Xb98=");
    //3.权限设置:
    //        int ALL = 31;
    aclList.add(new ACL(ZooDefs.Perms.ALL,id));

    zooKeeper.create("/test/node4","abc".getBytes(), aclList, CreateMode.PERSISTENT);
}
1.3.修改节点
1.3.1.同步修改
public void test8() throws KeeperException, InterruptedException {
    //1.path: 节点路径
    //2.data: 节点数据
    //3.version: 版本号 -1:不比较版本号
    //4.StatCallback: 回调接口
    //5.ctx: 上下文参数
    zooKeeper.setData("/test/node1", "abc".getBytes(), -1);
}
1.3.2.异步修改
public void test8() throws KeeperException, InterruptedException {
    //1.path: 节点路径
    //2.data: 节点数据
    //3.version: 版本号 -1:不比较版本号
    //4.StatCallback: 回调接口
    //5.ctx: 上下文参数
    zooKeeper.setData("/test/node1", "abc".getBytes(), -1, new AsyncCallback.StatCallback() {
        @Override
        public void processResult(int i, String path, Object ctx, Stat stat) {
            //stat:节点属性对象
            System.out.println(stat.toString());
        }
    },"Object ctx");
}
1.4.删除节点
1.4.1.同步删除
public void test9() throws KeeperException, InterruptedException {
    //1.path: 节点路径
    //2.version: 版本号 -1:不比较版本号
    zooKeeper.delete("/test/node1", -1);
}
1.4.2.异步删除
public void test9() throws KeeperException, InterruptedException {
    //1.path: 节点路径
    //2.version: 版本号 -1:不比较版本号
    //3.VoidCallback: 回调接口
    //4.ctx: 上下文参数
    zooKeeper.delete("/test/node1", -1, new AsyncCallback.VoidCallback() {
        @Override
        public void processResult(int i, String path, Object ctx) {

        }
    },"Object ctx");
}
1.5.获取节点数据
1.5.1.同步获取
public void test10() throws KeeperException, InterruptedException {
    //1.path: 节点路径
    //2.watch: 是否使用创建连接中注册的监听器
    //3.stat: 节点属性对象
    Stat stat=new Stat();
    byte[] data = zooKeeper.getData("/test/node1", false, stat);
}
1.5.2.异步获取
public void test10() throws KeeperException, InterruptedException {
    //1.path: 节点路径
    //2.watch: 是否使用创建连接中注册的监听器
    //3.DataCallback: 回调接口
    //4.ctx: 上下文参数

    zooKeeper.getData("/test/node1", false, new AsyncCallback.DataCallback() {
        @Override
        public void processResult(int i, String path, Object ctx, byte[] data, Stat stat) {
            //data:数据
            System.out.println(new String(data));
        }
    },"Object ctx");
}
1.6.获取子节点数据
1.6.1.同步获取
public void test11() throws KeeperException, InterruptedException {
    //1.path: 节点路径
    //2.watch: 是否使用创建连接中注册的监听器

    //注意:返回的是节点名称而非路径
    List<String> children = zooKeeper.getChildren("/test", false);
}
1.6.2.异步获取
public void test11() throws KeeperException, InterruptedException {
    //1.path: 节点路径
    //2.watch: 是否使用创建连接中注册的监听器
    //3.ChildrenCallback: 回调接口
    //4.ctx: 上下文参数

    zooKeeper.getChildren("/test", false, new AsyncCallback.ChildrenCallback() {
        @Override
        public void processResult(int i, String path, Object ctx, List<String> children) {

        }
    }, "Object ctx");
}
1.7.判断节点是否存在
1.7.1.同步获取
public void test12() throws KeeperException, InterruptedException {
    //1.path: 节点路径
    //2.watch: 是否使用创建连接中注册的监听器

    //注意:stat为null代表节点不存在
    Stat stat = zooKeeper.exists("/test", false);
}
1.7.2.异步获取
public void test12() throws KeeperException, InterruptedException {
    //1.path: 节点路径
    //2.watch: 是否使用创建连接中注册的监听器
    //3.StatCallback: 回调接口
    //4.ctx: 上下文参数

    zooKeeper.exists("/test/node1", false, new AsyncCallback.StatCallback() {
        @Override
        public void processResult(int i, String path, Object ctx, Stat stat) {

        }
    }, "Object ctx");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值