zookeeper在Linux客戶端操作命令大全

z猿猿不斷發表於2020-11-20

一、基本操作介紹

命令基本語法

功能描述

help

顯示所有操作命令

ls path

使用 ls 命令來檢視當前znode的子節點

-w  監聽子節點變化

-s   附加次級資訊

create

普通建立

-s  含有序列

-e  臨時(重啟或者超時消失)

get path

獲得節點的值

-w  監聽節點內容變化

-s   附加次級資訊

set

設定節點的具體值

stat

檢視節點狀態

delete

刪除節點

deleteall

遞迴刪除節點

 

二、操作演示

1)啟動客戶端
      bin/zkCli.sh


2)顯示所有操作命令
[zk: localhost:2181(CONNECTED) 1] help


3)檢視當前znode中所包含的內容
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]


4)檢視當前節點詳細資料
[zk: localhost:2181(CONNECTED) 1] ls2 /
[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


5)分別建立2個普通節點
[zk: localhost:2181(CONNECTED) 3] create /sanguo "jinlian"
Created /sanguo
[zk: localhost:2181(CONNECTED) 4] create /sanguo/shuguo "liubei"
Created /sanguo/shuguo


6)獲得節點的值
[zk: localhost:2181(CONNECTED) 5] get /sanguo
jinlian
cZxid = 0x100000003
ctime = Wed Aug 29 00:03:23 CST 2018
mZxid = 0x100000003
mtime = Wed Aug 29 00:03:23 CST 2018
pZxid = 0x100000004
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 1
[zk: localhost:2181(CONNECTED) 6]
[zk: localhost:2181(CONNECTED) 6] get /sanguo/shuguo
liubei
cZxid = 0x100000004
ctime = Wed Aug 29 00:04:35 CST 2018
mZxid = 0x100000004
mtime = Wed Aug 29 00:04:35 CST 2018
pZxid = 0x100000004
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0


7)建立短暫節點
      [zk: localhost:2181(CONNECTED) 7] create -e /sanguo/wuguo "zhouyu"
      Created /sanguo/wuguo


(1)在當前客戶端是能檢視到的
          [zk: localhost:2181(CONNECTED) 3] ls /sanguo 
         [wuguo, shuguo]


(2)退出當前客戶端然後再重啟客戶端
         [zk: localhost:2181(CONNECTED) 12] quit
          bin/zkCli.sh


(3)再次檢視根目錄下短暫節點已經刪除
          [zk: localhost:2181(CONNECTED) 0] ls /sanguo
        [shuguo]


8)建立帶序號的節點
    (1)先建立一個普通的根節點/sanguo/weiguo
             [zk: localhost:2181(CONNECTED) 1] create /sanguo/weiguo "caocao"
             Created /sanguo/weiguo


    (2)建立帶序號的節點
            [zk: localhost:2181(CONNECTED) 2] create -s /sanguo/weiguo/xiaoqiao "jinlian"
            Created /sanguo/weiguo/xiaoqiao0000000000
            [zk: localhost:2181(CONNECTED) 3] create -s /sanguo/weiguo/daqiao "jinlian"
            Created /sanguo/weiguo/daqiao0000000001
            [zk: localhost:2181(CONNECTED) 4] create -s /sanguo/weiguo/diaocan "jinlian"
            Created /sanguo/weiguo/diaocan0000000002
         如果原來沒有序號節點,序號從0開始依次遞增。如果原節點下已有2個節點,則再排序時從2開始,以此類推。


9)修改節點資料值
[zk: localhost:2181(CONNECTED) 6] set /sanguo/weiguo "simayi"


10)節點的值變化監聽
    (1)在hadoop104主機上註冊監聽/sanguo節點資料變化
[zk: localhost:2181(CONNECTED) 26] [zk: localhost:2181(CONNECTED) 8] get /sanguo watch
    (2)在hadoop103主機上修改/sanguo節點的資料
[zk: localhost:2181(CONNECTED) 1] set /sanguo "xisi"
    (3)觀察hadoop104主機收到資料變化的監聽
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/sanguo


11)節點的子節點變化監聽(路徑變化)
    (1)在hadoop104主機上註冊監聽/sanguo節點的子節點變化
[zk: localhost:2181(CONNECTED) 1] ls /sanguo watch
[aa0000000001, server101]
    (2)在hadoop103主機/sanguo節點上建立子節點
[zk: localhost:2181(CONNECTED) 2] create /sanguo/jin "simayi"
Created /sanguo/jin
    (3)觀察hadoop104主機收到子節點變化的監聽
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/sanguo


12)刪除節點
[zk: localhost:2181(CONNECTED) 4] delete /sanguo/jin


13)遞迴刪除節點
[zk: localhost:2181(CONNECTED) 15] rmr /sanguo/shuguo


14)檢視節點狀態
[zk: localhost:2181(CONNECTED) 17] stat /sanguo
cZxid = 0x100000003
ctime = Wed Aug 29 00:03:23 CST 2018
mZxid = 0x100000011
mtime = Wed Aug 29 00:21:23 CST 2018
pZxid = 0x100000014
cversion = 9
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 1

 

相關文章