Zookeeper Java API

利用Java实现与zookeeper的连接,达到和ZOOKEEPER_HOME\bin\zkCli.sh中的部分基础功能.
Java代码 复制代码 收藏代码
  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.     } 


下面一段代码实现了简单的操作,添加,获取数据,获取子节点.
Java代码 复制代码 收藏代码
  1. import java.util.Arrays; 
  2. import java.util.List; 
  3. import org.apache.zookeeper.CreateMode; 
  4. import org.apache.zookeeper.KeeperException; 
  5. import org.apache.zookeeper.ZooDefs.Ids; 
  6.  
  7. /**
  8. * @author ruodao
  9. * @since 1.0
  10. * 2010-2-18 下午09:03:21
  11. */ 
  12. public class ZooKeeperOperator extends AbstractZooKeeper { 
  13.     /**
  14.      * 创建持久态的znode,比支持多层创建.比如在创建/parent/child的情况下,无/parent.无法通过.
  15.      * @param path eg:  /parent/child1
  16.      * @param data
  17.      * @throws InterruptedException
  18.      * @throws KeeperException
  19.      */ 
  20.     public void create(String path,byte[] data) throws KeeperException, InterruptedException{ 
  21.         this.zooKeeper.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT/*此处创建的为持久态的节点,可为瞬态*/); 
  22.     } 
  23.      
  24.      
  25.     /**
  26.      * 获取节点的孩子信息
  27.      * @param path
  28.      * @throws KeeperException
  29.      * @throws InterruptedException
  30.      */ 
  31.     public void getChild(String path) throws KeeperException, InterruptedException{ 
  32.         try
  33.             List<String> children = this.zooKeeper.getChildren(path, false); 
  34.             if (children.isEmpty()) { 
  35.                 System.out.printf("没有节点在%s中.", path); 
  36.                 return
  37.             }else
  38.                 System.out.printf("节点%s中存在的节点:\n", path); 
  39.                 for(String child: children){ 
  40.                     System.out.println(child); 
  41.                 } 
  42.             } 
  43.         } catch (KeeperException.NoNodeException e) { 
  44.             System.out.printf("%s节点不存在.", path); 
  45.             throw e; 
  46.         } 
  47.     } 
  48.  
  49.     public byte[] getData(String path) throws KeeperException, InterruptedException { 
  50.         return  this.zooKeeper.getData(path, false,null); 
  51.     } 


main函数
Java代码 复制代码 收藏代码
  1. public static void main(String[] args) { 
  2.         try
  3.             ZooKeeperOperator zkoperator             = new ZooKeeperOperator(); 
  4.             zkoperator.connect("192.168.0.115"); 
  5.             byte[] data = new byte[]{'d','a','t','a'}; 
  6.              
  7.             zkoperator.create("/root",null); 
  8.             System.out.println(Arrays.toString(zkoperator.getData("/root"))); 
  9.              
  10.             zkoperator.create("/root/child1",data); 
  11.             System.out.println(Arrays.toString(zkoperator.getData("/root/child1"))); 
  12.              
  13.             zkoperator.create("/root/child2",data); 
  14.             System.out.println(Arrays.toString(zkoperator.getData("/root/child2"))); 
  15.              
  16.             System.out.println("节点孩子信息:"); 
  17.             zkoperator.getChild("/root"); 
  18.              
  19.             zkoperator.close(); 
  20.         } catch (Exception e) { 
  21.             e.printStackTrace(); 
  22.         } 
  23.     } 

到这里,一个简单的程序已经可以运行了.以下是linux上的截图.
Zookeeper Java API_第1张图片

你可能感兴趣的:(Zookeeper Java API)