zoukankan      html  css  js  c++  java
  • Hadoop基础(三十五):Zookeeper API 应用 客户端模式

    1 Vscode 环境搭建

    1.1.创建一个 Maven 工程

     
     

    1.2.添加 pom 文件

     <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.13</version>
        </dependency>
        <dependency>
          <groupId>org.apache.zookeeper</groupId>
          <artifactId>zookeeper</artifactId>
          <version>3.4.14</version>
        </dependency>
        <dependency>
          <groupId>org.apache.logging.log4j</groupId>
          <artifactId>log4j-core</artifactId>
          <version>2.8.2</version>
        </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<String> 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

  • 相关阅读:
    zabbix笔记之计算型监控项详解
    zabbix笔记之磁盘IO介绍
    zabbix笔记之Graphtree配置
    zabbix笔记之告警时远程执行命令
    zabbix笔记之异常优化
    zabbix笔记之IPMI配置
    基本的sql 语句
    socket 套接字
    调用父类的三种方法
    实例属性和类属性
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/13352502.html
Copyright © 2011-2022 走看看