java读取zookeeper_java连接zookeeper实现zookeeper的基本操作

1 packagecom;2

3 importjava.util.List;4 importjava.util.concurrent.CountDownLatch;5 importorg.apache.zookeeper.CreateMode;6 importorg.apache.zookeeper.KeeperException;7 importorg.apache.zookeeper.WatchedEvent;8 importorg.apache.zookeeper.Watcher;9 importorg.apache.zookeeper.Watcher.Event.KeeperState;10 importorg.apache.zookeeper.ZooDefs.Ids;11 importorg.apache.zookeeper.ZooKeeper;12 importorg.apache.zookeeper.data.Stat;13

14

15

16 public class BaseZookeeper implementsWatcher{17 18    privateZooKeeper zookeeper;19     /**

20 * 超时时间21 */

22    private static final int SESSION_TIME_OUT = 2000;23    private CountDownLatch countDownLatch = new CountDownLatch(1);24 @Override25    public voidprocess(WatchedEvent event) {26       if (event.getState() ==KeeperState.SyncConnected) {27          System.out.println("Watch received event");28 countDownLatch.countDown();29 }30 }31

32

33

34 35    /**连接zookeeper36 *@paramhost37 *@throwsException38 */

39    public void connectZookeeper(String host) throwsException{40       zookeeper = new ZooKeeper(host, SESSION_TIME_OUT, this);41 countDownLatch.await();42       System.out.println("zookeeper connection success");43 }44 45    /**

46 * 创建节点47 *@parampath48 *@paramdata49 *@throwsException50 */

51    public String createNode(String path,String data) throwsException{52       return this.zookeeper.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);53 }54 55    /**

56 * 获取路径下所有子节点57 *@parampath58 *@return

59 *@throwsKeeperException60 *@throwsInterruptedException61 */

62    public List getChildren(String path) throwsKeeperException, InterruptedException{63       List children = zookeeper.getChildren(path, false);64       returnchildren;65 }66 67    /**

68 * 获取节点上面的数据69 *@parampath  路径70 *@return

71 *@throwsKeeperException72 *@throwsInterruptedException73 */

74    public String getData(String path) throwsKeeperException, InterruptedException{75       byte[] data = zookeeper.getData(path, false, null);76       if (data == null) {77          return "";78 }79       return newString(data);80 }81 82    /**

83 * 设置节点信息84 *@parampath  路径85 *@paramdata  数据86 *@return

87 *@throwsKeeperException88 *@throwsInterruptedException89 */

90    public Stat setData(String path,String data) throwsKeeperException, InterruptedException{91       Stat stat = zookeeper.setData(path, data.getBytes(), -1);92       returnstat;93 }94 95    /**

96 * 删除节点97 *@parampath98 *@throwsInterruptedException99 *@throwsKeeperException100 */

101    public void deleteNode(String path) throwsInterruptedException, KeeperException{102       zookeeper.delete(path, -1);103 }104 105    /**

106 * 获取创建时间107 *@parampath108 *@return

109 *@throwsKeeperException110 *@throwsInterruptedException111 */

112    public String getCTime(String path) throwsKeeperException, InterruptedException{113       Stat stat = zookeeper.exists(path, false);114       returnString.valueOf(stat.getCtime());115 }116 117    /**

118 * 获取某个路径下孩子的数量119 *@parampath120 *@return

121 *@throwsKeeperException122 *@throwsInterruptedException123 */

124    public Integer getChildrenNum(String path) throwsKeeperException, InterruptedException{125       int childenNum = zookeeper.getChildren(path, false).size();126       returnchildenNum;127 }128    /**

129 * 关闭连接130 *@throwsInterruptedException131 */

132    public void closeConnection() throwsInterruptedException{133       if (zookeeper != null) {134 zookeeper.close();135 }136 }137 138 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值