java 连接zookeeper_Zookeeper入门(七)之Java连接Zookeeper

packagezookeeper_demo;importjava.util.List;importjava.util.concurrent.CountDownLatch;importorg.apache.zookeeper.CreateMode;importorg.apache.zookeeper.KeeperException;importorg.apache.zookeeper.WatchedEvent;importorg.apache.zookeeper.Watcher;importorg.apache.zookeeper.Watcher.Event.KeeperState;importorg.apache.zookeeper.ZooDefs.Ids;importorg.apache.zookeeper.ZooKeeper;importorg.apache.zookeeper.data.Stat;public class BaseZookeeper implementsWatcher{privateZooKeeper zookeeper;/*** 超时时间*/

private static final int SESSION_TIME_OUT = 2000;private CountDownLatch countDownLatch = new CountDownLatch(1);public voidprocess(WatchedEvent event) {if (event.getState() ==KeeperState.SyncConnected) {

System.out.println("Watch received event");

countDownLatch.countDown();

}

}/**连接zookeeper

*@paramhost

*@throwsException*/

public void connectZookeeper(String host) throwsException{

zookeeper= new ZooKeeper(host, SESSION_TIME_OUT, this);

countDownLatch.await();

System.out.println("zookeeper connection success");

}/*** 创建节点

*@parampath

*@paramdata

*@throwsException*/

public String createNode(String path,String data) throwsException{return this.zookeeper.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

}/*** 获取路径下所有子节点

*@parampath

*@return*@throwsKeeperException

*@throwsInterruptedException*/

public List getChildren(String path) throwsKeeperException, InterruptedException{

List children = zookeeper.getChildren(path, false);returnchildren;

}/*** 获取节点上面的数据

*@parampath 路径

*@return*@throwsKeeperException

*@throwsInterruptedException*/

public String getData(String path) throwsKeeperException, InterruptedException{byte[] data = zookeeper.getData(path, false, null);if (data == null) {return "";

}return newString(data);

}/*** 设置节点信息

*@parampath 路径

*@paramdata 数据

*@return*@throwsKeeperException

*@throwsInterruptedException*/

public Stat setData(String path,String data) throwsKeeperException, InterruptedException{

Stat stat= zookeeper.setData(path, data.getBytes(), -1);returnstat;

}/*** 删除节点

*@parampath

*@throwsInterruptedException

*@throwsKeeperException*/

public void deleteNode(String path) throwsInterruptedException, KeeperException{

zookeeper.delete(path,-1);

}/*** 获取创建时间

*@parampath

*@return*@throwsKeeperException

*@throwsInterruptedException*/

public String getCTime(String path) throwsKeeperException, InterruptedException{

Stat stat= zookeeper.exists(path, false);returnString.valueOf(stat.getCtime());

}/*** 获取某个路径下孩子的数量

*@parampath

*@return*@throwsKeeperException

*@throwsInterruptedException*/

public Integer getChildrenNum(String path) throwsKeeperException, InterruptedException{int childenNum = zookeeper.getChildren(path, false).size();returnchildenNum;

}/*** 关闭连接

*@throwsInterruptedException*/

public void closeConnection() throwsInterruptedException{if (zookeeper != null) {

zookeeper.close();

}

}public static void main(String[] args) throwsException {

BaseZookeeper zookeeper= newBaseZookeeper();

zookeeper.connectZookeeper("192.168.126.128:2181");

List children = zookeeper.getChildren("/");

System.out.println(children);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值