大数据3_03_zookeeper的API操作

4 ZooKeeper的API操作

4.1 环境准备

步骤1:创建一个Maven工程,添加pom文件

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.5.7</version>
        </dependency>
    </dependencies>

步骤2:在/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  

4.2 创建zookeeper客户端

创建节点
package com.codejiwei.zookeeper;
import org.apache.zookeeper.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class ZKClient2 {
    private ZooKeeper zooKeeper = null;
    @Before
    public void before() throws IOException {
        zooKeeper = new ZooKeeper(
                //zookeeper集群的所有节点的连接,2181是客户端连接的端口
                "hadoop102:2181,hadoop103:2181,hadoop104:2181",
                //通信心跳数,zookeeper服务器与客户端心跳时间
                2000,
                //系统默认监听器
                new Watcher() {
                    @Override
                    public void process(WatchedEvent watchedEvent) {
                        System.out.println(watchedEvent.getType() + "---" + watchedEvent.getPath());
                    }
                });
    }
    @After
    public void after(){
        try {
            zooKeeper.close();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void testCreate() throws KeeperException, InterruptedException {
        zooKeeper.create(
                //节点名称
                "/aaa/liu",
                //节点内的数据
                "zixin".getBytes(),
                //节点访问列表权限
                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                //节点的类型
                CreateMode.PERSISTENT);
    }
}

其中节点的四种类型API中为:

  • 永久、无序号节点:PERSISTENT
  • 永久、带序号节点:PERSISTENT_SEQUENTIAL
  • 临时、无序号节点:EPHEMERAL
  • 临时、有序号节点:EPHEMERAL_SEQUENTIAL
查看当前节点下所有子节点
    @Test
    public void testLs() throws KeeperException, InterruptedException {
        //true表示使用默认的监视器
        List<String> children = zooKeeper.getChildren("/aaa", true);
        //遍历输出/aaa下的子节点
        for (String child : children) {
            System.out.println(child);
        }
        //延时阻塞
        Thread.sleep(Long.MAX_VALUE);
    }

在zkCli创建一个/aaa/sun的节点,显示:

image-20201019182800464

stat查看节点详细信息
    @Test
    public void testStat() throws KeeperException, InterruptedException {
        Stat stat = zooKeeper.exists(
                "/aaa",
                //自定义的监听器
                new Watcher() {
                    @Override
                    public void process(WatchedEvent watchedEvent) {
                        System.out.println(watchedEvent.getPath() + "==" + watchedEvent.getType());
                    }
                });
        if (stat == null){
            System.out.println("节点不存在");
        }else {
            long pzxid = stat.getPzxid();//创建节点的zxid,时间戳,事务id
            long mzxid = stat.getMzxid();//最后修改节点的zxid
            long czxid = stat.getCzxid();//最后修改子节点的zxid
            int version = stat.getVersion();//该节点版本
            int dataLength = stat.getDataLength();//数据长度
            long ephemeralOwner = stat.getEphemeralOwner();//节点类型
            int numChildren = stat.getNumChildren();//子节点个数
            System.out.println(pzxid);
            System.out.println(mzxid);
            System.out.println(czxid);
            System.out.println(version);
            System.out.println(dataLength);
            System.out.println(ephemeralOwner);
            System.out.println(numChildren);
        }
        Thread.sleep(Integer.MAX_VALUE);
    }

此时,对/aaa节点的data修改:set /aaa mmmm

image-20201019184011736

获取某个节点的值
    @Test
    public void testGet() throws KeeperException, InterruptedException, IOException {
        byte[] data = zooKeeper.getData("/aaa", false, new Stat());
        System.out.write(data);
    }
设置某个节点的值
    @Test
    public void testSet() throws KeeperException, InterruptedException {
        //首先获取给节点的stat,从stat中可以获取该节点的version
        Stat stat = zooKeeper.exists("/aaa", false);
        if (stat == null){
            System.out.println("节点不存在");
        }else {
            zooKeeper.setData("/aaa", "hadoop".getBytes(), stat.getVersion());
        }
    }
删除某个节点

zookeeper不提供递归删除的API

    @Test
    public void testDelete() throws KeeperException, InterruptedException {
        Stat stat = zooKeeper.exists("/aaa/ji", false);
        if (stat == null){
            System.out.println("节点不存在");
        }else {
            zooKeeper.delete("/aaa/ji", stat.getVersion());
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

最佳第六六六人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值