ZooKeeper客户端 zkCli.sh 节点的增删改查

  • 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

 zhangliuningdeMacBook-Pro:bin sherry$ ./zkCli.sh -timeout  -server 127.0.0.1:
Connecting to 127.0.0.1:
-- ::, [myid:] - INFO [main:Environment@] - Client environment:zookeeper.version=3.4.--, built on // : GMT
-- ::, [myid:] - INFO [main:Environment@] - Client environment:host.name=192.168.0.105
-- ::, [myid:] - INFO [main:Environment@] - Client environment:java.version=1.8.0_71
-- ::, [myid:] - INFO [main:Environment@] - Client environment:java.vendor=Oracle Corporation
-- ::, [myid:] - INFO [main:Environment@] - Client environment:java.home=/Library/Java/JavaVirtualMachines/jdk1..0_71.jdk/Contents/Home/jre
-- ::, [myid:] - INFO [main:Environment@] - 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:
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:.
2016-08-27 15:07:04,042 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/var/folders/_n/d05bph5x5bl6k5p0pkx2g66m0000gn/T/
2016-08-27 15:07:04,042 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2016-08-27 15:07:04,042 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Mac OS X
2016-08-27 15:07:04,042 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=x86_64
2016-08-27 15:07:04,042 [myid:] - INFO [main:Environment@100] - Client environment:os.version=10.11.5
2016-08-27 15:07:04,043 [myid:] - INFO [main:Environment@100] - Client environment:user.name=sherry
2016-08-27 15:07:04,043 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/Users/sherry
2016-08-27 15:07:04,043 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/Users/sherry/Applications/zk1/bin
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
Welcome to ZooKeeper!
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)
JLine support is enabled
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
[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 WATCHER:: 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客户端   zkCli.sh  节点的增删改查

大多数时候,我们队ZooKeeper的操作都是在对节点进行增删改查

常用命令:

查询相关指令

ls path:列出path下的文件

[zk: 127.0.0.1:(CONNECTED) ] ls /
[zookeeper]

如上:列出根目录节点下的所有文件,目前就只有一个系统自带的zookeeper节点

stat path:查看节点状态

[zk: 127.0.0.1:(CONNECTED) ] stat /zookeeper
cZxid = 0x0
ctime = Thu Jan :: CST
mZxid = 0x0
mtime = Thu Jan :: CST
pZxid = 0x0
cversion = -
dataVersion =
aclVersion =
ephemeralOwner = 0x0
dataLength =
numChildren =
cZxid:创建节点时的事务id
pZxid:子节点列表最后一次被修改的事务id
cversion:节点版本号
dataCersion:数据版本号
aclVerson:acl权限版本号 ...... get path:获取指定节点的内容 ls2 path:列出path节点的子节点及状态信息
创建指令

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

 [zk: 127.0.0.1:(CONNECTED) ] create /node_1
Created /node_1

如上:在根目录创建了node_1节点,携带数据 123

使用 get /node_1 验证是否添加节点及其数据成功

[zk: 127.0.0.1:(CONNECTED) ] create -e /node_1/node_1_1
Created /node_1/node_1_1

创建了一个临时节点(-e),并且携带数据234,使用stat命令查看这个新建的临时节点

[zk: 127.0.0.1:(CONNECTED) ] stat /node_1/node_1_1
cZxid = 0x200000003
ctime = Sat Aug :: CST
mZxid = 0x200000003
mtime = Sat Aug :: CST
pZxid = 0x200000003
cversion =
dataVersion =
aclVersion =
ephemeralOwner = 0x156cacb918b0000
dataLength =
numChildren =
ephemeralOwner值不再是0,表示这个临时节点的版本号,如果是永久节点则其值为 0x0
 [zk: 127.0.0.1:(CONNECTED) ] create -s /node_1/node_1_1
Created /node_1/node_1_10000000001

通过使用-s参数,创建一个顺序节点,我们虽然指定的节点名是node_1_1,但是实际上,名称却是 node_1_10000000001,如果我们重复执行:

[zk: 127.0.0.1:(CONNECTED) ] create -s /node_1/node_1_1
Created /node_1/node_1_10000000002

效果如上

这个特性,我们可以利用一下,生成在分布式环境下的主键生成器

-s 和 -e 可以同时使用


退出:
quit
[zk: 127.0.0.1:(CONNECTED) ] quit
Quitting...
-- ::, [myid:] - INFO [main:ZooKeeper@] - Session: 0x156cacb918b0000 closed
-- ::, [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@] - EventThread shut down for session: 0x156cacb918b0000

这个时候如果重新登录,执行 ls /node_1,发现其子节点是空的,创建的node_1_1子节点不见了,那是因为之前创建的时候就已经指定了,我们创建的是一个临时节点。当客户端会话结束后,临时节点是会被删除了。

修改相关指定:

set path data [version]

[zk: 127.0.0.1:(CONNECTED) ] set /node_1
cZxid = 0x200000002
ctime = Sat Aug :: CST
mZxid = 0x20000000a
mtime = Sat Aug :: CST
pZxid = 0x200000008
cversion =
dataVersion =
aclVersion =
ephemeralOwner = 0x0
dataLength =
numChildren =

如果我们多次修改,会发现  dataVersion ,也就是数据版本,在不停得发生变化(自增)

如果我们在set的时候手动去指定了版本号,就必须和上一次查询出来的结果一致,否则 就会报错。

这个可以用于我们在修改节点数据的时候,保证我们修改前数据没被别人修改过。因为如果别人修改过了,我们这次修改是不会成功的

删除指令:

delete path [version]

删除指定节点数据,其version参数的作用于set指定一致

delete /node_1/node_1_10000000001

整个节点全删除

注意:delete只能删除不包含子节点的节点,如果要删除的节点包含子节点,使用rmr命令

rmr /node_1
 
 
 
上一篇:RPM包的制作


下一篇:etree和Beautiful Soup的使用