zooKeeper API 简单应用

zooKeeper API 简单应用


zooKeeperTest.java
package com;
import java.util.Arrays;
public class zooKeeperTest{

public static void main(String[] args) {
try {
ZooKeeperOperator zkoperator = new ZooKeeperOperator();
zkoperator.connect("114.55.72.173:2181");
byte[] data = new byte[]{'d','a','t','a'};

zkoperator.create("/root",null);
System.out.println(Arrays.toString(zkoperator.getData("/root")));

zkoperator.create("/root/child4",data);
System.out.println(Arrays.toString(zkoperator.getData("/root/child1")));

zkoperator.create("/root/child3",data);
System.out.println(Arrays.toString(zkoperator.getData("/root/child2")));

System.out.println("节点孩子信息:");
zkoperator.getChild("/root");

zkoperator.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}



AbstractZooKeeper.java
package com;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.KeeperState;

public class AbstractZooKeeper implements Watcher {
private static final int SESSION_TIME = 2000;
protected ZooKeeper zooKeeper;
protected CountDownLatch countDownLatch = new CountDownLatch(1);

public void connect(String hosts) throws IOException, InterruptedException{
zooKeeper = new ZooKeeper(hosts,SESSION_TIME,this);
countDownLatch.await();
}
public void process(WatchedEvent event) {
if(event.getState() == KeeperState.SyncConnected){
countDownLatch.countDown();
}
}
public void close() throws InterruptedException{
zooKeeper.close();
}
}



ZooKeeperOperator.java
package com;
import java.util.Arrays;
import java.util.List;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs.Ids;

public class ZooKeeperOperator extends AbstractZooKeeper {
/**
* 创建持久态的znode,比支持多层创建.比如在创建/parent/child的情况下,无/parent.无法通过.
* @param path eg: /parent/child1
* @param data
* @throws InterruptedException
* @throws KeeperException
*/
public void create(String path,byte[] data) throws KeeperException, InterruptedException{
this.zooKeeper.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT/*此处创建的为持久态的节点,可为瞬态*/);
}

/**
* 获取节点的孩子信息
* @param path
* @throws KeeperException
* @throws InterruptedException
*/
public void getChild(String path) throws KeeperException, InterruptedException{
try {
List<String> children = this.zooKeeper.getChildren(path, false);
if (children.isEmpty()) {
System.out.printf("没有节点在%s中.", path);
return;
}else{
System.out.printf("节点%s中存在的节点:\n", path);
for(String child: children){
System.out.println(child);
}
}
} catch (KeeperException.NoNodeException e) {
System.out.printf("%s节点不存在.", path);
throw e;
}
}

public byte[] getData(String path) throws KeeperException, InterruptedException {
return this.zooKeeper.getData(path, false,null);
}
}



[url]https://blog.csdn.net/heyutao007/article/details/38741207[/url](ZooKeeper 节点类型)
参考原文:[url]http://okwangxing.iteye.com/blog/598548[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值