java连接zookeeper进行简单操作

pfjia 2014-05-13

下面是简单的java对zookeeper的操作

package com.yydone.zookeeper;

import java.io.IOException;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Test {

	private static final int SESSION_TIMEOUT = 30000;
	public static final Logger logger = LoggerFactory.getLogger(Test.class);
	
	private Watcher watcher = new Watcher() {
		@Override
		public void process(WatchedEvent event) {
			logger.info("process:"+event.getType());
		}
	};
	
	private ZooKeeper zooKeeper;
	
	public ZooKeeper getZooKeeper() {
		return zooKeeper;
	}

	public void setZooKeeper(ZooKeeper zooKeeper) {
		this.zooKeeper = zooKeeper;
	}

	public void connect() throws IOException {
		zooKeeper = new ZooKeeper("192.168.1.102:2181",SESSION_TIMEOUT, watcher);
	}
	
	public void close() {
		try {
			zooKeeper.close();
		} catch (InterruptedException e) {
			logger.error("关闭发生异常",e);
		}
	}
	
	public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
		
		Test test = new Test();
		//连接
		test.connect();
		logger.info("连接成功");
		//创建
		String result = test.getZooKeeper().create("/test001", "test001data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		logger.info("创建成功:"+result);
		//判断是否存在
		Stat stat = test.getZooKeeper().exists("/test001", false);
		logger.info("exists:",stat.getCzxid());
		
		if(stat!=null) {
			result = new String(test.getZooKeeper().getData("/test001", null, null));
			logger.info("获取:"+result);
		}
		
		stat = test.getZooKeeper().setData("/test001", "test1001,ceshi".getBytes(), -1);
		result = new String(test.getZooKeeper().getData("/test001", null, null));
		logger.info("获取:"+result);
		
		test.getZooKeeper().delete("/test001", -1);
	}

}

 运行结果如下:

2014-05-13 23:54:45,673 0 [main]  INFO [Environment.java:100] - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
 2014-05-13 23:54:45,676 3 [main]  INFO [Environment.java:100] - Client environment:host.name=yunix-PC
 2014-05-13 23:54:45,676 3 [main]  INFO [Environment.java:100] - Client environment:java.version=1.8.0_05
 2014-05-13 23:54:45,676 3 [main]  INFO [Environment.java:100] - Client environment:java.vendor=Oracle Corporation
 2014-05-13 23:54:45,679 6 [main]  INFO [Environment.java:100] - Client environment:java.home=D:\Java\jre8
 2014-05-13 23:54:45,679 6 [main]  INFO [Environment.java:100] - Client environment:java.class.path=F:\workspace\yunix-zookeeper\bin;C:\Users\yunix\Desktop\zookeeper-3.4.6\zookeeper-3.4.6.jar;C:\Users\yunix\Desktop\zookeeper-3.4.6\lib\jline-0.9.94.jar;C:\Users\yunix\Desktop\zookeeper-3.4.6\lib\log4j-1.2.16.jar;C:\Users\yunix\Desktop\zookeeper-3.4.6\lib\netty-3.7.0.Final.jar;C:\Users\yunix\Desktop\zookeeper-3.4.6\lib\slf4j-api-1.6.1.jar;C:\Users\yunix\Desktop\zookeeper-3.4.6\lib\slf4j-log4j12-1.6.1.jar
 2014-05-13 23:54:45,679 6 [main]  INFO [Environment.java:100] - Client environment:java.library.path=D:\Java\jre8\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;.
 2014-05-13 23:54:45,680 7 [main]  INFO [Environment.java:100] - Client environment:java.io.tmpdir=C:\Users\yunix\AppData\Local\Temp\
 2014-05-13 23:54:45,680 7 [main]  INFO [Environment.java:100] - Client environment:java.compiler=<NA>
 2014-05-13 23:54:45,680 7 [main]  INFO [Environment.java:100] - Client environment:os.name=Windows 7
 2014-05-13 23:54:45,680 7 [main]  INFO [Environment.java:100] - Client environment:os.arch=amd64
 2014-05-13 23:54:45,680 7 [main]  INFO [Environment.java:100] - Client environment:os.version=6.1
 2014-05-13 23:54:45,680 7 [main]  INFO [Environment.java:100] - Client environment:user.name=yunix
 2014-05-13 23:54:45,680 7 [main]  INFO [Environment.java:100] - Client environment:user.home=C:\Users\yunix
 2014-05-13 23:54:45,680 7 [main]  INFO [Environment.java:100] - Client environment:user.dir=F:\workspace\yunix-zookeeper
 2014-05-13 23:54:45,681 8 [main]  INFO [ZooKeeper.java:438] - Initiating client connection, connectString=192.168.1.102:2181 sessionTimeout=30000 watcher=com.yydone.zookeeper.Test$1@5910e440
 2014-05-13 23:54:45,734 61 [main]  INFO [Test.java:54] - 连接成功
 2014-05-13 23:54:45,746 73 [main-SendThread(192.168.1.102:2181)]  INFO [ClientCnxn.java:975] - Opening socket connection to server 192.168.1.102/192.168.1.102:2181. Will not attempt to authenticate using SASL (unknown error)
 2014-05-13 23:54:45,750 77 [main-SendThread(192.168.1.102:2181)]  INFO [ClientCnxn.java:852] - Socket connection established to 192.168.1.102/192.168.1.102:2181, initiating session
 2014-05-13 23:54:45,765 92 [main-SendThread(192.168.1.102:2181)]  INFO [ClientCnxn.java:1235] - Session establishment complete on server 192.168.1.102/192.168.1.102:2181, sessionid = 0x145f6068b8c0006, negotiated timeout = 30000
 2014-05-13 23:54:45,768 95 [main-EventThread]  INFO [Test.java:23] - process:None
 2014-05-13 23:54:45,788 115 [main]  INFO [Test.java:57] - 创建成功:/test001
 2014-05-13 23:54:45,797 124 [main]  INFO [Test.java:60] - exists:
 2014-05-13 23:54:45,801 128 [main]  INFO [Test.java:64] - 获取:test001data
 2014-05-13 23:54:45,806 133 [main]  INFO [Test.java:69] - 获取:test1001,ceshi

相关推荐

一个人的世界 / 0评论 2018-08-25