Java服务端连接Zookeeper,进行节点信息的获取,管理…,整理成一个基本工具,

添加依赖:

 <dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.6</version>
</dependency>
 package com;

 import java.util.List;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat; public class BaseZookeeper implements Watcher{
 
   private ZooKeeper zookeeper;
    /**
     * 超时时间
     */
   private static final int SESSION_TIME_OUT = 2000;
   private CountDownLatch countDownLatch = new CountDownLatch(1);
   @Override
   public void process(WatchedEvent event) {
      if (event.getState() == KeeperState.SyncConnected) {
         System.out.println("Watch received event");
         countDownLatch.countDown();
      }
   }   
   /**连接zookeeper
    * @param host
    * @throws Exception
    */
   public void connectZookeeper(String host) throws Exception{
      zookeeper = new ZooKeeper(host, SESSION_TIME_OUT, this);
      countDownLatch.await();
      System.out.println("zookeeper connection success");
   }
  
   /**
    * 创建节点
    * @param path
    * @param data
    * @throws Exception
    */
   public String createNode(String path,String data) throws Exception{
      return this.zookeeper.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
   }
  
   /**
    * 获取路径下所有子节点
    * @param path
    * @return
    * @throws KeeperException
    * @throws InterruptedException
    */
   public List<String> getChildren(String path) throws KeeperException, InterruptedException{
      List<String> children = zookeeper.getChildren(path, false);
      return children;
   }
  
   /**
    * 获取节点上面的数据
    * @param path  路径
    * @return
    * @throws KeeperException
    * @throws InterruptedException
    */
   public String getData(String path) throws KeeperException, InterruptedException{
      byte[] data = zookeeper.getData(path, false, null);
      if (data == null) {
         return "";
      }
      return new String(data);
   }
  
   /**
    * 设置节点信息
    * @param path  路径
    * @param data  数据
    * @return
    * @throws KeeperException
    * @throws InterruptedException
    */
   public Stat setData(String path,String data) throws KeeperException, InterruptedException{
      Stat stat = zookeeper.setData(path, data.getBytes(), -1);
      return stat;
   }
  
   /**
    * 删除节点
    * @param path
    * @throws InterruptedException
    * @throws KeeperException
    */
   public void deleteNode(String path) throws InterruptedException, KeeperException{
      zookeeper.delete(path, -1);
   }
  
   /**
    * 获取创建时间
    * @param path
    * @return
    * @throws KeeperException
    * @throws InterruptedException
    */
   public String getCTime(String path) throws KeeperException, InterruptedException{
      Stat stat = zookeeper.exists(path, false);
      return String.valueOf(stat.getCtime());
   }
  
   /**
    * 获取某个路径下孩子的数量
    * @param path
    * @return
    * @throws KeeperException
    * @throws InterruptedException
    */
   public Integer getChildrenNum(String path) throws KeeperException, InterruptedException{
      int childenNum = zookeeper.getChildren(path, false).size();
      return childenNum;
   }
   /**
    * 关闭连接
    * @throws InterruptedException
    */
   public void closeConnection() throws InterruptedException{
      if (zookeeper != null) {
         zookeeper.close();
      }
   }
  
}

测试:

 public class Demo {

     public static void main(String[] args) throws Exception {
        BaseZookeeper zookeeper = new BaseZookeeper();
        zookeeper.connectZookeeper("192.168.0.1:2181");         List<String> children = zookeeper.getChildren("/");
        System.out.println(children);
    } }

java连接zookeeper实现zookeeper的基本操作的更多相关文章

  1. Java连接Hive使用Zookeeper的方式

    Java连接Hive的方式就是通过JDBC的方式来连接,URL为jdbc:hive2://host:port/db;principal=X@BIGDATA.COM等,这种方式是直接连接HiveServ ...

  2. Zookeeper入门(七)之Java连接Zookeeper

    Java操作Zookeeper很简单,但是前提要把包导对. 关于Zookeeper的Linux环境搭建可以参考我的这篇博客:Linux环境下Zookeeper安装 下面进入正题: 一.导入依赖 < ...

  3. java连接zookeeper服务器出现“KeeperErrorCode = ConnectionLoss for ...”

    错误信息如下: Exception in thread "main" org.apache.zookeeper.KeeperException$ConnectionLossExce ...

  4. java连接zookeeper服务器出现“KeeperErrorCode = ConnectionLoss for /test”

    昨天调试java连接zookeeper服务器,zookeeper搭建过程在这里不做赘述,在创建连接后,然后操作节点一直报异常 错误信息如下: Exception in thread "mai ...

  5. JAVA 连接 ZooKeeper之初体验

    Java连接Zookeeper 一.配置zk环境 本人使用的是虚拟机,创建了两台linux服务器(安装过程百度上很多) 准备zk的安装包,zookeeper-3.4.10.tar.gz,可在Apach ...

  6. java 学习笔记(四) java连接ZooKeeper

    public class Demo2 { public static void main(String[] args) { String connectString = "192.168.1 ...

  7. java架构之路-(分布式zookeeper)zookeeper集群配置和选举机制详解

    上次博客我们说了一下zookeeper的配置文件,以及命令的使用https://www.cnblogs.com/cxiaocai/p/11597465.html.我们这次来说一下我们的zookeepe ...

  8. 死磕 java同步系列之zookeeper分布式锁

    问题 (1)zookeeper如何实现分布式锁? (2)zookeeper分布式锁有哪些优点? (3)zookeeper分布式锁有哪些缺点? 简介 zooKeeper是一个分布式的,开放源码的分布式应 ...

  9. Java 使用ZkClient操作Zookeeper

    目录 ZkClient介绍 导入jar包依赖 简单使用样例 ZkClient介绍 因为Zookeeper API比较复杂,使用并不方便,所以出现了ZkClient,ZkClient对Zookeeper ...

随机推荐

  1. 如何利用awk计算文件某一列的平均值?

    [root@master yjt]# cat yjt.sh #!/bin/bash awk -v field="$1" '{sum+=$field; n++;}END {if (n ...

  2. Js 之常见手势操作插件 Hammer.js

    一.下载 链接:https://pan.baidu.com/s/1UbEtSbT1xcmdzzTCaWmW1A提取码:ldqy 二.案例 三.代码 <!DOCTYPE html> < ...

  3. [Codeforces1137F]Matches Are Not a Child's Play——LCT+树状数组

    题目链接: [Codeforces1137F]Matches Are Not a Child's Play 题目大意: 我们定义一棵树的删除序列为:每一次将树中编号最小的叶子删掉,将该节点编号加入到当 ...

  4. js 返回两数(包含这两数)之间的随机数函数

    function selectFrom( lowerValue, upperValue ){ var choices = upperValue - lowerValue + 1; return Mat ...

  5. TCP/IP 这猝不及防的爱情

    前言 前几天看了老刘的一篇文章,TCP/IP 大明邮差.正好最近也在读<计算机自顶向下>一书 心血来潮,想写一个女版的TCP/IP 正文 一天,我正在百花会上赏花,赏着赏着,就出现了一个令 ...

  6. Python 中路径的有效使用

    import arcpy arcpy.GetCount_management("c:/temp/streams.shp") arcpy.GetCount_management(&q ...

  7. mac电脑如何快速显示桌面及切换应用

    使用mac电脑时,我们习惯打开很多应用,文档等等.如果打开应用非常多,需要操作桌面,却不知如何快速返回桌面和切换应用时,操作就非常不便了,下面简单介绍mac电脑系统如何快速显示桌面及切换应用? 工具/ ...

  8. HBase-集群安装

    需要先启动 HDFS 集群和 ZooKeeper 集群. Hadoop 集群安装:https://www.cnblogs.com/jhxxb/p/10629796.html ZooKeeper 集群安 ...

  9. LoadRunne遇到的一些问题FAQ(持续更新...)

    1.LR11破解完成,添加License失败,报错License security violation Loadrunner11破解成功后,用deletelicense.exe工具把License全删 ...

  10. CentOS7下搭建SVN服务器

    (1).安装SVN 1)安装SVN [root@youxi1 ~]# yum -y install subversion mod_dav_svn [root@youxi1 ~]# cat /etc/s ...