Zookeeper入门(六)之zkCli.sh对节点的增删改查

参考地址为:https://www.cnblogs.com/sherrykid/p/5813148.html

 

1.连接

在 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

Connecting to 127.0.0.1:2181
2018-09-19 16:48:35,332 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
2018-09-19 16:48:35,336 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=iZ2ze4wl2jm5gr50lqkcu2Z
2018-09-19 16:48:35,337 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_181
2018-09-19 16:48:35,339 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2018-09-19 16:48:35,339 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/lib/jvm/java-8-oracle/jre
2018-09-19 16:48:35,339 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper-3.4.6/bin/../build/classes:/usr/local/zookeeper-3.4.6/bin/../build/lib/*.jar:/usr/local/zookeeper-3.4.6/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper-3.4.6/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper-3.4.6/bin/../lib/netty-3.7.0.Final.jar:/usr/local/zookeeper-3.4.6/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper-3.4.6/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper-3.4.6/bin/../zookeeper-3.4.6.jar:/usr/local/zookeeper-3.4.6/bin/../src/java/lib/*.jar:/usr/local/zookeeper-3.4.6/bin/../conf:
2018-09-19 16:48:35,339 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2018-09-19 16:48:35,340 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2018-09-19 16:48:35,340 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2018-09-19 16:48:35,340 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
2018-09-19 16:48:35,340 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
2018-09-19 16:48:35,340 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=4.4.0-117-generic
2018-09-19 16:48:35,340 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root
2018-09-19 16:48:35,341 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root
2018-09-19 16:48:35,341 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/usr/local/zookeeper-3.4.6/bin
2018-09-19 16:48:35,344 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=5000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@506c589e
Welcome to ZooKeeper!
2018-09-19 16:48:35,410 [myid:] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@975] - 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
2018-09-19 16:48:35,580 [myid:] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@852] - Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
2018-09-19 16:48:35,591 [myid:] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1235] - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x165f0e74c000003, negotiated timeout = 5000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
如上,表示我们和ZooKeeper建立了连接,可以通过命令与ZooKeeper进行交互了


2.查看命令帮助
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.png放到桌面

大多数时候,我们对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节点的子节点及状态信息

3.创建指令
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 可以同时使用

 



4.退出:
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子节点不见了,那是因为之前创建的时候就已经指定了,我们创建的是一个临时节点。当客户端会话结束后,临时节点是会被删除了。

 

5.修改相关指定:

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的时候手动去指定了版本号,就必须和上一次查询出来的结果一致,否则 就会报错。

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

 

 

6.删除指令:

delete path [version]

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

 

delete /node_1/node_1_10000000001
整个节点全删除

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

rmr /node_1

posted @ 2018-09-19 18:54  挑战者V  阅读(556)  评论(0编辑  收藏  举报