zookeeper_API

ZooKeeper API的基础知识

与ZooKeeper集合进行交互的应用程序称为 ZooKeeper客户端或简称客户端

Znode是ZooKeeper集合的核心组件,ZooKeeper API提供了一小组方法使用ZooKeeper集合来操纵znode的所有细节。

客户端应该遵循以步骤,与ZooKeeper集合进行清晰和干净的交互。

  • 连接到ZooKeeper集合。ZooKeeper集合为客户端分配会话ID。

  • 定期向服务器发送心跳。否则,ZooKeeper集合将过期会话ID,客户端需要重新连接。

  • 只要会话ID处于活动状态,就可以获取/设置znode。

  • 所有任务完成后,断开与ZooKeeper集合的连接。如果客户端长时间不活动,则ZooKeeper集合将自动断开客户端。

Java绑定

让我们来了解本章中最重要的一组ZooKeeper API。ZooKeeper API的核心部分是ZooKeeper类。它提供了在其构造函数中连接ZooKeeper集合的选项,并具有以下方法:

  • connect - 连接到ZooKeeper集合

  • create- 创建znode

  • exists- 检查znode是否存在及其信息

  • getData - 从特定的znode获取数据

  • setData - 在特定的znode中设置数据

  • getChildren - 获取特定znode中的所有子节点

  • delete - 删除特定的znode及其所有子项

  • close - 关闭连接

package com.atguigu.zookeeper;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

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

public class test01 {
    public static void main(String[] args) throws Exception {
        ZKClient client = new ZKClient();

        client.connect();
        client.ls();
        client.create();
        client.get();
        client.set();
        client.delete();

    }
}

class ZKClient {

    private ZooKeeper zkCli;
    private static final String CONNECT_STRING = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
    private static final int SESSION_TIMEOUT = 2000;


    public void connect()throws IOException {
        zkCli = new ZooKeeper(CONNECT_STRING,SESSION_TIMEOUT,e->{
            System.out.println("默认回调函数");
        });
    }

    public void ls() throws KeeperException,InterruptedException {
        List<String> children = zkCli.getChildren("/",e->{
            System.out.println("自定义回调函数");
        });
        System.out.println("*****************");
        for(String child:children){
            System.out.println(child);
        }
        System.out.println("******************");
        Thread.sleep(Long.MAX_VALUE);
    }

    public void create() throws KeeperException,InterruptedException{
        String s = zkCli.create("/xxxx", "xxxxx".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println(s);
        Thread.sleep(Long.MAX_VALUE);
    }

    public void get() throws KeeperException,InterruptedException{
        byte[] data = zkCli.getData("/c", true, new Stat());
        String s = new String(data);
        System.out.println(s);
    }

    public void set() throws KeeperException,InterruptedException{
        Stat exists = zkCli.exists("/c", false);
        if (exists != null){
            Stat stat = zkCli.setData("/c", "c111111111".getBytes(), exists.getVersion());
            System.out.println(stat.getDataLength());
        }else{
            System.out.println("节点不存在");
        }

    }

    public void delete() throws KeeperException,InterruptedException{
        Stat exists = zkCli.exists("/c", false);
        if(exists != null){
            zkCli.delete("/c",exists.getVersion());
        }else{
            System.out.println("节点不存在");
        }
    }




}

 

posted on 2020-11-30 16:14  happygril3  阅读(144)  评论(0)    收藏  举报

导航