(3)zookeeper常用命令及节点属性介绍
本节介绍上节搭建完成了一个zookeeper集群后,本节将介绍一下zookeeper的一些客户端命令的使用方法。zookeeper命令首先启动zookeeper服务端,然后通过zkCli.sh脚本启动客户端。[root@zookeeper zookeeper-3.4.6]# ./bin/zkCli.shConnecting to localhost:21812018-09-0...
本节介绍
上节搭建完成了一个zookeeper集群后,本节将介绍一下zookeeper的一些客户端命令的使用方法。
zookeeper命令
首先启动zookeeper服务端,然后通过zkCli.sh脚本启动客户端。
[root@zookeeper zookeeper-3.4.6]# ./bin/zkCli.sh
Connecting to localhost:2181
2018-09-07 16:26:12,462 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
2018-09-07 16:26:12,464 [myid:] - INFO [main:Environment@100] - Client environment:host.name=zookeeper
2018-09-07 16:26:12,465 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.7.0_79
2018-09-07 16:26:12,466 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2018-09-07 16:26:12,466 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/local/jdk1.7.0_79/jre
2018-09-07 16:26:12,466 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/usr/local/soft/zookeeper-3.4.6/bin/../build/classes:/usr/local/soft/zookeeper-3.4.6/bin/../build/lib/*.jar:/usr/local/soft/zookeeper-3.4.6/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/soft/zookeeper-3.4.6/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/soft/zookeeper-3.4.6/bin/../lib/netty-3.7.0.Final.jar:/usr/local/soft/zookeeper-3.4.6/bin/../lib/log4j-1.2.16.jar:/usr/local/soft/zookeeper-3.4.6/bin/../lib/jline-0.9.94.jar:/usr/local/soft/zookeeper-3.4.6/bin/../zookeeper-3.4.6.jar:/usr/local/soft/zookeeper-3.4.6/bin/../src/java/lib/*.jar:/usr/local/soft/zookeeper-3.4.6/bin/../conf:.:/usr/local/jdk1.7.0_79/lib:/usr/local/jdk1.7.0_79/jre/lib:
2018-09-07 16:26:12,467 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2018-09-07 16:26:12,467 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2018-09-07 16:26:12,467 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2018-09-07 16:26:12,467 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2018-09-07 16:26:12,467 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2018-09-07 16:26:12,467 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-514.26.2.el7.x86_64
2018-09-07 16:26:12,467 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2018-09-07 16:26:12,467 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2018-09-07 16:26:12,467 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/usr/local/soft/zookeeper-3.4.6
2018-09-07 16:26:12,469 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@4b4bc1e
Welcome to ZooKeeper!
2018-09-07 16:26:12,491 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@975] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2018-09-07 16:26:12,495 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@852] - Socket connection established to localhost/127.0.0.1:2181, initiating session
JLine support is enabled
2018-09-07 16:26:12,525 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1235] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x165b2028b390000, negotiated timeout = 30000
[zk: localhost:2181(CONNECTED) 0]
WATCHER::WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
我们看到客户端已经启动了。通过执行 help ,我们可以看到可用的zookeeper命令
[zk: localhost:2181(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
connect host:port
get path [watch]
ls path [watch]
set path data [version]
rmr path
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
[zk: localhost:2181(CONNECTED) 1]
常用zookeeper命令操作使用
1、使用ls命令查看当前Zookeeper中所包含的内容:ls /
[zk: localhost:2181(CONNECTED) 1] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 2]
2、使用create命令创建一个新的Znode节点"testNode",并设置内容为字符串"test",执行命令:create /testNode test
[zk: localhost:2181(CONNECTED) 2] create /testNode test
Created /testNode
[zk: localhost:2181(CONNECTED) 3] ls /
[testNode, zookeeper]
[zk: localhost:2181(CONNECTED) 4]
我们再次通过 ls / 命令查看,发现testNode节点已经被创建了。
3、通过get命令可以查看我们创建的节点,执行命令:get /testNode
[zk: localhost:2181(CONNECTED) 4] get /testNode
test
cZxid = 0x2
ctime = Fri Sep 07 17:28:23 CST 2018
mZxid = 0x2
mtime = Fri Sep 07 17:28:23 CST 2018
pZxid = 0x2
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
[zk: localhost:2181(CONNECTED) 5]
我们可以看到上面我们设置的字符串内容“test”
4、我们可以通过set命令对我们添加的节点进行修改,执行命令: set /testNode newTest
[zk: localhost:2181(CONNECTED) 5] set /testNode newTest
cZxid = 0x2
ctime = Fri Sep 07 17:28:23 CST 2018
mZxid = 0x3
mtime = Fri Sep 07 17:37:54 CST 2018
pZxid = 0x2
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0
[zk: localhost:2181(CONNECTED) 6] get /testNode
newTest
cZxid = 0x2
ctime = Fri Sep 07 17:28:23 CST 2018
mZxid = 0x3
mtime = Fri Sep 07 17:37:54 CST 2018
pZxid = 0x2
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0
[zk: localhost:2181(CONNECTED) 7]
执行完set之后,我们再次通过get命令查看得治,testNode节点已经被修改了。
5、stat命令可以查看节点信息,(通过执行结果我们可以看到,其跟get命令的区别就是没有打印出节点的内容),执行命令: stat /testNode
[zk: localhost:2181(CONNECTED) 7] stat /testNode
cZxid = 0x2
ctime = Fri Sep 07 17:28:23 CST 2018
mZxid = 0x3
mtime = Fri Sep 07 17:37:54 CST 2018
pZxid = 0x2
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0
6、我们可以通过delete命令删除对应的节点,执行命令: delete /testNode
[zk: localhost:2181(CONNECTED) 8] delete /testNode
[zk: localhost:2181(CONNECTED) 9] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 10]
我们再次通过 ls / 命令可以看到,testNode节点已经被删除了。
7、rmr 命令有点像Linux中的rm -rf 可以删除目录及目录下所有内容,
[zk: localhost:2181(CONNECTED) 10] create /a aaaa
Created /a
[zk: localhost:2181(CONNECTED) 11] ls /
[zookeeper, a]
[zk: localhost:2181(CONNECTED) 12] create /a/a1 a111
Created /a/a1
[zk: localhost:2181(CONNECTED) 13] delete /a
Node not empty: /a
[zk: localhost:2181(CONNECTED) 14] rmr /a
[zk: localhost:2181(CONNECTED) 15] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 16]
我们创建一个a节点,然后给a节点创建一个子节点a1,然后执行delete命令发现无法删除,我们可以发现delete无法删除带有子节点的节点,而用rmr 就可以删除。然后通过ls 命令发现a节点已经被删除了。
8、history命令就没什么好说的了,就是列出执行过的命令
[zk: localhost:2181(CONNECTED) 16] history
6 - get /testNode
7 - stat /testNode
8 - delete /testNode
9 - ls /
10 - create /a aaaa
11 - ls /
12 - create /a/a1 a111
13 - delete /a
14 - rmr /a
15 - ls /
16 - history
[zk: localhost:2181(CONNECTED) 17]
9、quit就是退出当前客户端连接(Ctrl+z 也可以退出)
[zk: localhost:2181(CONNECTED) 17] quit
Quitting...
2018-09-07 17:55:47,581 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x165b2028b390000 closed
2018-09-07 17:55:47,581 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@512] - EventThread shut down
zookeeper节点属性介绍
ZooKeeper有多种记录时间的形式,其中包含以下几个主要属性:
(1) Zxid
致使ZooKeeper节点状态改变的每一个操作都将使节点接收到一个Zxid格式的时间戳,并且这个时间戳全局有序。也就是说,也就是说,每个对节点的改变都将产生一个唯一的Zxid。如果Zxid1的值小于Zxid2的值,那么Zxid1所对应的事件发生在Zxid2所对应的事件之前。实际上,ZooKeeper的每个节点维护者三个Zxid值,为别为:cZxid、mZxid、pZxid。
cZxid: 是节点的创建时间所对应的Zxid格式时间戳(Create)。
mZxid:是节点的修改时间所对应的Zxid格式时间戳(Mofify)。
pZxid:这个节点就和子节点有关啦!是与 该节点的子节点(或该节点)的最近一次 创建 / 删除 的时间戳对应。(注:只与 本节点 / 该节点的子节点,有关;与孙子节点无关)
实现中Zxid是一个64为的数字,它高32位是epoch用来标识leader关系是否改变,每次一个leader被选出来,它都会有一个 新的epoch。低32位是个递增计数。
(2) 版本号
对节点的每一个操作都将致使这个节点的版本号增加。每个节点维护着三个版本号,他们分别为:
① version:节点数据版本号
② cversion:子节点版本号
③ aversion:节点所拥有的ACL版本号
zookeeper节点属性介绍
其实上面我们在演示create、set命令的时候,我们看到一些属性比如修改时间、数据版本号等已经出现了改变,您可以通过创建节点、修改节点、创建修改子节点等操作,然后进行查看节点相关属性的变化。
下一节会介绍一下zookeeper的原生java客户端。
更多推荐
所有评论(0)