zookeeper cli

本文根據ZooKeeper-cli: the ZooKeeper command line interface對一些常見的zookeeper cli命令作一些筆記.html

登陸

咱們可使用zkCli命令來登陸上zookeeper.node

# 本地登陸,默認爲localhost:2181
[thinktik@ThinkOnline bin]$ ./zkCli.sh
# 指定host和port,咱們可使用這個命令加上-server參數來進行遠程登陸
[thinktik@ThinkOnline bin]$ ./zkCli.sh -server localhost:2181

查看文檔

咱們可使用zookeeper時經過help來查看文檔apache

[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節點vim

建立persistent節點

使用create建立的節點默認爲persistent節點ui

[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參數來建立臨時節點,臨時節點的特色是若是當前的鏈接被斷開,那麼這個節點會自動的被刪除.spa

[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節點會自動的爲你加上一個單調遞增的序列號code

# 建立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 檢查一次server

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

咱們使用-c參數便可htm

# 建立容器節點
[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節點就是有生命週期的節點,你在建立的時候指定過時時間,那麼它會在你規定的時間後自動的被刪除.blog

# 若是你不開啓-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

image

刪除節點

咱們可使用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

相關文章
相關標籤/搜索