zookeeper cli

本文根据ZooKeeper-cli: the ZooKeeper command line interface对一些常见的zookeeper cli命令做一些笔记.

登录

我们可以使用zkCli命令来登录上zookeeper.

# 本地登录,默认为localhost:2181
[thinktik@ThinkOnline bin]$ ./zkCli.sh
# 指定host和port,我们可以使用这个命令加上-server参数来进行远程登录
[thinktik@ThinkOnline bin]$ ./zkCli.sh -server localhost:2181

查看文档

我们可以使用zookeeper时通过help来查看文档

[zk: localhost:2181(CONNECTED) 0] help
ZooKeeper -server host:port -client-configuration properties-file cmd args
    addWatch [-m mode] path # optional mode is one of [PERSISTENT, PERSISTENT_RECURSIVE] - default is PERSISTENT_RECURSIVE
    addauth scheme auth
    close 
    config [-c] [-w] [-s]
    connect host:port
    create [-s] [-e] [-c] [-t ttl] path [data] [acl]
    delete [-v version] path
    deleteall path [-b batch size]
    delquota [-n|-b] path
    get [-s] [-w] path
    getAcl [-s] path
    getAllChildrenNumber path
    getEphemerals path
    history 
    listquota path
    ls [-s] [-w] [-R] path
    printwatches on|off
    quit 
    reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*]
    redo cmdno
    removewatches path [-c|-d|-a] [-l]
    set [-s] [-v version] path data
    setAcl [-s] [-v version] [-R] path acl
    setquota -n|-b val path
    stat [-w] path
    sync path
    version 
Command not found: Command not found help

创建节点

我们可以使用create [-s] [-e] [-c] [-t ttl] path [data] [acl]来创建节点,默认节点类型为persistent节点

创建persistent节点

使用create创建的节点默认为persistent节点

[zk: localhost:2181(CONNECTED) 9] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 10] create /persistent_node_test
Created /persistent_node_test
[zk: localhost:2181(CONNECTED) 11] create /persistent_node_test/p_data thinktik
Created /persistent_node_test/p_date
[zk: localhost:2181(CONNECTED) 12] get /persistent_node_test
null
[zk: localhost:2181(CONNECTED) 14] get /persistent_node_test/p_data
thinktik

创建ephemeral节点

我们可以在创建节点的时候加上-e参数来创建临时节点,临时节点的特点是如果当前的连接被断开,那么这个节点会自动的被删除.

[zk: localhost:2181(CONNECTED) 24] create -e /ephemeral_node_test thinktik
Created /ephemeral_node_test
[zk: localhost:2181(CONNECTED) 25] ls /
[ephemeral_node_test, zookeeper]
[zk: localhost:2181(CONNECTED) 27] get /ephemeral_node_test
thinktik

创建sequential节点

对于前面的2种节点,我们也可以加上-s来创建sequential节点,sequential节点会自动的为你加上一个单调递增的序列号

# 创建persistent并且为sequential的节点
[zk: localhost:2181(CONNECTED) 6] create -s /s- thinktik
Created /s-0000000008
[zk: localhost:2181(CONNECTED) 7] get /s-0000000008
thinktik
# 创建ephemeral并且为sequential的节点
[zk: localhost:2181(CONNECTED) 8] create -e -s /s- thinktik
Created /s-0000000009
[zk: localhost:2181(CONNECTED) 10] get /s-0000000009
thinktik
[zk: localhost:2181(CONNECTED) 9] ls /
[s-0000000008, s-0000000009, zookeeper]

创建container节点

container节点是zookeeper 3.5.3开始提供的新的节点类型,它的主要特点就是如果Container节点下面没有子节点,则Container节点 在未来会被Zookeeper自动清除,定时任务默认60s 检查一次

The time interval in milliseconds for each check of candidate container and ttl nodes. Default is "60000"

我们使用-c参数即可

# 创建容器节点
[zk: localhost:2181(CONNECTED) 19] create -c /container thinktik_c0
Created /container
# 创建容器节点中的子节点
[zk: localhost:2181(CONNECTED) 20] create /container/sub1 thinktik_c1
Created /container/sub1
[zk: localhost:2181(CONNECTED) 21] ls /
[container, zookeeper]
[zk: localhost:2181(CONNECTED) 22] ls /container
[sub1]
# 删除容器节点中的子节点
[zk: localhost:2181(CONNECTED) 23] delete /container/sub1
# 此时容器节点已经空了,等待大约60s,如果该节点中还是没有子节点,那么会被自动删除
[zk: localhost:2181(CONNECTED) 27] ls /container
[]
# 已经被自动删除了
[zk: localhost:2181(CONNECTED) 30] ls /
[zookeeper]

创建TTL节点

TTL节点就是有生命周期的节点,你在创建的时候指定过期时间,那么它会在你规定的时间后自动的被删除.

# 如果你不开启-Dzookeeper.extendedTypesEnabled=true的话,TTL节点是无法创建.
[zk: localhost:2181(CONNECTED) 1] create -t 60 /ttl_test thinktik
KeeperErrorCode = Unimplemented for /ttl_test
# 开启后就可以了
[zk: localhost:2181(CONNECTED) 0] create -t 60000 /ttl_node thinktik
Created /ttl_node

我们可以这样添加启动参数

# 修改启动脚本,添加-Dzookeeper.extendedTypesEnabled=true
[thinktik@ThinkOnline bin]$ vim ./zkServer.sh

zookeeper cli_第1张图片

删除节点

我们可以使用delete来删除节点

# 使用delete可以删除节点,但是它只能直接删除某个节点,如果该节点下有子节点时不能递归删除
[zk: localhost:2181(CONNECTED) 22] delete /persistent_node_test
Node not empty: /persistent_node_test
# 我们可以使用deleteall来进行递归删除
[zk: localhost:2181(CONNECTED) 23] deleteall /persistent_node_test

更新节点的值

我们可以使用set命令来更新节点的值

[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] create /p1 thinktik
Created /p1
[zk: localhost:2181(CONNECTED) 2] get /p1
thinktik
# 更新/p1节点的值
[zk: localhost:2181(CONNECTED) 3] set /p1 think
[zk: localhost:2181(CONNECTED) 4] get /p1
think

查看节点状态

我们可以查看节点状态

[zk: localhost:2181(CONNECTED) 6] stat /p1
cZxid = 0x3a
ctime = Mon Feb 15 21:50:10 CST 2021
mZxid = 0x3b
mtime = Mon Feb 15 21:50:20 CST 2021
pZxid = 0x3a
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0

查看节点

[zk: localhost:2181(CONNECTED) 13] ls /
[p1, zookeeper]
# 我们可以添加-R来递归查看子节点
[zk: localhost:2181(CONNECTED) 12] ls -R /p1
/p1
/p1/sp1

查看版本

我们可以这样查看zookeeper version

[zk: localhost:2181(CONNECTED) 7] version
ZooKeeper CLI version: 3.6.2--803c7f1a12f85978cb049af5e4ef23bd8b688715, built on 09/04/2020 12:44 GMT

监听节点

我们可以在获取节点的时候添加-w来监听节点变化

我们需要开启2个窗口在获取节点值的时候添加-w来设置一个watch

A窗口获取值并添加监听器:

[zk: localhost:2181(CONNECTED) 2] get -w /p1 
think
[zk: localhost:2181(CONNECTED) 3] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeDataChanged path:/p1

B窗口更改值:

[zk: localhost:2181(CONNECTED) 15] set /p1 thinktik

查看历史记录

[zk: localhost:2181(CONNECTED) 16] history
6 - stat /p1
7 - version
8 - ls /
9 - create /p1/sp1
10 - ls /
11 - ls -R /
12 - ls -R /p1
13 - ls /
14 - ls /
15 - set /p1 thinktik
16 - history

本文原创链接: zookeeper cli

你可能感兴趣的:(zookeeper)