Zookeeper学习笔记(三) API使用

1 IDEA环境搭建

1)创建一个Maven Module

2)添加pom文件

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.5.7</version>
        </dependency>
</dependencies>

3)拷贝log4j.properties文件到项目根目录
需要在项目的src/main/resources目录下,新建一个文件,命名为“log4j.properties”,在文件中填入。

log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

2 初始化ZooKeeper客户端

public class Zookeeper &#123;

private String connectString;
private int sessionTimeout;
private ZooKeeper zkClient;

@Before   //获取客户端对象

public void init() throws IOException &#123;

​    connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";int sessionTimeout = 10000;//参数解读 1集群连接字符串  2连接超时时间 单位:毫秒  3当前客户端默认的监控器
​    zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() &#123;@Overridepublic void process(WatchedEvent event) &#123;&#125;&#125;);
&#125;

@After //关闭客户端对象
public void close() throws InterruptedException &#123;
    zkClient.close();
&#125;

&#125;

3 获取子节点列表,不监听

@Test
public void ls() throws IOException, KeeperException, InterruptedException &#123;
    //用客户端对象做各种操作
    List<String> children = zkClient.getChildren("/", false);
    System.out.println(children);
&#125;

4 获取子节点列表,并监听

@Test
public void lsAndWatch() throws KeeperException, InterruptedException &#123;
    List<String> children = zkClient.getChildren("/atguigu", new Watcher() &#123;
        @Override
        public void process(WatchedEvent event) &#123;
            System.out.println(event);
        &#125;
    &#125;);
System.out.println(children);
    //因为设置了监听,所以当前线程不能结束
Thread.sleep(Long.MAX_VALUE);
    &#125;

5 创建子节点

@Test
public void create() throws KeeperException, InterruptedException &#123;
//参数解读 1节点路径  2节点存储的数据  
//3节点的权限(使用Ids选个OPEN即可) 4节点类型 短暂 持久 短暂带序号 持久带序号
     String path = zkClient.create("/atguigu", "shanguigu".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

//创建临时节点

//String path = zkClient.create("/atguigu2", "shanguigu".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);

System.out.println(path);

//创建临时节点的话,需要线程阻塞
//Thread.sleep(10000);

&#125;

6 判断Znode是否存在

@Test
public void exist() throws Exception &#123;

Stat stat = zkClient.exists("/atguigu", false);
System.out.println(stat == null ? "not exist" : "exist");

&#125;

7 获取子节点存储的数据,不监听

@Test
public void get() throws KeeperException, InterruptedException &#123;
    //判断节点是否存在
    Stat stat = zkClient.exists("/atguigu", false);
    if (stat == null) &#123;
        System.out.println("节点不存在...");
        return;
    &#125;

byte[] data = zkClient.getData("/atguigu", false, stat);
System.out.println(new String(data));

&#125;

8 获取子节点存储的数据,并监听

@Test
public void getAndWatch() throws KeeperException, InterruptedException &#123;
    //判断节点是否存在
    Stat stat = zkClient.exists("/atguigu", false);
    if (stat == null) &#123;
        System.out.println("节点不存在...");
        return;
    &#125;

    byte[] data = zkClient.getData("/atguigu", new Watcher() &#123;
        @Override
        public void process(WatchedEvent event) &#123;
            System.out.println(event);
        &#125;
    &#125;, stat);
    System.out.println(new String(data));
    //线程阻塞
    Thread.sleep(Long.MAX_VALUE);
&#125;

9 设置节点的值

@Test
public void set() throws KeeperException, InterruptedException &#123;
    //判断节点是否存在
    Stat stat = zkClient.exists("/atguigu", false);
    if (stat == null) &#123;
        System.out.println("节点不存在...");
        return;
    &#125;
    //参数解读 1节点路径 2节点的值 3版本号
    zkClient.setData("/atguigu", "sgg".getBytes(), stat.getVersion());
&#125;

10 删除空节点

@Test
public void delete() throws KeeperException, InterruptedException &#123;
    //判断节点是否存在
    Stat stat = zkClient.exists("/aaa", false);
    if (stat == null) &#123;
        System.out.println("节点不存在...");
        return;
    &#125;
    zkClient.delete("/aaa", stat.getVersion());
&#125;

11 删除非空节点,递归实现

//封装一个方法,方便递归调用
public void deleteAll(String path, ZooKeeper zk) throws KeeperException, InterruptedException &#123;
    //判断节点是否存在
    Stat stat = zkClient.exists(path, false);
    if (stat == null) &#123;
        System.out.println("节点不存在...");
        return;
    &#125;
    //先获取当前传入节点下的所有子节点
    List<String> children = zk.getChildren(path, false);
    if (children.isEmpty()) &#123;
        //说明传入的节点没有子节点,可以直接删除
        zk.delete(path, stat.getVersion());
    &#125; else &#123;
        //如果传入的节点有子节点,循环所有子节点
        for (String child : children) &#123;
            //删除子节点,但是不知道子节点下面还有没有子节点,所以递归调用
            deleteAll(path + "/" + child, zk);  
        &#125;
        //删除完所有子节点以后,记得删除传入的节点
        zk.delete(path, stat.getVersion());
    &#125;
&#125;
//测试deleteAll
@Test
public void testDeleteAll() throws KeeperException, InterruptedException &#123;
    deleteAll("/atguigu",zkClient);
&#125;
文章目录
  1. 1 IDEA环境搭建
  2. 2 初始化ZooKeeper客户端
  3. 3 获取子节点列表,不监听
  4. 4 获取子节点列表,并监听
  5. 5 创建子节点
  6. 6 判断Znode是否存在
  7. 8 获取子节点存储的数据,并监听
  8. 9 设置节点的值
  9. 10 删除空节点
  10. 11 删除非空节点,递归实现