---
title: "Zookeeper Java API"
author: "okwangxing"
date: "2010-02-18T22:00:24Z"
tags: ["Apache", "Java", "Linux"]
categories: ["企业架构"]
slug: "zookeeper-java-api"
draft: false
lastmod: "2010-02-18T22:00:24Z"
id: 598548
url: "https://okwangxing.iteye.com/blog/598548"
---

续前话,Zookeeper的环境已经搭建好了.接下来就是利用Java实现与zookeeper的连接,达到和ZOOKEEPER\_HOME\\bin\\zkCli.sh中的部分基础功能. 具体的zookeeper的安装见\[url=http://okwangxing.iteye.com/admin/blogs/594023]ZooKeeper起步配置\[/url]. \[code="java"] 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; /\** * @author ruodao * @since 1.0 * 2010-2-18 下午08:57:36 \*/ 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(); } } \[/code] 下面一段代码实现了简单的操作,添加,获取数据,获取子节点. \[code="java"] 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 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); } } \[/code] main函数 \[code="java"] 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(); } } \[/code] 到这里,一个简单的程序已经可以运行了.以下是linux上的截图. \[img]http://dl.iteye.com/upload/attachment/206539/2390253d-b758-34a2-8869-1308355490a4.jpg\[/img]