栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Zookeeper

Zookeeper

目录

1.IDEA 环境搭建

1)创建一个工程:zookeeper

2)添加pom文件

3)拷贝log4j.properties文件到项目根目录

4)创建包名com.atguigu.zk

5)创建类名称zkClient

2.创建 ZooKeeper 客户端

1)初始化

2)创建子节点

3)获取子节点

4)监听节点变化

5)判断 Znode 是否存在

6)完整代码


前提:保证 hadoop102、hadoop103、hadoop104 服务器上 Zookeeper 集群服务端启动。

1.IDEA 环境搭建

1)创建一个工程:zookeeper

2)添加pom文件

在最后一行前添加以下内容

    
        
            junit
            junit
            RELEASE
        

        
            org.apache.logging.log4j
            log4j-core
            2.8.2

        

        
            org.apache.zookeeper
            zookeeper
            3.5.7
        
    

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

4)创建包名com.atguigu.zk

5)创建类名称zkClient

2.创建 ZooKeeper 客户端

1)初始化
    // 初始化
    @Before
    public void init() throws IOException {
        //ZooKeeper对应的包刚刚开始不能导入,关闭idea后可能是重新加载了Zookeeper,可以导入了
        zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
            }
        });
    }

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

3)获取子节点
    // 获取子节点
    @Test
    public void getChildren() throws KeeperException, InterruptedException {
        //第二参数代表是否用初始化的watcher
        List children = zkClient.getChildren("/", true);

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

        // 延时
        Thread.sleep(Long.MAX_VALUE);
    }

4)监听节点变化

为了监听到节点的变化,将监听内容放入初始化new的Watcher的process里,使得节点变化都能在控制台上输出

创建atguigu2--->创建atguigu3后的结果:

5)判断 Znode 是否存在
    // 判断znode是否存在
    @Test
    public void exist() throws KeeperException, InterruptedException {
        Stat stat = zkClient.exists("/atguigu1", false);

        System.out.println(stat == null? "not exist" : "exist");
    }

6)完整代码
public class zkClient {
    //hadoop102已经配置好与对应的ip映射,如果没配置,这里hadoop102写成ip,""里的,左右不能加空格
    private String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
    private int sessionTimeout = 2000;
    private ZooKeeper zkClient;

    // 初始化
    @Before
    public void init() throws IOException {
        //ZooKeeper对应的包刚刚开始不能导入,关闭idea后可能是重新加载了Zookeeper,可以导入了
        zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("--------------------");
                List children = null;
                try {
                    children = zkClient.getChildren("/", true);
                    for (String child : children) {
                        System.out.println(child);
                    }
                    System.out.println("--------------------");
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

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

    // 获取子节点
    @Test
    public void getChildren() throws KeeperException, InterruptedException {
        //第二参数代表是否用初始化的watcher
        List children = zkClient.getChildren("/", true);

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

        // 延时
        Thread.sleep(Long.MAX_VALUE);
    }

    // 判断znode是否存在
    @Test
    public void exist() throws KeeperException, InterruptedException {
        Stat stat = zkClient.exists("/atguigu1", false);

        System.out.println(stat == null? "not exist" : "exist");
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/722305.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号