ienki 发表于 2015-11-22 07:49:55

Zookeeper Java API 实例

Zookeeper Java API 实例
Api介绍
  
  
  表 1 org.apache.zookeeper. ZooKeeper 方法列表



  除了以上这些上表中列出的方法之外还有一些重载方法,如都提供了一个回调类的重载方法以及可以设置特定 Watcher 的重载方法,具体的方法可以参考 org.apache.zookeeper. ZooKeeper 类的 API 说明。
  英文版: https://zookeeper.apache.org/doc/r3.4.2/api/index.html
实例
  public class MyZk implements Watcher {
  
  public void process(WatchedEvent watchedEvent) {
  System.out.println("触发了" + watchedEvent.getType() + "事件!");
  }
  }
  
  package com.mylearn.zookeeper.test;
  
  import IceUtilInternal.StringUtil;
  import com.jd.common.util.ArrayUtils;
  import org.apache.cxf.common.util.StringUtils;
  import org.apache.zookeeper.*;
  import org.apache.zookeeper.data.ACL;
  import org.apache.zookeeper.data.Stat;
  
  import java.io.IOException;
  import java.util.List;
  
  /**
  * Created by IntelliJ IDEA.
  * User: yingkuohao
  * Date: 13-11-26
  * Time: 上午8:57
  * CopyRight:360buy
  * Descrption:
  * zk java api测试
  * To change this template use File | Settings | File Templates.
  */
  public class ZkApi {
  
  private static String server = "192.168.229.79:";
  private static String port = "2181";
  private static final int SESSION_TIMEOUT = 10000;
  private static ZooKeeper zk = null;
  
  public static void main(String args[]) {
  ZkApi testzkApi = new ZkApi();
  Watcher watcher = new MyZk();
  //      ZooKeeper zk =testzkApi.init(server + port, ClientBase.CONNECTION_TIMEOUT); //创建zk对象
  zk = testzkApi.init(server + port, SESSION_TIMEOUT, watcher); //创建zk对象
  
  String parentPath = "/testParentPath";      //节点路径 ,the path for the node
  String testParentData = "testParentData";       //节点初始化数据,the initial data for the node
  List<ACL> aclList = ZooDefs.Ids.OPEN_ACL_UNSAFE; //节点的权限 the acl for the node
  //      CreateMode createMode = CreateMode.EPHEMERAL;//临时节点,一旦创建这个节点的客户端与服务端端口session超时,这种节点会被自动删除。
  CreateMode createMode = CreateMode.PERSISTENT;//持久节点,这个目录节点存储的数据不会丢失。
  try {
  createNod(parentPath, testParentData, aclList, createMode);
  String parentData = new String(zk.getData(parentPath, false, null));//读取付父节点内容,getData方法
  System.out.println(&quot;parentData=&quot; &#43; parentData);
  
  String childPath1 = parentPath &#43; &quot;/testChildPath1&quot;;
  String childData1 = &quot;testChildData1&quot;;
  
  createNod(childPath1, childData1, aclList, createMode);//创建子目录 1
  String childDataPop = new String(zk.getData(childPath1, false, null));//读取子节点内容
  System.out.println(&quot;childData1=&quot; &#43; childDataPop);
  
  
  String childPath2 = parentPath &#43; &quot;/testChildPath2&quot;;
  String childData2 = &quot;testChildData2&quot;;
  
  createNod(childPath2, childData2, aclList, createMode);//创建子目录2
  //创建另一个父节点
  String parentPath2 = &quot;/testParentPath2&quot;;
  String testParentData2 = &quot;testParentData2&quot;;
  createNod(parentPath2, testParentData2, aclList, createMode);
  
  listNode(&quot;/&quot;);
  listNode(parentPath);
  
  Stat stat = zk.exists(parentPath, false);
  List<ACL> rootAcl = zk.getACL(parentPath, stat);   //获取某个目录节点的访问权限列表。
  System.out.println(&quot;节点权限:&quot; &#43; ArrayUtils.join(rootAcl.toArray(), &quot;,&quot;)&#43;&quot;节点版本:&quot;&#43;stat.getVersion());
  zk.setACL(parentPath,ZooDefs.Ids.READ_ACL_UNSAFE,stat.getVersion());
  System.out.println(&quot;节点权限:&quot; &#43; ArrayUtils.join( zk.getACL(parentPath, stat).toArray(), &quot;,&quot;));
  
  deleteNode(&quot;/testChildPath&quot;);
  deleteNode(parentPath); //父目录下有子节点,不能直接删除,会报错: KeeperErrorCode = Directory not empty for /testParentPath
  listNode(&quot;/&quot;);
  } catch (KeeperException e) {
  e.printStackTrace();//To change body of catch statement use File | Settings | File Templates.
  } catch (InterruptedException e) {
  e.printStackTrace();//To change body of catch statement use File | Settings | File Templates.
  }
  
  }
  
  
  /**
  * 删除节点
  * @param path
  * @throws KeeperException
  * @throws InterruptedException
  */
  private static void deleteNode(String path) throws KeeperException, InterruptedException {
  Stat stat = zk.exists(path, true);
  if (stat != null) {
  zk.delete(path, -1);//-1可以匹配任何版本,也就删除了这个目录节点所有数据,
  System.out.println(&quot;删除&quot; &#43; path &#43; &quot;节点成功&quot;);
  listNode(path);
  } else {
  System.out.println(path &#43; &quot;节点不存在&quot;);
  }
  }
  
  /**
  * 打印节点信息
  * @param path
  * @throws KeeperException
  * @throws InterruptedException
  */
  private static void listNode(String path) throws KeeperException, InterruptedException {
  List<String> nodeList = zk.getChildren(path, false); //获取孩子节点
  System.out.println(path &#43; &quot;路径下的节点&quot; &#43; ArrayUtils.join(nodeList.toArray(), &quot;,&quot;));
  }
  
  /**
  * 创建节点
  *
  * @param nodePath   节点路径
  * @param nodeData   节点初始化数据
  * @param aclList    权限列表
  * @param createMode 节点类型
  * @throws KeeperException
  * @throws InterruptedException
  */
  private static void createNod(String nodePath, String nodeData, List<ACL> aclList, CreateMode createMode) {
  try {
  Stat stat = zk.exists(nodePath, true);
  if (stat != null) {
  //如两次执行 EPHEMERAL节点,就不会执行这段代码,因为每次都会清除;而 PERSISTENT节点,创建一次后就会一直存在,除非手动删除
  System.out.println(&quot;节点&quot; &#43; nodePath &#43; &quot;已存在!&quot;);
  } else {
  zk.create(nodePath, nodeData.getBytes(), aclList, createMode);//创建节点 ,create方法
  }
  } catch (KeeperException e) {
  e.printStackTrace();
  } catch (InterruptedException e) {
  e.printStackTrace();
  }
  }
  
  
  /**
  * 初始化zk
  * @param path
  * @param timeOut
  * @param watcher
  * @return
  */
  public ZooKeeper init(String path, int timeOut, Watcher watcher) {
  if (zk == null) {
  try {
  zk = new ZooKeeper(path, timeOut, watcher);
  } catch (IOException e) {
  e.printStackTrace();//To change body of catch statement use File | Settings | File Templates.
  }
  }
  return zk;
  }
  
  
  }
  
  
  参考:
  https://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/
页: [1]
查看完整版本: Zookeeper Java API 实例