package com.weihao.www.utils;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.List;
/**
* Auth:wei.hao
* Date:2020-04-07
*/
public class ZookeeperUtils {
/**
* The logger.
*/
private static final Logger log = LoggerFactory.getLogger(ZookeeperUtils.class);
private ZooKeeper zooKeeper;
private ZookeeperUtils() {
}
/**
* get Zookeeper connect
*
* @param path znode节点
* @param sessionTimeOut 超时时间
*/
public ZookeeperUtils(String path, int sessionTimeOut) {
try {
zooKeeper = new ZooKeeper(path, sessionTimeOut, watchedEvent -> {
if (watchedEvent.getState().equals(Watcher.Event.KeeperState.SyncConnected)) {
log.info("create zookeeper connect success");
}
});
} catch (IOException e) {
log.error("create zookeeper connect failed", e);
e.printStackTrace();
}
}
/**
* 创建znode节点
*
* @param path 节点路径
* @param data 值
* @param mode 节点类型(持久、持久顺序、临时、临时顺序)
* @return
*/
public boolean createZnode(String path, String data, CreateMode mode) {
try {
if (zooKeeper.exists(path, true) == null) {
zooKeeper.addAuthInfo("digest", "weihao:Aa111111".getBytes());
zooKeeper.create(path, data.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, mode);
return true;
}
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
log.error("create znode failed,", e);
throw new RuntimeException("create znode failed", e);
}
return false;
}
/**
* set znode节点
*
* @param path
* @param data
* @return
*/
public boolean updateZnode(String path, String data) {
try {
Stat stat = zooKeeper.exists(path, true);
if (null != stat) {
zooKeeper.setData(path, data.getBytes(), stat.getVersion());
return true;
}
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
log.error("set Znode failed", e);
throw new RuntimeException("set Znode failed", e);
}
return false;
}
/**
* 删除节点以及子节点
*
* @param path
* @return
*/
public boolean deleteZnode(String path) {
try {
Stat stat = zooKeeper.exists(path, true);
if (stat != null) {
List<String> children = zooKeeper.getChildren(path, true);
for (String child : children) {
deleteZnode(path + "/" + child);
}
zooKeeper.delete(path, stat.getVersion());
}
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
log.error("delete Znode failed", e);
throw new RuntimeException("delete Znode failed", e);
}
return true;
}
/**
* 获取节点数据
*
* @param path
* @return
*/
public String getZnodeData(String path) {
String result = null;
try {
Stat exists = zooKeeper.exists(path, true);
if (null != exists) {
byte[] data = zooKeeper.getData(path, true, exists);
result = new String(data);
}
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
log.error("get Znode data failed", e);
throw new RuntimeException("get Znode data failed", e);
}
return result;
}
}
代码使用的pom.xml依赖
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.5</version>
</dependency>
【大数据入门系列1.2】zookeeper入门API
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 官网地址:https://zookeeper.apache.org/ Zookeeper简介: ZooKeeper...
- 1、解压 2、配置环境变量 3、修改conf/zoo_sample.cfg文件为zoo.cfg server为固定...
- 常用命令 启动Zookeeper 可选参数: 启动ZooInspector,可以查看注册到Zookeeper的Ka...
- 本文接“上文”继续讲解zookeeper相关的技术点,所以序号接上文。 想了解大数据其他知识点可以点击文章末尾“了...