zookeeper 安装 和 常用命令 及 (IDEA)通过java实现其 增删改查

zookeeper安装

(5条消息) zookeeper是什么? 和 centos8 安装zookeeper_阿A轲的博客-CSDN博客

---------------------------------------------------------------------------------------------------------------------------------

zookeeper的常用命令

1. 显示根目录下、文件: ls / 使用 ls 命令来查看当前 ZooKeeper 中所包含的内容

2. 显示根目录下、文件: ls2 / 查看当前节点数据并能看到更新次数等数据

3. 创建文件,并设置初始内容: create /zk "test" 创建一个新的 znode节点“ zk ”以及与它关联的字符串

4. 获取文件内容: get /zk 确认 znode 是否包含我们所创建的字符串

5. 修改文件内容: set /zk "zkbak" 对 zk 所关联的字符串进行设置

6. 删除文件: delete /zk 将刚才创建的 znode 删除

7. 退出客户端: quit

8. 帮助命令: help

---------------------------------------------------------------------------------------------------------------------------------

通过java实现zookeeper中的增删改查 

1.新建一个maven项目

 

        (maven配置可以用自己的,也可以用idea自带的)

2. pom.xml 文件中 引入zookeeper jar包

<dependencies>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.12</version>
        </dependency>
 </dependencies>

引入成功后就是这样(如果引入失败,不能解析依赖,就更换maven配置)

 java文件夹创建一个类

 开始写代码(总代码在最后):

根据zookeeper源码写

首先写:

  //zk的链接地址
    public static final String zkconnect="你的服务器IP:2181";
    //超时时间
    public static final int timeout=15000;

    //zk链接方法
    public static ZooKeeper connect() throws IOException {
        ZooKeeper zk=new ZooKeeper(zkconnect,timeout,null);
        System.out.println("zk链接成功");
        return zk;
    }

  zk的连接方法这里有抛出异常是因为 调用的zookeeper的源码中的方法 有抛出异常。

 再写上main方法  (调用connect方法 所以main方法也要抛出异常)

 public static void main(String[] s) throws IOException{
        ZooKeeper zooKeeper=connect();
      
    }

运行测试

        如果有问题报错:

        排查问题的步骤:

                第一步:先看一下程序有没有正常启动,lsof -i:2181;

        如果说有输出,说明程序正常启动。

        如果没有输出,说明程序没有正常启动,然后重新启动程序。

                第二步:如果程序正常启动,本地还是访问不到。

        先检查防火墙,再检查安全组。只有这两个都开放端口了,才说明这个端口开放了。

 增加

 //增加
    public static void create(ZooKeeper zooKeeper,String node,String data) throws KeeperException,InterruptedException {
        System.out.println("开始创建节点:"+node+"节点数据:"+data);

        List<ACL> acl= ZooDefs.Ids.OPEN_ACL_UNSAFE;
        CreateMode createMode= CreateMode.PERSISTENT;

        zooKeeper.create(node,data.getBytes(),acl,createMode);
        //这个creat并不是,我们写的这个creat,是zookeeper类中的creat方法

        System.out.println("zk节点创建成功");
    }

(根据源码中

 要告诉创建节点是什么权限,以及节点数据有哪些格式,并且抛出异常也是因为源码中creat抛出异常)

main方法中再写入

create(zooKeeper,"/zktest","hello");

运行

再去zookeeper中看一下

ok

删除

都是根据源码写

   //删除
    public static void delete(ZooKeeper zooKeeper,String node) throws InterruptedException,KeeperException{
        //查节点是否存在
        Stat stat=zooKeeper.exists(node,false);
        System.out.println("开始删除节点:"+node+"原来版本号:"+stat.getVersion());

        zooKeeper.delete(node,stat.getVersion());
            //此 delete 是zookeeper类中的
        System.out.println("删除节点成功");
    }

 main方法中写入

delete(zooKeeper,"/zktest");

运行

zookeeper中,节点被删掉啦 

 修改

 //修改
    public static void setdata(ZooKeeper zooKeeper,String node,String data)throws InterruptedException,KeeperException{
    // 查看节点是否存在:
       Stat stat=zooKeeper.exists(node,false);

        System.out.println("开始修改节点"+node+"原来版本号:"+stat.getVersion());

        zooKeeper.setData(node,data.getBytes(),stat.getVersion());

        System.out.println("修改成功");
    }

main中写入

setdata(zooKeeper,"/zktest","hi");

运行

 zookeeper中 hello 修改为了 hi 

查询

    //查找
    public static void query(ZooKeeper zooKeeper,String node)throws InterruptedException,KeeperException{
       String result= new String(zooKeeper.getData(node,false,null));
       System.out.println("查询的数据的节点:"+node+"---数据为:"+result);

    }

main 方法中写入

query(zooKeeper,"/zktest");

运行

全部代码:

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;

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

public class MyzkTest {
    //zk的链接地址
    public static final String zkconnect="101.43.158.237:2181";
    //超时时间
    public static final int timeout=15000;

    public static void main(String[] s) throws IOException,KeeperException,InterruptedException{
        ZooKeeper zooKeeper=connect();
        // create(zooKeeper,"/zktest","hello");
        //delete(zooKeeper,"/zktest");
         //setdata(zooKeeper,"/zktest","hi");
        query(zooKeeper,"/zktest");
    }

    //zk链接方法
    public static ZooKeeper connect() throws IOException {
        ZooKeeper zk=new ZooKeeper(zkconnect,timeout,null);
        System.out.println("zk链接成功");
        return zk;
    }
    //增加
    public static void create(ZooKeeper zooKeeper,String node,String data) throws KeeperException,InterruptedException {
        System.out.println("开始创建节点:"+node+"节点数据:"+data);

        List<ACL> acl= ZooDefs.Ids.OPEN_ACL_UNSAFE;
        CreateMode createMode= CreateMode.PERSISTENT;
        zooKeeper.create(node,data.getBytes(),acl,createMode);
        System.out.println("zk节点创建成功");
    }
    //删除
    public static void delete(ZooKeeper zooKeeper,String node) throws InterruptedException,KeeperException{
        //查节点是否存在
        Stat stat=zooKeeper.exists(node,false);
        System.out.println("开始删除节点:"+node+"原来版本号:"+stat.getVersion());
        zooKeeper.delete(node,stat.getVersion());
        System.out.println("删除节点成功");
    }
    //修改
    public static void setdata(ZooKeeper zooKeeper,String node,String data)throws InterruptedException,KeeperException{
        Stat stat=zooKeeper.exists(node,false);
        System.out.println("开始修改节点"+node+"原来版本号:"+stat.getVersion());
        zooKeeper.setData(node,data.getBytes(),stat.getVersion());
        System.out.println("修改成功");
    }
    //查找
    public static void query(ZooKeeper zooKeeper,String node)throws InterruptedException,KeeperException{
       String result= new String(zooKeeper.getData(node,false,null));
       System.out.println("查询的数据的节点:"+node+"---数据为:"+result);

    }

}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值