阅读 108

Hadoop基础(三十五):Zookeeper API 应用 客户端模式

1 Vscode 环境搭建

1.1.创建一个 Maven 工程

 
参照
 

1.2.添加 pom 文件

 <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.13version>
    dependency>
    <dependency>
      <groupId>org.apache.zookeepergroupId>
      <artifactId>zookeeperartifactId>
      <version>3.4.14version>
    dependency>
    <dependency>
      <groupId>org.apache.logging.log4jgroupId>
      <artifactId>log4j-coreartifactId>
      <version>2.8.2version>
    dependency>
 dependencies>
1.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 客户端

 

 

3 创建子节点

// 创建子节点
@Test
public void create() throws Exception {
// 参数 1:要创建的节点的路径; 参数 2:节点数据 ; 参数 3:节点权限 ;参数 4:节点的类型
String nodeCreated = zkClient.create("/atguigu", 
"jinlian".getBytes(), Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
}

 

4 获取子节点并监听节点变化

 

5 判断 Znode 是否存在

 
package com.atguigu.zkclient;

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

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;

/**
 * a simple zkclient.
 */
public class ZkClient {

    private static final int SESSION_TIMEOUT = 300000;
    private static final String CONNECT_STRING = "192.168.1.122:2181,192.168.1.133:2181,192.168.1.144:2181";
    private ZooKeeper zkCli;

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

    @Test
    public void ls() throws IOException, InterruptedException, KeeperException {
        //查看文件信息
        List children = zkCli.getChildren("/", true);
        System.out.println("==========================================");
        for (String child : children) {
            System.out.println(child);
        }
        System.out.println("==========================================");
    }

    @Test
    public void create() throws IOException, InterruptedException, KeeperException {
        String s = zkCli.create("/Idea", "Idea2018".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println(s);

        Thread.sleep(Long.MAX_VALUE);
    }

    @Test
    public void get() throws IOException, KeeperException, InterruptedException {
        byte[] data = zkCli.getData("/zxx00000007", true, new Stat());
        String string = new String(data);

        System.out.println(string);
    }

    @Test
    public void set() throws InterruptedException, KeeperException {
        Stat stat = zkCli.setData("/zxx00000007", "defabc".getBytes(), 0);
        System.out.println(stat.getDataLength());
    }

    @Test
    public void stat() throws KeeperException, InterruptedException {
        Stat exists =  zkCli.exists("/zxx00000007", false);
        if (exists == null) {
            System.out.println("节点不存在");
        } else {
            System.out.println(exists.getDataLength());
        }
    }

    @Test
    public void delete() throws KeeperException, InterruptedException {
        Stat exists = zkCli.exists("/zxx00000007", false);
        if (exists != null)
           zkCli.delete("/zxx00000007", exists.getVersion());
    }

    @Test
    public void register() throws KeeperException, InterruptedException {
        byte[] data = zkCli.getData("/a", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                try {
                    register();
                } catch (KeeperException e) {
                    //TODO: handle exception
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, null);
        System.out.println(new String(data));
    }

    @Test
    public void testRegister() {
        try {
            register();
            Thread.sleep(Long.MAX_VALUE);
        } catch (KeeperException e) {
            //TODO: handle exception
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

 

原文:https://www.cnblogs.com/qiu-hua/p/13352502.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐