java-zookeeper ZooKeeper常用命令以及一些JAVA API操作示例 - break

📅 2026-11-30 👤 拼多多助力营销技巧 📂 拼多多助力网站 👁️ 187 次阅读

ZooKeeper客户端shell命令

1.进入客户端

zkCli.sh

2.create创建节点

创建普通节点

create /test "123"'

’

创建临时节点,重新进入客户端节点会消失 create -e /test/test1 “456” 创建顺序节点 create -s /test/test1 'a'

create -s /test/test2 'b'

create -s /test/test3 'c'

3.ls查看某路径的节点

ls /

4.get查看节点详细信息

get /test

czxid-创建节点的事务zxid每次修改ZooKeeper状态都会收到⼀个zxid形式的时间戳,也就是ZooKeeper事务ID。事务ID是ZooKeeper中所有修改总的次序。每个修改都有唯⼀的zxid,如果zxid1⼩于zxid2,那么zxid1在zxid2之前发⽣ctime - znode被创建的毫秒数mzxid - znode最后更新的事务zxidmtime - znode最后修改的毫秒数pZxid-znode最后更新的⼦节点zxidcversion - znode⼦节点变化号,znode⼦节点修改次数dataversion - znode数据变化号aclVersion - znode访问控制列表的变化号ephemeralOwner- 如果是临时节点,这个是znode拥有者的session id。如果不是临时节点则是0

5.ls2 查看某一路径下的所有节点和信息

ls2 /

6.set 设置节点的值

set /test "01"

7.delete删除节点(没有字节的情况下)

delete /test/test10000000001

8.rmr 递归删除节点

rmr /test

9.history查看会话历史命令

history

10.help查看命令帮助

help

11.监听器的使用

11.1监听节点数据变化

在hadoop001主机上注册监听/test节点数据变化

get /test watch

在hadoop002主机上修改/test节点的数据

set /test "2"

观察hadoop001主机收到数据变化的监听

11.2 监听节点的⼦节点变化

在hadoop001主机上注册监听/test节点的⼦节点变化

ls /test watch

在hadoop002主机/test节点上 创建⼦节点

create /test/test01 "2"

观察hadoop001主机收到⼦节点变化的监听

Zookeeper的API使⽤

1.引入jar包

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">

big_datacode

org.example

1.0-SNAPSHOT

4.0.0

zk_opt

8

8

org.projectlombok

lombok

1.18.10

org.slf4j

slf4j-log4j12

1.7.25

org.apache.curator

curator-framework

2.13.0

org.apache.curator

curator-recipes

2.13.0

com.google.collections

google-collections

1.0

org.apache.zookeeper

zookeeper

3.4.9

junit

junit

RELEASE

org.slf4j

slf4j-simple

1.7.25

org.apache.maven.plugins

maven-compiler-plugin

3.1

1.8

1.8

UTF-8

2.节点操作

2.1创建节点

2.1.1创建持久节点、临时节点

public class zkOpt {

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

//定义重试策略

ExponentialBackoffRetry exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3, Integer.MAX_VALUE);

//构建zk,连接客户端

CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("hadoop001:2181,hadoop002:2181,hadoop003:2181", exponentialBackoffRetry);

//启动客户端

curatorFramework.start();

//创建节点

curatorFramework.create()

.creatingParentContainersIfNeeded()

.withMode(CreateMode.PERSISTENT) //PERSISTENT表示持久节点,EPHEMERAL表示临时节点

.forPath("/testJAVA","helloWorld".getBytes());

2.1.2 修改节点数据、删除节点、获取节点数据

//更改节点值

curatorFramework.setData().forPath("/testJAVA","haha".getBytes());

//删除节点

curatorFramework.delete().forPath("/test/test01");

//递归删除节点

curatorFramework.delete()

.deletingChildrenIfNeeded()

.forPath("/test");

//获取节点值

byte[] bytes = curatorFramework.getData().forPath("/testJAVA");

System.out.println(new String(bytes));

//获取节点数据和stat信息 拼多多帮助力入口

Stat statinfo = new Stat();

byte[] node10 = curatorFramework.getData()

.storingStatIn(statinfo)//获取stat信息存储到stat对象

.forPath("/testJAVA");

System.out.println("=====>该节点信息为:" + new String(node10));

System.out.println("=====>该节点的数据版本号为:" +

statinfo.getVersion());

2.1.3 监听机制

try {

String path = "treeCacheTest";

//定义重试策略

ExponentialBackoffRetry exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3, Integer.MAX_VALUE);

//构建zk,连接客户端

CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("hadoop001:2181,hadoop002:2181,hadoop003:2181", exponentialBackoffRetry);

//启动客户端

curatorFramework.start();

//如果存在path,删除路径

Stat stat = curatorFramework.checkExists().forPath("/testJAVA" + path);

if (stat != null) {

curatorFramework.delete().guaranteed().deletingChildrenIfNeeded().forPath("/testJAVA" + path);

}

//创建节点

curatorFramework.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/testJAVA"+path,path.getBytes());

final TreeCache cache = new TreeCache(curatorFramework, "/testJAVA"+path);

cache.getListenable().addListener((cli,event) ->{

TreeCacheEvent.Type type = event.getType();

switch (type){

case CONNECTION_RECONNECTED:

break;

case CONNECTION_SUSPENDED:

break;

case CONNECTION_LOST:

System.out.println("连接丢失");

break;

case NODE_ADDED:

System.out.println("添加节点:" + event.getData().getPath());

break;

case NODE_UPDATED:

System.out.println("更新节点");

break;

case NODE_REMOVED:

System.out.println("删除节点:"+ event.getData().getPath());

break;

default:

}

});

cache.start();

//创建子节点/testJAVA/1

curatorFramework.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/testJAVA"+path+"/testJAVA/1",path.getBytes());

Thread.sleep(1000);

//创建子节点/testJAVA/1/2

curatorFramework.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/testJAVA"+path+"/testJAVA/1/2",path.getBytes());

Thread.sleep(1000);

//删除子节点/testJAVA/1/2

curatorFramework.delete().guaranteed().deletingChildrenIfNeeded().forPath("/testJAVA"+path+ "/testJAVA/1/2");

Thread.sleep(1000);

//删除子节点/testJAVA/1

curatorFramework.delete().guaranteed().deletingChildrenIfNeeded().forPath("/testJAVA"+path+"/testJAVA/1");

//Thread.sleep(1000);

Thread.sleep(Integer.MAX_VALUE);

cache.close();

//关闭客户端

curatorFramework.close();

}catch (Exception e) {

//TODO: handle exception

}

2.1.4 判断节点是否存在

//判断接待你是否存在

//连接字符串

String connectString = "hadoop001:2181,hadoop002:2181,hadoop003:2181";

//指定重试策略

ExponentialBackoffRetry exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3, Integer.MAX_VALUE);

//创建客户端

CuratorFramework client = CuratorFrameworkFactory.newClient(connectString,exponentialBackoffRetry);

//启动客户端

client.start();

Stat exisNodeStat =

client.checkExists().forPath("/testJAVA");

if (exisNodeStat == null){

System.out.println("===>节点不存在");

}

if (exisNodeStat.getEphemeralOwner()>0){

System.out.println("===>临时节点");

}

else{

System.out.println("===>永久节点");

}

推荐链接,java

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧