Zookeeper学习笔记2-------group创建、查询、删除
来源:互联网 发布:网络层的4个功能 编辑:程序博客网 时间:2024/05/16 00:28
下面一段代码涉及到了 ZooKeeper 创建group、为group添加member、列出group下面的 member、递归删除group。
这里我引用的是 ZooKeeper 3.4.10 提供的jar包。
GroupHelper.java
import java.io.IOException;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;public class GroupHelper implements Watcher {private static final int SESSION_TIMEOUT = 5000;private ZooKeeper zk;private CountDownLatch connectSignal = new CountDownLatch(1);private String groupName=null;/** * connect to zookeeper * @param host * @throws IOException * @throws InterruptedException */public void connect(String host) throws IOException, InterruptedException {this.zk = new ZooKeeper(host, SESSION_TIMEOUT, this);connectSignal.await();}@Overridepublic void process(WatchedEvent event) {// TODO Auto-generated method stubif (event.getState() == KeeperState.SyncConnected) {connectSignal.countDown();}}/** * * @param groupName * @throws KeeperException * @throws InterruptedException */public void create(String groupName) throws KeeperException, InterruptedException {this.groupName=groupName;String path="/".concat(groupName) ;String createPath=zk.create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);System.out.println("Create Path:"+createPath);}/** * @param memberName * @throws KeeperException * @throws InterruptedException */public void joinMember(String memberName) throws KeeperException, InterruptedException { String path=String.format("/%s/%s", this.groupName,memberName); path=zk.create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); System.out.println("Path:"+path);} /** * * @param groupName * @throws InterruptedException * @throws KeeperException */public void listMember(String groupName) throws InterruptedException { List<String> children;try {children = zk.getChildren(groupName, false);if(children.isEmpty()) {System.out.println("No Member in List......");System.exit(-1);}for(String child :children) { System.out.println(child);}} catch (KeeperException e) {// TODO Auto-generated catch blocke.printStackTrace();System.exit(-1);}}/** * * @param path * @throws KeeperException * @throws InterruptedException */public void deleteMember(String path) throws KeeperException, InterruptedException {List<String> children = zk.getChildren(path, false);if(children.isEmpty()) {return ;}for(String child:children) {String tmpPath=path+"/"+child;List<String> tmpList=zk.getChildren(tmpPath, false);if(tmpList.isEmpty()) {zk.delete(tmpPath, -1);}else {this.deleteMember(tmpPath);}}zk.delete(path, -1);}}
GroupTest.java
我们创建一个组a ,然后又在组a下面创建c、ds、b成员,然后又在/a/ds下创建ss成员 ,最后递归删除他。
import java.io.IOException;import org.apache.log4j.BasicConfigurator;import org.apache.zookeeper.KeeperException;public class ZookeeperTest { static { BasicConfigurator.configure(); } public static void main(String []argv) throws IOException, InterruptedException, KeeperException { GroupHelper cg=new GroupHelper(); cg.connect("127.0.0.1:2181"); cg.create("a"); cg.joinMember("b"); cg.joinMember("c"); cg.joinMember("ds"); cg.joinMember("ds/ss"); cg.listMember("/a"); //递归删除 cg.deleteMember("/a"); }}
运行结果:
对 Zookeeper的组操作是非常简单的。
阅读全文
1 0
- Zookeeper学习笔记2-------group创建、查询、删除
- Elasticsearch 学习笔记 索引创建、插入、查询、修改、删除
- ZooKeeper学习笔记-5---ZooKeeper编程-2
- Zookeeper学习笔记 --- Zookeeper调研报告2
- android 学习笔记 数据库的创建, 查询 和删除表内的信息
- ZooKeeper学习笔记:初识zookeeper
- MySQL学习笔记----创建修改删除表
- ZooKeeper学习笔记-2---ZooKeeper部署与运行
- ZooKeeper源码学习笔记(2)--Standalone模式下的ZooKeeper
- zookeeper学习笔记[2] - 应用及特性
- 【学习笔记】--zookeeper
- zookeeper学习笔记一
- python zookeeper学习笔记
- zookeeper学习笔记
- Zookeeper学习笔记
- zookeeper学习笔记
- Zookeeper 学习笔记
- Zookeeper学习笔记
- 多个条件的模糊汇总
- java 将含有unicod的字符串 转成中文
- Quartz学习5:Spring整合Quartz在集群中的应用
- 自制动漫游戏AMV
- PowerDesigner中SQL文件、数据库表反向生成PDM
- Zookeeper学习笔记2-------group创建、查询、删除
- 基于服务器端缓存redis的简单实现
- HTML+CSS制作鼠标悬浮上去可以放大的照片墙?
- 阿里巴巴校招内推简历筛选方案(总结篇)
- Java知识(集合框架和List)
- 匈牙利算法(转)
- Ubuntu下Sublime Text 3无法输入中文解决方案
- Android 进阶1:Activity 的生命周期
- MTK平台使用刷机软件刷机错误代码