一.配置开发环境

在之前的testHadoop下新建模块testZK,修改的pom.xml文件(没有之前的testhadoop的话就重新新建一个maven项目,参考java操作HDFS)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

        <artifactId>testZK</artifactId>
        <groupId>com.qf</groupId>
        <version>1.0-SNAPSHOT</version>
    <modelVersion>4.0.0</modelVersion>

    <dependencies>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.10</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
</project>

二.在 src/main/java 下新建一个OperateZK.java

文件的内容为:

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;


import java.nio.charset.StandardCharsets;
import java.util.List;

public class OperateZK {

    @Test
    /*
     列出根节点下的子节点
     */

    public void testList() throws Exception {
        String conn = "192.168.154.10:2181,192.168.154.11:2181,192.168.154.12:2181";
        ZooKeeper zk = new ZooKeeper(conn, 5000, null);
        //列出根目录下的子节点
        List<String> children = zk.getChildren("/", false);
        for (String child : children) {
            System.out.println(child);
        }
    }


    @Test
    /*
    创建节点/a,并写入数据Tom
     */


    public void testCreate() throws Exception {
        String conn = "192.168.154.10:2181,192.168.154.11:2181,192.168.154.12:2181";
        ZooKeeper zk = new ZooKeeper(conn, 5000, null);
        String s = zk.create("/a", "Tom".getBytes(StandardCharsets.UTF_8), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println(s);
        zk.close();
    }

    @Test
    /*
    获取指定节点/a的数据
     */

    public void testGet() throws Exception {
        String conn = "192.168.154.10:2181,192.168.154.11:2181,192.168.154.12:2181";
        ZooKeeper zk = new ZooKeeper(conn, 5000, null);
        Stat stat = new Stat();
        System.out.println(stat.toString());
        //获取接根节点的数据
        byte[] a = zk.getData("/a", false, stat);
        System.out.println(stat.toString());
        System.out.println(new String(a));
        zk.close();


    }

    public static void main(String[] args) throws Exception {

        new OperateZK().testList();
        //new OperateZK().testCreate();不要重复执行此命令,要重复执行此程序这一行记得注释掉。
        new OperateZK().testCreate();
        new OperateZK().testGet();
    }
}

三.启动zookeeper

1.进入zookeeper安装目录

cd /usr/local/src/zookeeper/bin

2.启动服务(三台都要执行)

zkServer.sh start

启动zookeeper后就可以运行OperateZK.java了

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐