fenl 发表于 2015-11-21 13:19:05

Zookeeper(五)JAVA API

  org.apache.zookeeper.ZooKeeper类 主要方法列表


方法名称
描述

String create(final String path, byte data[], List acl, CreateMode createMode)  创建一个znode节点,
  参数: 路径、 znode内容,ACL(访问控制列表)、 znode创建类型


void delete(final String path, int version)  删除一个znode节点,
  参数: 路径、版本号;如果版本号与znode的版本号不一致,将无法删除,是一种乐观加锁机制;如果将版本号设置为-1,不会去检测版本,直接删除;


Stat exists(final String path, Watcher watcher)  判断某个znode节点是否存在
  参数: 路径、Watcher(监视器);当这个znode节点被改变时,将会触发当前Watcher


Stat exists(String path, boolean watch)  判断某个znode节点是否存在
  参数: 路径、并设置是否监控这个目录节点,这里的 watcher 是在创建 ZooKeeper 实例时指定的 watcher


Stat setData(final String path, byte data[], int version)  设置某个znode上的数据
  参数: 路径、数据、版本号;如果为-1,跳过版本检查


byte[] getData(final String path, Watcher watcher, Stat stat)  获取某个znode上的数据
  参数: 路径、监视器、数据版本等信息


List getChildren(final String path, Watcher watcher)  获取某个节点下的所有子节点
  参数: 路径、监视器;该方法有多个重载
  


  znode创建类型(CreateMode):



PERSISTENT               持久化节点

   

PERSISTENT_SEQUENTIAL   顺序自动编号持久化节点,这种节点会根据当前已存在的节点数自动加 1

   

EPHEMERAL    临时节点, 客户端session超时这类节点就会被自动删除

  EPHEMERAL_SEQUENTIAL   临时自动编号节点

  



/*
* ZookeeperTest.java
*/
package com.x.zookeeper;
import java.io.IOException;
import java.util.List;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author http://blog.iyunv.com/java2000_wl
* @version <b>1.0</b>
*/
public class ZookeeperTest {
private static final int SESSION_TIMEOUT = 30000;
public static final Logger LOGGER = LoggerFactory.getLogger(ZookeeperTest.class);
private Watcher watcher =new Watcher() {
public void process(WatchedEvent event) {
LOGGER.info(&quot;process : &quot; + event.getType());
}
};
private ZooKeeper zooKeeper;
/**
*连接zookeeper
* <br>------------------------------<br>
* @throws IOException
*/
@Before
public void connect() throws IOException {
zooKeeper= new ZooKeeper(&quot;localhost:2181,localhost:2182,localhost:2183&quot;, SESSION_TIMEOUT, watcher);
}
/**
*关闭连接
* <br>------------------------------<br>
*/
@After
public void close() {
try {
zooKeeper.close();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 创建一个znode
*1.CreateMode 取值
*PERSISTENT:持久化,这个目录节点存储的数据不会丢失
*PERSISTENT_SEQUENTIAL:顺序自动编号的目录节点,这种目录节点会根据当前已近存在的节点数自动加 1,然后返回给客户端已经成功创建的目录节点名;
*EPHEMERAL:临时目录节点,一旦创建这个节点的客户端与服务器端口也就是 session过期超时,这种节点会被自动删除
*EPHEMERAL_SEQUENTIAL:临时自动编号节点
* <br>------------------------------<br>
*/
@Test
public void testCreate() {
String result = null;
try {
result = zooKeeper.create(&quot;/zk001&quot;, &quot;zk001data&quot;.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} catch (Exception e) {
LOGGER.error(e.getMessage());
Assert.fail();
}
LOGGER.info(&quot;create result : {}&quot;, result);
}
/**
* 删除节点忽略版本
* <br>------------------------------<br>
*/
@Test
public void testDelete() {
try {
zooKeeper.delete(&quot;/zk001&quot;, -1);
} catch (Exception e) {
LOGGER.error(e.getMessage());
Assert.fail();
}
}
/**
*   获取数据
* <br>------------------------------<br>
*/
@Test
public void testGetData() {
String result = null;
try {
byte[] bytes = zooKeeper.getData(&quot;/zk001&quot;, null, null);
result = new String(bytes);
} catch (Exception e) {
LOGGER.error(e.getMessage());
Assert.fail();
}
LOGGER.info(&quot;getdata result : {}&quot;, result);
}
/**
*   获取数据设置watch
* <br>------------------------------<br>
*/
@Test
public void testGetDataWatch() {
String result = null;
try {
byte[] bytes = zooKeeper.getData(&quot;/zk001&quot;, new Watcher() {
public void process(WatchedEvent event) {
LOGGER.info(&quot;testGetDataWatchwatch : {}&quot;, event.getType());
}
}, null);
result = new String(bytes);
} catch (Exception e) {
LOGGER.error(e.getMessage());
Assert.fail();
}
LOGGER.info(&quot;getdata result : {}&quot;, result);
// 触发wacthNodeDataChanged
try {
zooKeeper.setData(&quot;/zk001&quot;, &quot;testSetData&quot;.getBytes(), -1);
} catch (Exception e) {
LOGGER.error(e.getMessage());
Assert.fail();
}
}
/**
*    判断节点是否存在
*    设置是否监控这个目录节点,这里的 watcher 是在创建 ZooKeeper实例时指定的 watcher
* <br>------------------------------<br>
*/
@Test
public void testExists() {
Stat stat = null;
try {
stat = zooKeeper.exists(&quot;/zk001&quot;, false);
} catch (Exception e) {
LOGGER.error(e.getMessage());
Assert.fail();
}
Assert.assertNotNull(stat);
LOGGER.info(&quot;exists result : {}&quot;, stat.getCzxid());
}
/**
*   设置对应znode下的数据,-1表示匹配所有版本
* <br>------------------------------<br>
*/
@Test
public void testSetData() {
Stat stat = null;
try {
stat = zooKeeper.setData(&quot;/zk001&quot;, &quot;testSetData&quot;.getBytes(), -1);
} catch (Exception e) {
LOGGER.error(e.getMessage());
Assert.fail();
}
Assert.assertNotNull(stat);
LOGGER.info(&quot;exists result : {}&quot;, stat.getVersion());
}
/**
*    判断节点是否存在,
*    设置是否监控这个目录节点,这里的 watcher 是在创建 ZooKeeper实例时指定的 watcher
* <br>------------------------------<br>
*/
@Test
public void testExistsWatch1() {
Stat stat = null;
try {
stat = zooKeeper.exists(&quot;/zk001&quot;, true);
} catch (Exception e) {
LOGGER.error(e.getMessage());
Assert.fail();
}
Assert.assertNotNull(stat);
try {
zooKeeper.delete(&quot;/zk001&quot;, -1);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*    判断节点是否存在,
*    设置监控这个目录节点的 Watcher
* <br>------------------------------<br>
*/
@Test
public void testExistsWatch2() {
Stat stat = null;
try {
stat = zooKeeper.exists(&quot;/zk002&quot;, new Watcher() {
@Override
public void process(WatchedEvent event) {
LOGGER.info(&quot;testExistsWatch2watch : {}&quot;, event.getType());
}
});
} catch (Exception e) {
LOGGER.error(e.getMessage());
Assert.fail();
}
Assert.assertNotNull(stat);
// 触发watch 中的process方法   NodeDataChanged
try {
zooKeeper.setData(&quot;/zk002&quot;, &quot;testExistsWatch2&quot;.getBytes(), -1);
} catch (Exception e) {
e.printStackTrace();
}
// 不会触发watch 只会触发一次
try {
zooKeeper.delete(&quot;/zk002&quot;, -1);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*获取指定节点下的子节点
* <br>------------------------------<br>
*/
@Test
public void testGetChild() {
try {
zooKeeper.create(&quot;/zk/001&quot;, &quot;001&quot;.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zooKeeper.create(&quot;/zk/002&quot;, &quot;002&quot;.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
List<String> list = zooKeeper.getChildren(&quot;/zk&quot;, true);
for (String node : list) {
LOGGER.info(&quot;node {}&quot;, node);
}
} catch (Exception e) {
LOGGER.error(e.getMessage());
Assert.fail();
}
}
}



  
页: [1]
查看完整版本: Zookeeper(五)JAVA API