Zookeeper Java API操作

目录

前提条件

一、安装zookeeper单机模式

二、Java API操作


前提条件

1.1台Linux环境机器

2.Linux下安装好jdk8

3.Windows安装好IDEA

一、安装zookeeper单机模式

下载

Index of /dist/zookeeper

下载文件apache-zookeeper-3.8.0-bin.tar.gz

解压

$ tar -zxvf apache-zookeeper-3.8.0-bin.tar.gz -C ~/soft/

建立软链接

[hadoop@node1 installfile]$ cd ~/soft/
​
[hadoop@node1 soft]$ ln -s apache-zookeeper-3.8.0-bin zookeeper

添加zookeeper环境变量

修改~/.bashrc文件,

[hadoop@node1 soft]$ nano ~/.bashrc 

添加如下内容:

export ZOOKEEPER_HOME=~/soft/zookeeper
export PATH=$PATH:$ZOOKEEPER_HOME/bin

让环境变量生效

[hadoop@node1 soft]$ source ~/.bashrc 

配置zookeeper

进入配置目录

[hadoop@node1 soft]$ cd $ZOOKEEPER_HOME/conf 
[hadoop@node1 conf]$ ls
configuration.xsl  logback.xml  zoo_sample.cfg

复制模板文件zoo_sample.cfgzoo.cfg

[hadoop@node1 conf]$ cp zoo_sample.cfg zoo.cfg

修改zoo.cfg

dataDir=/home/hadoop/soft/zookeeper/data

启动zookeeper服务

[hadoop@node1 conf]$ zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/hadoop/soft/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
​

查看zookeeper状态

[hadoop@node1 conf]$ zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/hadoop/soft/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: standalone

进入zookeeper客户端

[hadoop@node1 conf]$ zkCli.sh
​
...
​
[zk: localhost:2181(CONNECTED) 0] 
​

二、Java API操作

1.新建maven工程

2.添加依赖

   <dependencies>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.8.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

3.编写代码

代码路径:src/test/java/org/example/zkClient.java

package org.example;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;

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

public class zkClient {
    private String connectString = "192.168.193.140:2181";//注意修改
    private int sessionTimeout = 2000;
    private ZooKeeper zkClient;

    @Before
    public void init() throws IOException {
        zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {

            }
        });
    }

    /**
     * 创建节点
     */
    @Test
    public void create() throws InterruptedException, KeeperException {
        String nodeCreated = zkClient.create("/zkapi", "hello world".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }

    /**
     * 节点是否存在
     */
    @Test
    public void exist() throws InterruptedException, KeeperException {
        Stat exists = zkClient.exists("/zkapi", false);
        System.out.println(exists==null? "not exist": "exist");
    }

    /*
     * 获取子节点
     */
    @Test
    public void getChildren() throws InterruptedException, KeeperException {
        List<String> children = zkClient.getChildren("/", true);//不会递归查询

        for (String child : children) {
            System.out.println(child);
        }
    }

    /*
     * 获取数据
     */
    @Test
    public void getData() throws InterruptedException, KeeperException {
        byte[] data = zkClient.getData("/test", false, null);
//        System.out.println(data);//[B@54a097cc
        System.out.println(new String(data));
    }

    /*
     * 修改数据
     */
    @Test
    public void updData() throws InterruptedException, KeeperException {
        Stat stat = zkClient.setData("/test", "hello zk".getBytes(), zkClient.exists("/test", true).getVersion());
        System.out.println(stat.getVersion());
    }

    /*
     * 删除数据
     */
    @Test
    public void delData() throws InterruptedException, KeeperException {
        zkClient.delete("/zkapi",zkClient.exists("/zkapi",false).getVersion());
    }
    
}

完成!enjoy it!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值