Zookeeper入门(七)之Java连接Zookeeper
Java操作Zookeeper很简单,但是前提要把包导对。
关于Zookeeper的Linux环境搭建可以参考我的这篇博客:Linux环境下Zookeeper安装
下面进入正题:
一、导入依赖
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.zookeeper</groupId>
<artifactId>zookeeper_demo</artifactId>
<version>0.0.1-SNAPSHOT</version> <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
</dependencies>
</project>
二、编写工具类代码和测试代码
package zookeeper_demo; 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); 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 static void main(String[] args) throws Exception {
BaseZookeeper zookeeper = new BaseZookeeper();
zookeeper.connectZookeeper("192.168.126.128:2181");
List<String> children = zookeeper.getChildren("/");
System.out.println(children);
}
}
完成以上两步,即可完成Java连接并对Zookeeper的简单操作。
Zookeeper入门(七)之Java连接Zookeeper的更多相关文章
- java连接zookeeper服务器出现“KeeperErrorCode = ConnectionLoss for /test”
昨天调试java连接zookeeper服务器,zookeeper搭建过程在这里不做赘述,在创建连接后,然后操作节点一直报异常 错误信息如下: Exception in thread "mai ...
- JAVA 连接 ZooKeeper之初体验
Java连接Zookeeper 一.配置zk环境 本人使用的是虚拟机,创建了两台linux服务器(安装过程百度上很多) 准备zk的安装包,zookeeper-3.4.10.tar.gz,可在Apach ...
- java连接zookeeper服务器出现“KeeperErrorCode = ConnectionLoss for ...”
错误信息如下: Exception in thread "main" org.apache.zookeeper.KeeperException$ConnectionLossExce ...
- java连接zookeeper实现zookeeper的基本操作
Java服务端连接Zookeeper,进行节点信息的获取,管理…,整理成一个基本工具, 添加依赖: <dependency> <groupId>org.apache.zooke ...
- java 学习笔记(四) java连接ZooKeeper
public class Demo2 { public static void main(String[] args) { String connectString = "192.168.1 ...
- java springboot整合zookeeper入门教程(增删改查)
java springboot整合zookeeper增删改查入门教程 zookeeper的安装与集群搭建参考:https://www.cnblogs.com/zwcry/p/10272506.html ...
- zookeeper - 通过java代码连接zookeeper(2)
首先创建一个Maven项目 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...
- Java连接Hive使用Zookeeper的方式
Java连接Hive的方式就是通过JDBC的方式来连接,URL为jdbc:hive2://host:port/db;principal=X@BIGDATA.COM等,这种方式是直接连接HiveServ ...
- Zookeeper入门-Java版本HelloWorld例子
上一篇介绍了,Zookeeper的基本概念,怎么启动,怎么解决可能遇到的几个问题.本篇,根据网上代码,整理了一个例子,Zookeeper的HelloWorld. 下面这个代码,还是比较简单的,核心类就 ...
随机推荐
- 你写的 Java 代码是如何一步步输出结果的? (转)
出处: 一步步解析java执行内幕 对于任何一门语言,要想达到精通的水平,研究它的执行原理(或者叫底层机制)不失为一种良好的方式.在本篇文章中,将重点研究java源代码的执行原理,即从程 序员编写J ...
- 关于Faster-RCNN训练细节
Faster RCNN训练: 四部训练法: Faster R-CNN,可以大致分为两个部分,一个是RPN网络,另一个是Fast R-CNN网络,前者是一种候选框(proposal)的推荐算法,而后者则 ...
- Springboot实现上传文件接口,使用python的requests进行组装报文上传文件的方法
记录瞬间 近段时间使用Springboot实现了文件的上传服务,但是在使用python的requests进行post上传时,总是报错. 比如: 1.Current request is not a m ...
- Asp.net4.5未在web服务器上注册
在使用vs2012打开项目时,显示Asp.net4.5未在web服务器上注册?是由于没有下载一个补丁的原因,只需下载安装补丁 VS11-KB3002339.exe 下载地址:https://downl ...
- Tomcat 设置80端口
1:修改tomcat配置 vi /usr/local/tomcat/conf/server.xml 找到 Connector port="8080" protocol=" ...
- 为什么单线程的Redis却能支撑高并发
Redis的高并发和快速原因 redis是基于内存的,内存的读写速度非常快: 核心是基于非阻塞的IO多路复用机制: redis是单线程的,反而省去了很多上下文切换线程的时间: 为什么Redis是单线程 ...
- python之开发规范
命名规范 Python之父推荐的规范 Type Public Internal Modules lower_with_under _lower_with_under Packages lower_wi ...
- PHP高级进阶之路
一:常见模式与框架 学习PHP技术体系,设计模式,流行的框架 常见的设计模式,编码必备 Laravel.ThinkPHP开发必不可少的最新框架 YII.Symfony4.1核心源码剖析 二:微服务架构 ...
- 第五篇 CSS入门 明白 三种嵌套形式,三种常用控制器
CSS入门 css是 层叠式样式表 css的作用是什么呢?举个抽象的例子啊,HTML是人,CSS则是衣服... css给html穿上衣服,有三种形式: 内嵌.内联.外联. 这三种形式,优先级为 ...
- shell 删除文本中的重复行
三种常见方法:第一,用sort+uniq,注意,单纯uniq是不行的. shell> sort -k2n file | uniq > a.out 这里我做了个简单的测试,当file中的重复 ...