栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

zookeeper的增删改查

zookeeper的增删改查

private Curatorframework client;

@Before
    public void testConnect(){
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
        client = CuratorframeworkFactory.builder().connectString("127.0.0.1:2181")
                .sessionTimeoutMs(60 * 100)
                .connectionTimeoutMs(15 * 1000)
                .retryPolicy(retryPolicy).namespace("it").build();
        client.start();

    }



    @Test
    public void testCreate() throws Exception {
        client.create().creatingParentContainersIfNeeded().forPath("/it/app1");//同时创建父节点和子节点

        String path = client.create().forPath("/app1");
        System.out.println(path);
    }





    @Test
    public void testGet() throws Exception {
        byte[] bytes = client.getData().forPath("/app1");//当前节点信息
        System.out.println(new String(bytes));

        List strings = client.getChildren().forPath("/");//查询子节点
        System.out.println(strings);

        Stat stat = new Stat();
        client.getData().storingStatIn(stat).forPath("/app1");//将节点所有信息记录到stat
        System.out.println(stat);
    }



    @Test
    public void testSet() throws Exception {
        Stat stat = new Stat();
        client.getData().storingStatIn(stat).forPath("/app1");
        int version = stat.getVersion();
        client.setData().withVersion(version).forPath("/","hhh".getBytes(StandardCharsets.UTF_8));//根据版本修改若版本不对这修改失败
    }



    @Test
    public void testDelete() throws Exception {

        client.delete().forPath("/app1");

        client.delete().deletingChildrenIfNeeded().forPath("/it");//有子节点也删除

        client.delete().guaranteed().forPath("/app1"); //guaranteed为保证被删除(反复尝试)

        //返回被删除节点信息
        client.delete().guaranteed().inBackground(new BackgroundCallback() {
            @Override
            public void processResult(Curatorframework curatorframework, CuratorEvent curatorEvent) throws Exception {
                System.out.println(curatorEvent);
            }
        }).forPath("/app1");
    }



 @After
    public void closer(){
        client.close();
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/584414.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号