當(dāng)前位置:首頁 > IT技術(shù) > 系統(tǒng)服務(wù) > 正文

ZooKeeper快速入門系列(5) | Zookeeper常用的shell操作
2021-09-02 20:53:45

??此篇的操作都是在上篇的基礎(chǔ)上進(jìn)行的。


下 表 為 客 戶 端 常 用 的 s h e l l 操 作 color{#FF0000}{下表為客戶端常用的shell操作} shell

命令基本語法 功能描述
help 顯示所有操作命令
ls path [watch] 使用 ls 命令來查看當(dāng)前znode中所包含的內(nèi)容
ls2 path [watch] 查看當(dāng)前節(jié)點(diǎn)數(shù)據(jù)并能看到更新次數(shù)等數(shù)據(jù)
create 普通創(chuàng)建
create -s 含有序列
create -e 臨時(shí)(重啟或者超時(shí)消失)
get path [watch] 獲得節(jié)點(diǎn)的值
set 設(shè)置節(jié)點(diǎn)的具體值
stat 查看節(jié)點(diǎn)狀態(tài)
delete 刪除節(jié)點(diǎn)
rmr 遞歸刪除節(jié)點(diǎn)
1. 啟動(dòng)客戶端
[bigdata@hadoop002 zookeeper-3.4.10]$ bin/zkCli.sh

ZooKeeper快速入門系列(5) | Zookeeper常用的shell操作_客戶端

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

ZooKeeper快速入門系列(5) | Zookeeper常用的shell操作_zookeeper_02

3. 查看當(dāng)前znode中所包含的內(nèi)容
[zk: localhost:2181(CONNECTED) 2] ls /

ZooKeeper快速入門系列(5) | Zookeeper常用的shell操作_客戶端_03

4. 查看當(dāng)前節(jié)點(diǎn)詳細(xì)數(shù)據(jù)
[zk: localhost:2181(CONNECTED) 3] ls2 /

ZooKeeper快速入門系列(5) | Zookeeper常用的shell操作_數(shù)據(jù)_04

5. 分別創(chuàng)建2個(gè)普通節(jié)點(diǎn)
[zk: localhost:2181(CONNECTED) 6] create /sanguo "jinlian" Created /sanguo
Node already exists: /sanguo
[zk: localhost:2181(CONNECTED) 7] create /sanguo/shuguo "liubei"          
Created /sanguo/shuguo

6. 獲取節(jié)點(diǎn)的值
[zk: localhost:2181(CONNECTED) 8] get /sanguo

[zk: localhost:2181(CONNECTED) 9] get /sanguo/shuguo

ZooKeeper快速入門系列(5) | Zookeeper常用的shell操作_客戶端_05

7. 創(chuàng)建短暫節(jié)點(diǎn)
[zk: localhost:2181(CONNECTED) 10] create -e /sanguo/wuguo "zhouyu"
Created /sanguo/wuguo

  • 1. 在當(dāng)前客戶端時(shí)能查看到的
[zk: localhost:2181(CONNECTED) 11] ls /sanguo 
[wuguo, shuguo]

  • 2. 退出當(dāng)前客戶端然后再重啟客戶端
[zk: localhost:2181(CONNECTED) 12] quit
[bigdata@hadoop002 zookeeper-3.4.10]$ bin/zkCli.sh
  • 3. 再次查看根目錄下短暫節(jié)點(diǎn)已經(jīng)刪除
[zk: localhost:2181(CONNECTED) 0] ls /sanguo
[shuguo]
8. 創(chuàng)建帶序號(hào)的節(jié)點(diǎn)
  • 1.先創(chuàng)建一個(gè)普通的根節(jié)點(diǎn)/sanguo/weiguo
[zk: localhost:2181(CONNECTED) 1] create /sanguo/weiguo "caocao"
Created /sanguo/weiguo
  • 2. 創(chuàng)建帶序號(hào)的節(jié)點(diǎn)
    ZooKeeper快速入門系列(5) | Zookeeper常用的shell操作_數(shù)據(jù)_06
    ??如果原來沒有序號(hào)節(jié)點(diǎn),序號(hào)從0開始依次遞增。如果原節(jié)點(diǎn)下已有2個(gè)節(jié)點(diǎn),則再排序時(shí)從2開始,以此類推。

9. 修改節(jié)點(diǎn)數(shù)據(jù)值
[zk: localhost:2181(CONNECTED) 5] set /sanguo/weiguo "simayi"

ZooKeeper快速入門系列(5) | Zookeeper常用的shell操作_數(shù)據(jù)_07

10. 節(jié)點(diǎn)的值變化監(jiān)聽
  • 1. 在hadoop004主機(jī)上注冊(cè)監(jiān)聽/sanguo節(jié)點(diǎn)數(shù)據(jù)變化
[zk: localhost:2181(CONNECTED) 26] [zk: localhost:2181(CONNECTED) 8] get /sanguo watch
  • 2. 在hadoop003主機(jī)上修改/sanguo節(jié)點(diǎn)的數(shù)據(jù)
[zk: localhost:2181(CONNECTED) 1] set /sanguo "xisi"
  • 3. 觀察hadoop004主機(jī)收到數(shù)據(jù)變化的監(jiān)聽
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/sanguo
  • 4. 圖解
    ZooKeeper快速入門系列(5) | Zookeeper常用的shell操作_客戶端_08
11. 節(jié)點(diǎn)的子節(jié)點(diǎn)變化監(jiān)聽(路徑變化)
  • 1. 在hadoop004主機(jī)上注冊(cè)監(jiān)聽/sanguo節(jié)點(diǎn)的子節(jié)點(diǎn)變化
[zk: localhost:2181(CONNECTED) 1] ls /sanguo watch
[shuguo, weiguo]
  • 2. 在hadoop003主機(jī)/sanguo節(jié)點(diǎn)上創(chuàng)建子節(jié)點(diǎn)
[zk: localhost:2181(CONNECTED) 2] create /sanguo/jin "simayi"
Created /sanguo/jin
  • 3. 觀察hadoop004主機(jī)收到子節(jié)點(diǎn)變化的監(jiān)聽
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/sanguo
  • 4. 圖解

ZooKeeper快速入門系列(5) | Zookeeper常用的shell操作_數(shù)據(jù)_09

12. 刪除節(jié)點(diǎn)
[zk: localhost:2181(CONNECTED) 4] delete /sanguo/jin
13. 遞歸刪除節(jié)點(diǎn)
[zk: localhost:2181(CONNECTED) 15] rmr /sanguo/shuguo
14. 查看節(jié)點(diǎn)狀態(tài)
[zk: localhost:2181(CONNECTED) 17] stat /sanguo

ZooKeeper快速入門系列(5) | Zookeeper常用的shell操作_大數(shù)據(jù)_10

本篇博客就到這里了,下一篇博客博主將為大家?guī)鞿ookeeper的API操作,敬請(qǐng)期待!!!


ZooKeeper快速入門系列(5) | Zookeeper常用的shell操作_數(shù)據(jù)_11

?? 看 完 就 贊 , 養(yǎng) 成 習(xí) 慣 ! ! ! color{#FF0000}{看完就贊,養(yǎng)成習(xí)慣?。?!} ,養(yǎng)習(xí)!!^ _ ^ ?? ?? ??
??碼字不易,大家的支持就是我堅(jiān)持下去的動(dòng)力。點(diǎn)贊后不要忘了關(guān)注我哦!

本文摘自 :https://blog.51cto.com/u

開通會(huì)員,享受整站包年服務(wù)立即開通 >