java 测试zookeeper_zookeeper服务器Java代码和测试命令

package zktest;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.CreateMode;

import org.apache.zookeeper.WatchedEvent;

import org.apache.zookeeper.Watcher;

import org.apache.zookeeper.Watcher.Event.KeeperState;

import org.apache.zookeeper.ZooDefs.Ids;

import org.apache.zookeeper.ZooKeeper;

import org.apache.zookeeper.ZooKeeper.States;

import org.apache.zookeeper.data.Stat;

public class Conf {

public static void waitUntilConnected(ZooKeeper zooKeeper,

CountDownLatch connectedLatch) {

if (States.CONNECTING == zooKeeper.getState()) {

try {

connectedLatch.await();

} catch (InterruptedException e) {

throw new IllegalStateException(e);

}

}

}

static class ConnectedWatcher implements Watcher {

private CountDownLatch connectedLatch;

ConnectedWatcher(CountDownLatch connectedLatch) {

this.connectedLatch = connectedLatch;

}

@Override

public void process(WatchedEvent event) {

if (event.getState() == KeeperState.SyncConnected) {

connectedLatch.countDown();

}

}

}

static public Conf Instance() {

if (static_ == null) {

static_ = new Conf();

}

return static_;

}

public boolean Init(String hostports, int times) {

try {

CountDownLatch connectedLatch = new CountDownLatch(1);

Watcher watcher = new ConnectedWatcher(connectedLatch);

zk = new ZooKeeper(hostports, times, watcher);

System.out.println("zk:" + zk.toString());

waitUntilConnected(zk, connectedLatch);

System.out.println("safsa");

System.out.println("safsa");

// zk.create("/home/hadoop/testRootPath", "testRootData".getBytes(),

// Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

// zk.create("/home/hadoop/testRootPath/testChildPathOne",

// "testChildDataOne".getBytes(), Ids.OPEN_ACL_UNSAFE,

// CreateMode.PERSISTENT);

// System.out.println(new

// String(zk.getData("/home/hadoop/testRootPath", false,

// null)));

// System.out.println(zk.getChildren("/home/hadoop/testRootPath",

// true));

// zk.setData("/home/hadoop/testRootPath/testChildPathOne",

// "modifyChildDataOne".getBytes(), -1);

// System.out.println("dir node state:[" +

// zk.exists("/home/hadoop/testRootPath", true)

// + "]");

// zk.create("/testRootPath/testChildPathTwo",

// "testChildDataTwo".getBytes(), Ids.OPEN_ACL_UNSAFE,

// CreateMode.PERSISTENT);

// System.out.println(new String(zk.getData(

// "/home/hadoop/testRootPath/testChildPathTwo", true, null)));

// // zk.delete("/home/hadoop/testRootPath/testChildPathTwo", -1);

// // zk.delete("/home/hadoop/testRootPath/testChildPathOne", -1);

// // zk.delete("/home/hadoop/testRootPath", -1);

// zk.close();

//

zk.create("/hu", ("hello1"+"\n"+"hello2").getBytes(),

Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

//zk.create("/testRootPath", "testRootData".getBytes(),

//Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

创建一个子目录节点

//zk.create("/testRootPath/testChildPathOne",

//"testChildDataOne".getBytes(), Ids.OPEN_ACL_UNSAFE,

//CreateMode.PERSISTENT);

System.out.println(new String(zk.getData("/testRootPath", false,

null)));

// 取出子目录节点列表

System.out.println(zk.getChildren("/testRootPath", true));

// 修改子目录节点数据

zk.setData("/testRootPath/testChildPathOne",

"modifyChildDataOne".getBytes(), -1);

System.out.println("目录节点状态:[" + zk.exists("/testRootPath", true)

+ "]");

// 创建另外一个子目录节点

//zk.create("/testRootPath/testChildPathTwo",

//"testChildDataTwo".getBytes(), Ids.OPEN_ACL_UNSAFE,

//CreateMode.PERSISTENT);

System.out.println(new String(zk.getData(

"/testRootPath/testChildPathTwo", true, null)));

// 删除子目录节点

//zk.delete("/testRootPath/testChildPathTwo", -1);

//zk.delete("/testRootPath/testChildPathOne", -1);

删除父目录节点

//zk.delete("/testRootPath", -1);

// 关闭连接

zk.close();

} catch (Exception e) {

System.out.println(e);

return false;

}

return true;

}

public String Get(String keys) {

String re = "";

String ppath = "/tmp/zookeeper";

int oldpos = -1;

int pos = 0;

while (true) {

pos = keys.indexOf(".", oldpos + 1);

if (pos < 0) {

ppath += "/";

String str = keys.substring(oldpos + 1);

ppath += str;

break;

}

ppath += "/";

String str = keys.substring(oldpos + 1, pos);

ppath += str;

oldpos = pos;

}

Stat stat = new Stat();

try {

System.out.println("asfsa:" + ppath);

byte[] b = zk.getData(ppath, false, stat); // 获取节点的信息及存储的数据

re = new String(b);

} catch (Exception e) {

System.out.println(e);

}

return re;

}

private Conf() {

}

private ZooKeeper zk;

static private Conf static_;

public static void main(String args[]) {

// String hostports =

// "192.168.1.88:2181,192.168.1.88:2182,192.168.1.88:2183";

String hostports = "127.0.0.1:2181";

Conf.Instance().Init(hostports, 1000);

// String str =

// Conf.Instance().Get("conf.logicpoint.subscriberserverip");

// str = Conf.Instance().Get("conf.logicpoint.subscriberserverport");

// System.out.println(str);

while (true) {

try {

Thread.sleep(100);

} catch (Exception e) {

}

}

}

}

命令:

[hadoop@hutx bin]$ ./zookeeper-shell.sh 127.0.0.1:2181

Connecting to 127.0.0.1:2181

Welcome to ZooKeeper!

JLine support is disabled

WATCHER::

WatchedEvent state:SyncConnected type:None path:null

ls /

[testRootPath, zookeeper]

create /zk "test"

ZooKeeper -server host:port cmd args

connect host:port

get path [watch]

ls path [watch]

set path data [version]

delquota [-n|-b] path

quit

printwatches on|off

create [-s] [-e] path data acl

stat path [watch]

close

ls2 path [watch]

history

listquota path

setAcl path acl

getAcl path

sync path

redo cmdno

addauth scheme auth

delete path [version]

setquota -n|-b val path

ls /

[testRootPath, zookeeper]

create /zk

ZooKeeper -server host:port cmd args

connect host:port

get path [watch]

ls path [watch]

set path data [version]

delquota [-n|-b] path

quit

printwatches on|off

create [-s] [-e] path data acl

stat path [watch]

close

ls2 path [watch]

history

listquota path

setAcl path acl

getAcl path

sync path

redo cmdno

addauth scheme auth

delete path [version]

setquota -n|-b val path

ls /zookeeper

[quota]

ls2 /

[testRootPath, zookeeper]

cZxid = 0x0

ctime = Wed Dec 31 16:00:00 PST 1969

mZxid = 0x0

mtime = Wed Dec 31 16:00:00 PST 1969

pZxid = 0x1a

cversion = 3

dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 0

numChildren = 2

create /zk myData

ZooKeeper -server host:port cmd args

connect host:port

get path [watch]

ls path [watch]

set path data [version]

delquota [-n|-b] path

quit

printwatches on|off

create [-s] [-e] path data acl

stat path [watch]

close

ls2 path [watch]

history

listquota path

setAcl path acl

getAcl path

sync path

redo cmdno

addauth scheme auth

delete path [version]

setquota -n|-b val path

create /zk my

Created /zk

ls /

[testRootPath, zk, zookeeper]

ls /zk

[]

ls /testRootPath

[testChildPathTwo, testChildPathOne]

create /zkk "abc"

Created /zkk

ls /zkk

[]

get /zkk

"abc"

cZxid = 0x27

ctime = Thu Jul 31 07:55:17 PDT 2014

mZxid = 0x27

mtime = Thu Jul 31 07:55:17 PDT 2014

pZxid = 0x27

cversion = 0

dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 5

numChildren = 0

get /zk

my

cZxid = 0x23

ctime = Thu Jul 31 07:53:15 PDT 2014

mZxid = 0x23

mtime = Thu Jul 31 07:53:15 PDT 2014

pZxid = 0x23

cversion = 0

dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 2

numChildren = 0

set /zk ccc

cZxid = 0x23

ctime = Thu Jul 31 07:53:15 PDT 2014

mZxid = 0x28

mtime = Thu Jul 31 08:01:00 PDT 2014

pZxid = 0x23

cversion = 0

dataVersion = 1

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 3

numChildren = 0

get /zk

ccc

cZxid = 0x23

ctime = Thu Jul 31 07:53:15 PDT 2014

mZxid = 0x28

mtime = Thu Jul 31 08:01:00 PDT 2014

pZxid = 0x23

cversion = 0

dataVersion = 1

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 3

numChildren = 0

ls

ZooKeeper -server host:port cmd args

connect host:port

get path [watch]

ls path [watch]

set path data [version]

delquota [-n|-b] path

quit

printwatches on|off

create [-s] [-e] path data acl

stat path [watch]

close

ls2 path [watch]

history

listquota path

setAcl path acl

getAcl path

sync path

redo cmdno

addauth scheme auth

delete path [version]

setquota -n|-b val path

ls /

[testRootPath, zk, zkk, zookeeper]

get /testRootPath

testRootData

cZxid = 0x1a

ctime = Thu Jul 31 07:20:57 PDT 2014

mZxid = 0x1a

mtime = Thu Jul 31 07:20:57 PDT 2014

pZxid = 0x1d

cversion = 2

dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 12

numChildren = 2

get /hu

hello1

hello2

cZxid = 0x2a

ctime = Thu Jul 31 08:07:35 PDT 2014

mZxid = 0x2a

mtime = Thu Jul 31 08:07:35 PDT 2014

pZxid = 0x2a

cversion = 0

dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 13

numChildren = 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值