zoukankan      html  css  js  c++  java
  • zookeeper api

    **************************************ZkApiTest **************************************
    ZooKeeper zooKeeper=new ZooKeeper("192.168.157.128:2181", 2000, new Watcher() {

    **************************************************************************
    **************************************************************************
     
    package com.itheima.zkDemo;

    import org.apache.zookeeper.*;
    import org.apache.zookeeper.data.Stat;
    import org.junit.jupiter.api.Test;

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

    public class ZkApiTest {
    @Test
    public void test() throws IOException, KeeperException, InterruptedException {

    // 1、创建zookeeper连接
    ZooKeeper zooKeeper=new ZooKeeper("192.168.157.128:2181", 2000, new Watcher() {
    public void process(WatchedEvent watchedEvent) {
    System.out.println("触发了"+watchedEvent.getType()+"的事件");
    }
    });

    // 2、创建父节点
    //String path=zooKeeper.create("/itheima","itheimaValue".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    //System.out.println(path);
    // 3、创建子节点
    //String childrenpath=zooKeeper.create("/itheima/children","childrenValue".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    // System.out.println(childrenpath);
    // 4、获取节点中的值(父节点和子节点)
    // byte[] data=zooKeeper.getData("/itheima",false,null);
    //System.out.println(new String(data));
    // List<String> children=zooKeeper.getChildren("/itheima",false);
    // for(String child:children)
    // {
    // System.out.println(child);
    // }
    // 5、修改节点的值
    //Stat stat=zooKeeper.setData("/itheima","itheimaUpdate".getBytes(),-1);
    // System.out.println(stat);
    // 6、判断某个节点是否存在
    //Stat exists=zooKeeper.exists("/itheima/children",false);
    //System.out.println(exists);
    // 7、删除节点
    zooKeeper.delete("/itheima/children",-1);
    }

    }
  • 相关阅读:
    RQNOJ 342 最不听话的机器人:网格dp
    RQNOJ 329 刘翔!加油!:01背包
    RQNOJ 57 找啊找啊找GF:01背包
    RQNOJ 202 奥运火炬登珠峰:01背包
    RQNOJ 201 奥运大包围:LIS + 拼链成环
    2017SN多校D1T2 note:dp
    2017SN多校D1T1 loveletter:模拟
    HDU 2157 How many ways??:矩阵快速幂【i到j共经过k个节点的方法数】
    poj 3764 The xor-longest Path
    bzoj 1192 鬼谷子的钱袋
  • 原文地址:https://www.cnblogs.com/smallfa/p/12019288.html
Copyright © 2011-2022 走看看