1. import java.io.IOException;  
  2. import java.util.concurrent.CountDownLatch;  
  3. import org.apache.zookeeper.WatchedEvent;  
  4. import org.apache.zookeeper.Watcher;  
  5. import org.apache.zookeeper.ZooKeeper;  
  6. import org.apache.zookeeper.Watcher.Event.KeeperState;  
  7.   
  8. /** 
  9.  * @author ruodao 
  10.  * @since 1.0 
  11.  * 2010-2-18 下午08:57:36 
  12.  */  
  13. public class AbstractZooKeeper implements Watcher {  
  14.     private static final int SESSION_TIME   = 2000;  
  15.     protected ZooKeeper  zooKeeper;  
  16.     protected CountDownLatch countDownLatch = new CountDownLatch(1);  
  17.   
  18.     public void connect(String hosts) throws IOException, InterruptedException{  
  19.         zooKeeper = new ZooKeeper(hosts,SESSION_TIME,this);  
  20.         countDownLatch.await();  
  21.     }  
  22.     public void process(WatchedEvent event) {  
  23.         if(event.getState() == KeeperState.SyncConnected){  
  24.             countDownLatch.countDown();  
  25.         }  
  26.     }  
  27.     public void close() throws InterruptedException{  
  28.         zooKeeper.close();  
  29.     }  
  30. }  

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

/**
 * @author ruodao
 * @since 1.0
 * 2010-2-18 下午09:03:21
 */
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);
 }
}
public static void main(String[] args) {
  try {
   ZooKeeperOperator zkoperator    = new ZooKeeperOperator();
   zkoperator.connect("192.168.0.115");
   byte[] data = new byte[]{'d','a','t','a'};
   
   zkoperator.create("/root",null);
   System.out.println(Arrays.toString(zkoperator.getData("/root")));
   
   zkoperator.create("/root/child1",data);
   System.out.println(Arrays.toString(zkoperator.getData("/root/child1")));
   
   zkoperator.create("/root/child2",data);
   System.out.println(Arrays.toString(zkoperator.getData("/root/child2")));
   
   System.out.println("节点孩子信息:");
   zkoperator.getChild("/root");
   
   zkoperator.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
到这里,一个简单的程序已经可以运行了.以下是linux上的截图. 
  

  • 大小: 109 KB

删除组

Zookeeper提供了一个delete()方法,该方法有两个参数:路径和版本号。如果所提供的版本号与znode的版本号一致,Zookeeper会删除这个znode,这是一种乐观的加锁机制,使客户端能够检测出对znode的修改冲突。通过将版本号设置为-1,可以绕过这个版本检测机制,不管znode的版本号是什么而直接将其删除。

 

public class DeleteGroup extends ConnectionWatcher {
     
     public void delete(String groupName) throws KeeperException, InterruptedException {
         String path = "/" + groupName;
 
         try {
             List<String> children = zk.getChildren(path, false );
             for (String child : children) {
                 zk.delete(path + "/" + child, - 1);
//先递归删除子节点
             }
             zk.delete(path, -1);//后删除父节点
         } catch (KeeperException.NoNodeException e) {
             System.out.println( "Group " + groupName + " does not exist" );
         }
     }
}

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐