ZooKeeper命令行工具zkCli.sh

1.简介

ZooKeeper提供了一个非常简单的命令行客户端zkCli,它在ZooKeeper安装目录的bin目录下。

[root@node02 ~]# ls /usr/local/zookeeper/bin/
README.txt  zkCleanup.sh  zkCli.cmd  zkCli.sh  zkEnv.cmd  zkEnv.sh  zkServer.cmd  zkServer.sh

先连接一个已经启动的ZooKeeper实例。例如:

[root@node02 ~]# zkCli.sh -server localhost:2181
Connecting to localhost:2181
2020-03-06 14:47:51,339 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.7-1713338, built on 11/09/2015 04:32 GMT
2020-03-06 14:47:51,342 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=node02
2020-03-06 14:47:51,342 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_191
2020-03-06 14:47:51,344 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2020-03-06 14:47:51,344 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/local/jdk1.8.0_191/jre
2020-03-06 14:47:51,344 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper/bin/../build/classes:/usr/local/zookeeper/bin/../build/lib/*.jar:/usr/local/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper/bin/../lib/netty-3.7.0.Final.jar:/usr/local/zookeeper/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper/bin/../zookeeper-3.4.7.jar:/usr/local/zookeeper/bin/../src/java/lib/*.jar:/usr/local/zookeeper/bin/../conf:
2020-03-06 14:47:51,344 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2020-03-06 14:47:51,345 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2020-03-06 14:47:51,345 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2020-03-06 14:47:51,345 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
2020-03-06 14:47:51,345 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
2020-03-06 14:47:51,345 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=3.10.0-693.el7.x86_64
2020-03-06 14:47:51,345 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root
2020-03-06 14:47:51,345 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root
2020-03-06 14:47:51,345 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/root
2020-03-06 14:47:51,346 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@1a86f2f1
Welcome to ZooKeeper!
2020-03-06 14:47:51,401 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2020-03-06 14:47:51,576 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@876] - Socket connection established to localhost/127.0.0.1:2181, initiating session
2020-03-06 14:47:51,608 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x370ae8d1f490000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] 

连接过程中会输出一大堆信息。当连接成功后,将进入ZooKeeper的交互式模式:

WatchedEvent state:SyncConnected type:None path:null

[zk: localhost:2181(CONNECTED) 0]   # 在这里输入ZooKeeper允许的命令

例如,输入help输出帮助信息

[zk: localhost:2181(CONNECTED) 0]  help
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
[zk: localhost:2181(CONNECTED) 1] 

其中交互式命令提示符中有一个不断递增的数字,例如上面执行依次操作后,从0变成1。这个数字代表的是命令的历史编号。

也可以使用zkCli.sh的非交互式模式来执行一次性操作,格式为:

zkCli.sh -server IP:PORT COMMAND

例如help命令:

zkCli.sh -server localhost:2181 help

在交互式环境中,可以使用TAB键补全,或列出可用的命令、路径。就像bash一样。

ZooKeeper的命令很少很简单,大致分为几个类型:

create:在树中的某个位置创建一个节点。
delete:删除一个节点。
exists:测试一个节点是否存在。
get data:读取节点数据。
set data:向节点中写入数据。
get children:检索某节点的子节点列表。
sync:等待要传播的数据。

注意,这只是命令的功能类型,并不意味着它们就是命令。

2.创建znode

create [-s] [-e] path data acl
-s:创建的是带序列号的节点,序列号用0填充节点路径。
-e:创建的是临时节点。
path:znode的路径,ZooKeeper中没有相对路径,所有路径都必须以'/'开头。
data:znode携带的数据。
acl:这个节点的ACL。

例如,创建一个znode节点:

[zk: localhost:2181(CONNECTED) 1] create /zk_test mydata1
Created /zk_test

其中/zk_test是节点名称,mydata1是该节点携带的数据。

创建一个子节点:

[zk: localhost:2181(CONNECTED) 2] create /zk_test/child_node1 "mydata2"
Created /zk_test/child_node1

创建一个带序列号的znode:

[zk: localhost:2181(CONNECTED) 3] create -s /test1 222
Created /test10000000002

注意,创建了带序列号的znode后,以后只能使用带序列号的路径/test10000000003来引用这个znode,而不能用/test1来引用。

创建一个临时znode:

[zk: localhost:2181(CONNECTED) 4] create -e /test2 333
Created /test2

临时znode在会话退出时会自动删除,所以不能在临时节点上创建子节点。另外,虽然临时节点属于某会话,但所有客户端都可以查看、引用它。

3.查看节点的数据和状态信息

[zk: localhost:2181(CONNECTED) 5] get /zk_test
mydata1
cZxid = 0x200000002
ctime = Fri Mar 06 14:56:29 CST 2020
mZxid = 0x200000002
mtime = Fri Mar 06 14:56:29 CST 2020
pZxid = 0x200000003
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 1
[zk: localhost:2181(CONNECTED) 6] get /zk_test/child_node1
mydata2
cZxid = 0x200000003
ctime = Fri Mar 06 14:57:16 CST 2020
mZxid = 0x200000003
mtime = Fri Mar 06 14:57:16 CST 2020
pZxid = 0x200000003
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0

如果只是要获取节点的状态信息,可以使用stat命令:

[zk: localhost:2181(CONNECTED) 7] stat /zk_test
cZxid = 0x200000002
ctime = Fri Mar 06 14:56:29 CST 2020
mZxid = 0x200000002
mtime = Fri Mar 06 14:56:29 CST 2020
pZxid = 0x200000003
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 1

ls2命令是lsstat两个命令的结合体,可以获取给定节点下的子节点,以及给定节点的状态信息:

[zk: localhost:2181(CONNECTED) 8] ls2 /
[test2, zookeeper, test_znode, zk_test, test10000000002] # 子节点列表
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x200000005
cversion = 3
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 5  # 子节点数量

4.查看子节点

ls命令查看某节点下的子节点。注意,不会递归检索。

[zk: localhost:2181(CONNECTED) 9] ls /
[test2, zookeeper, test_znode, zk_test, test10000000002]
[zk: localhost:2181(CONNECTED) 10] ls /zk_test
[child_node1]

5.修改节点数据

set命令用于修改节点:

[zk: localhost:2181(CONNECTED) 11] set /zk_test "mydata2"
cZxid = 0x200000002
ctime = Fri Mar 06 14:56:29 CST 2020
mZxid = 0x200000006
mtime = Fri Mar 06 15:04:07 CST 2020
pZxid = 0x200000003
cversion = 1
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 1

6.删除节点

delete可以删除节点。注意,它不会递归删除,所以要删除一个包含子节点的节点,需要先删除所有子节点。

[zk: localhost:2181(CONNECTED) 12] delete /zk_test
Node not empty: /zk_test
[zk: localhost:2181(CONNECTED) 13] delete /zk_test/child_node1

rmr命令可以递归删除节点,所以也会删除节点中的所有子节点。

[zk: localhost:2181(CONNECTED) 14] create /test/hello 2323
Node does not exist: /test/hello
[zk: localhost:2181(CONNECTED) 15] create /test/hello/hello1 23233
Node does not exist: /test/hello/hello1
[zk: localhost:2181(CONNECTED) 16] rmr /test
Node does not exist: /test
[zk: localhost:2181(CONNECTED) 17] ls /
[test2, zookeeper, test_znode, zk_test, test10000000002]

7.history和redo

history命令可以列出最近操作的10条命令历史,并给出每个历史命令的编号。redo命令可以根据历史命令的编号重新调用这些命令。

[zk: localhost:2181(CONNECTED) 18] history
8 - ls2 /
9 - ls /
10 - ls /zk_test
11 - set /zk_test "mydata2"
12 - delete /zk_test
13 - delete /zk_test/child_node1
14 - create /test/hello 2323
15 - create /test/hello/hello1 23233
16 - rmr /test
17 - ls /
18 - history

重新执行编号为13的历史命令:

[zk: localhost:2181(CONNECTED) 19] redo 13
Node does not exist: /zk_test/child_node1

8.connect和close

connect命令用于连接其它ZooKeeper服务器,close用于关闭当前连接。就像ssh到其它服务器,然后exit一样。

[zk: localhost:2181(CONNECTED) 20] connect 192.168.1.220:2181
[zk: 192.168.1.220:2181(CONNECTED) 21] close
# 注意下面的ZooKeeper shell提示符中地址和连接状态的变化:

需要注意,当connect到其它服务器后,当前的连接就会断开,当前会话中的临时znode就会被删除。以下是connect到其它服务器时zookeeper.out中相关内容:

2020-03-06 15:10:48,582 [myid:1] - INFO  [CommitProcessor:1:ZooKeeperServer@643] - Established session 0x170ae8d1f450000 with negotiated timeout 30000 for client /192.168.1.222:49426
2020-03-06 15:11:52,525 [myid:1] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@1008] - Closed socket connection for client /192.168.1.222:49426 which had sessionid 0x170ae8d1f450000

所以,上面执行了close后,不会切换回之前的连接,需要手动再连接。

[zk: 192.168.1.220:2181(CLOSED) 22] close
Not connected
[zk: 192.168.1.220:2181(CLOSED) 23] close
Not connected
[zk: 192.168.1.220:2181(CLOSED) 24] ls /
Not connected
[zk: 192.168.1.220:2181(CLOSED) 25] connect localhost:2181
[zk: localhost:2181(CONNECTED) 26]  ls /
[zookeeper, test_znode, zk_test, test10000000002]

9.quit

[zk: localhost:2181(CONNECTED) 27] quit
Quitting...
2020-03-06 15:22:15,233 [myid:] - INFO  [main:ZooKeeper@684] - Session: 0x370ae8d1f490001 closed
2020-03-06 15:22:15,234 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x370ae8d1f490001

 

posted @ 2020-03-06 15:23  星火撩原  阅读(2021)  评论(0编辑  收藏  举报