ZooKeeper客户端 zkCli.sh 节点的增删改查
在 bin 目录下的 zkCli.sh 就是ZooKeeper客户端
./zkCli.sh -timeout 5000 -server 127.0.0.1:2181 客户端与ZooKeeper建立链接
timeout:超时时间,单位毫秒
r:只读模式,当节点坏掉的时候,还可以提供读服务
示例:
./zkCli.sh -timeout 5000 -server 127.0.0.1:2181
1 zhangliuningdeMacBook-Pro:bin sherry$ ./zkCli.sh -timeout 5000 -server 127.0.0.1:2181
2 Connecting to 127.0.0.1:2181
3 2016-08-27 15:07:04,036 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.8--1, built on 02/06/2016 03:18 GMT
4 2016-08-27 15:07:04,039 [myid:] - INFO [main:Environment@100] - Client environment:host.name=192.168.0.105
5 2016-08-27 15:07:04,039 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_71
6 2016-08-27 15:07:04,041 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
7 2016-08-27 15:07:04,041 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_71.jdk/Contents/Home/jre
8 2016-08-27 15:07:04,042 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/Users/sherry/Applications/zk1/bin/../build/classes:/Users/sherry/Applications/zk1/bin/../build/lib/*.jar:/Users/sherry/Applications/zk1/bin/../lib/slf4j-log4j12-1.6.1.jar:/Users/sherry/Applications/zk1/bin/../lib/slf4j-api-1.6.1.jar:/Users/sherry/Applications/zk1/bin/../lib/netty-3.7.0.Final.jar:/Users/sherry/Applications/zk1/bin/../lib/log4j-1.2.16.jar:/Users/sherry/Applications/zk1/bin/../lib/jline-0.9.94.jar:/Users/sherry/Applications/zk1/bin/../zookeeper-3.4.8.jar:/Users/sherry/Applications/zk1/bin/../src/java/lib/*.jar:/Users/sherry/Applications/zk1/bin/../conf:
9 2016-08-27 15:07:04,042 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/Users/sherry/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
10 2016-08-27 15:07:04,042 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/var/folders/_n/d05bph5x5bl6k5p0pkx2g66m0000gn/T/
11 2016-08-27 15:07:04,042 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
12 2016-08-27 15:07:04,042 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Mac OS X
13 2016-08-27 15:07:04,042 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=x86_64
14 2016-08-27 15:07:04,042 [myid:] - INFO [main:Environment@100] - Client environment:os.version=10.11.5
15 2016-08-27 15:07:04,043 [myid:] - INFO [main:Environment@100] - Client environment:user.name=sherry
16 2016-08-27 15:07:04,043 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/Users/sherry
17 2016-08-27 15:07:04,043 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/Users/sherry/Applications/zk1/bin
18 2016-08-27 15:07:04,044 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=5000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@531d72ca
19 Welcome to ZooKeeper!
20 2016-08-27 15:07:04,081 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
21 JLine support is enabled
22 2016-08-27 15:07:04,187 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@876] - Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
23 [zk: 127.0.0.1:2181(CONNECTING) 0] 2016-08-27 15:07:04,245 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x156cacb918b0000, negotiated timeout = 5000
24
25 WATCHER::
26
27 WatchedEvent state:SyncConnected type:None path:null
如上,表示我们和ZooKeeper建立了连接,可以通过命令与ZooKeeper进行交互了
h:帮助列表
h
ZooKeeper -server host:port cmd args
stat path [watch]
set path data [version]
ls path [watch]
delquota [-n|-b] path
ls2 path [watch]
setAcl path acl
setquota -n|-b val path
history
redo cmdno
printwatches on|off
delete path [version]
sync path
listquota path
rmr path
get path [watch]
create [-s] [-e] path data acl
addauth scheme auth
quit
getAcl path
close
connect host:port
显示能够执行的命令
命令分类和数据库相似,大概可以分为增删改查四类
ZooKeeper数据结构:
大多数时候,我们队ZooKeeper的操作都是在对节点进行增删改查
常用命令:
查询相关指令
ls path:列出path下的文件
[zk: 127.0.0.1:2181(CONNECTED) 4] ls /
[zookeeper]
如上:列出根目录节点下的所有文件,目前就只有一个系统自带的zookeeper节点
stat path:查看节点状态
[zk: 127.0.0.1:2181(CONNECTED) 6] stat /zookeeper
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1
cZxid:创建节点时的事务id
pZxid:子节点列表最后一次被修改的事务id
cversion:节点版本号
dataCersion:数据版本号
aclVerson:acl权限版本号 ...... get path:获取指定节点的内容 ls2 path:列出path节点的子节点及状态信息
创建指令
create [-s] [-e] path data acl
1 [zk: 127.0.0.1:2181(CONNECTED) 10] create /node_1 123
2 Created /node_1
如上:在根目录创建了node_1节点,携带数据 123
使用 get /node_1 验证是否添加节点及其数据成功
[zk: 127.0.0.1:2181(CONNECTED) 15] create -e /node_1/node_1_1 234
Created /node_1/node_1_1
创建了一个临时节点(-e),并且携带数据234,使用stat命令查看这个新建的临时节点
[zk: 127.0.0.1:2181(CONNECTED) 16] stat /node_1/node_1_1
cZxid = 0x200000003
ctime = Sat Aug 27 15:27:30 CST 2016
mZxid = 0x200000003
mtime = Sat Aug 27 15:27:30 CST 2016
pZxid = 0x200000003
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x156cacb918b0000
dataLength = 3
numChildren = 0
ephemeralOwner值不再是0,表示这个临时节点的版本号,如果是永久节点则其值为 0x0
1 [zk: 127.0.0.1:2181(CONNECTED) 1] create -s /node_1/node_1_1 234
2 Created /node_1/node_1_10000000001
通过使用-s参数,创建一个顺序节点,我们虽然指定的节点名是node_1_1,但是实际上,名称却是 node_1_10000000001,如果我们重复执行:
[zk: 127.0.0.1:2181(CONNECTED) 2] create -s /node_1/node_1_1 234
Created /node_1/node_1_10000000002
效果如上
这个特性,我们可以利用一下,生成在分布式环境下的主键生成器
-s 和 -e 可以同时使用
退出:
quit
[zk: 127.0.0.1:2181(CONNECTED) 19] quit
Quitting...
2016-08-27 15:29:44,760 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x156cacb918b0000 closed
2016-08-27 15:29:44,763 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x156cacb918b0000
这个时候如果重新登录,执行 ls /node_1,发现其子节点是空的,创建的node_1_1子节点不见了,那是因为之前创建的时候就已经指定了,我们创建的是一个临时节点。当客户端会话结束后,临时节点是会被删除了。
修改相关指定:
set path data [version]
[zk: 127.0.0.1:2181(CONNECTED) 14] set /node_1 998
cZxid = 0x200000002
ctime = Sat Aug 27 15:24:39 CST 2016
mZxid = 0x20000000a
mtime = Sat Aug 27 15:38:40 CST 2016
pZxid = 0x200000008
cversion = 5
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 3
如果我们多次修改,会发现 dataVersion ,也就是数据版本,在不停得发生变化(自增)
如果我们在set的时候手动去指定了版本号,就必须和上一次查询出来的结果一致,否则 就会报错。
这个可以用于我们在修改节点数据的时候,保证我们修改前数据没被别人修改过。因为如果别人修改过了,我们这次修改是不会成功的
删除指令:
delete path [version]
删除指定节点数据,其version参数的作用于set指定一致
delete /node_1/node_1_10000000001
整个节点全删除
注意:delete只能删除不包含子节点的节点,如果要删除的节点包含子节点,使用rmr命令
rmr /node_1