ZooKeeper-3.4.10 Java API的使用样例

大数据 专栏收录该内容
19 篇文章 0 订阅

ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务框架,包含一组简单的原语集合。通过这些原语言的组合使用,能够帮助我们解决更高层次的分布式问题。

package demo.jdbc.zookeeper.main;

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 SimpleZKClient {

    private static final String connectString = "Master:2181,Slave1:2181,Slave2:2181";
    private static final int sessionTimeout = 2000;

    private static ZooKeeper zooKeeper = null;

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        getConnection();
        if (testExist() == null) {
            testCreate();
        }
        getChildren();
        getData();
        setData();
        getData();
        deleteZNode();
        testExist();
    }

    /**
     * 初始化zooKeeper
     *
     * @throws IOException
     */
    public static void getConnection() throws IOException {
        zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent event) {

            }
        });
    }

    /**
     * @return
     * @throws KeeperException
     * @throws InterruptedException
     */
    public static Stat testExist() throws KeeperException, InterruptedException {
        Stat stat = zooKeeper.exists("/eclipse", false);
        if (stat == null) {
            System.out.println("NOT EXIST");
        } else {
            System.out.println("EXIST");
        }
        return stat;
    }

    /**
     * 在zk中创建数据节点
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    public static void testCreate() throws KeeperException, InterruptedException {
        // 参数1:创建的节点的路径   参数2:节点的数据   参数3:节点的权限   参数4:节点的类型
        zooKeeper.create("/eclipse", "eclipse".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        // 上传的数据可以是任何类型,但是要转换成byte[]
    }

    /**
     * 获取子节点
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    public static void getChildren() throws KeeperException, InterruptedException {
        List<String> children = zooKeeper.getChildren("/", true);
        System.out.println("ZNode List:{");
        for (String child : children) {
            System.out.println(child);
        }
        System.out.println("}");
    }

    /**
     * 获取ZNode中的数据
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    public static void getData() throws KeeperException, InterruptedException {
        byte[] data = zooKeeper.getData("/eclipse", false, null);
        System.out.println("data:" + new String(data));
    }

    /**
     * 修改ZNode中的数据
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    public static void setData() throws KeeperException, InterruptedException {
        zooKeeper.setData("/eclipse", "eclipse.new".getBytes(), -1);
    }

    /**
     * 删除ZNode
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    public static void deleteZNode() throws KeeperException, InterruptedException {
        // 参数2:指定要删除的版本   -1表示删除所有的版本
        zooKeeper.delete("/eclipse", -1);
    }

}

输出结果:

log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
NOT EXIST
ZNode List:{
zookeeper
eclipse
}
data:eclipse
data:eclipse.new
NOT EXIST

Process finished with exit code 0

依赖的Jar 包

jline-0.9.94.jar
log4j-1.2.16.jar
netty-3.10.5.Final.jar
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.1.jar
zookeeper-3.4.10.jar

注:依赖的Jar包均可以在zookeeper-3.4.10.tar.gz安装包里面找到。

Over

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值