Zookeeper的客戶端操作
| 常用操作命令: 1)一台機器進入Zookeeper客戶端:bin/zkCli.sh 一台機器退出Zookeeper客戶端:quit 2)查看:ls 根目錄(必須加) 3) 啟動:bin/zkServer.sh start 4) 停止:bin/zkServer.sh 5)查看狀態: bin/zkServer.sh status 注:如果希望節點開啟時Zookeeper也隨着啟動,就在配置文件/etc/profile里加上這句-- /opt/module/zookeeper-3.4.10/bin/zkServer.sh start 即可。 |
1.各機器啟動zookeeper
每台機器都啟動zookeeper:
1)先到此目錄下:/opt/module/zookeeper-3.4.10/bin
2)之后依次在bigdata111和bigdata112、bigdata113上輸入命令:
[root@bigdata111 bin]#./zkServer.sh start
3)查看節點工作狀態:
[root@bigdata111 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Mode: follower
[root@bigdata112 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Mode: leader
[root@bigdata113 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Mode: follower
2.停止zookeeper
[root@bigdata113 bin]#./zkServer.sh stop
3.客戶端命令行操作
這里可以先進入bigdata111的客戶端里:
[root@bigdata111 bin]# ./zkCli.sh
后續操作:
| 命令基本語法 |
功能描述 |
| help |
顯示所有操作命令 |
| ls path [watch] |
使用 ls 命令來查看當前znode中所包含的內容【watch--可監聽】 |
| ls2 path [watch] |
查看當前節點數據並能看到更新次數等數據 |
| create |
普通創建(永久節點) -s 含有序列 -e 臨時(重啟或者超時消失) |
| get path [watch] |
獲得節點的值-watch可修改此節點的值 |
| set |
設置節點的具體值 |
| stat |
查看節點狀態 |
| delete |
刪除節點 |
| rmr |
遞歸刪除節點 |
1)啟動客戶端
[itstar@bigdata112 zookeeper-3.4.10]$ 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)創建普通節點
[zk: localhost:2181(CONNECTED) 2] create /app1 "hello app1"
(注意:創建的節點里必須有數據)
Created /app1
例如:
[zk: localhost:2181(CONNECTED) 4] create /Renne "liuxinrong"(引號里面是內容-字符串)
Created /Renne
[zk: localhost:2181(CONNECTED) 5] ls /
[Renne, zookeeper]
[zk: localhost:2181(CONNECTED) 4] create /app1/server101 "192.168.1.101"
Created /app1/server101
6)獲得節點的值
[zk: localhost:2181(CONNECTED) 6] get /app1
hello app1
cZxid = 0x20000000a
ctime = Mon Jul 17 16:08:35 CST 2017
mZxid = 0x20000000a
mtime = Mon Jul 17 16:08:35 CST 2017
pZxid = 0x20000000b
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0(臨時節點)
dataLength = 10
numChildren = 1
[zk: localhost:2181(CONNECTED) 8] get /app1/server101
192.168.1.101
cZxid = 0x20000000b
ctime = Mon Jul 17 16:11:04 CST 2017
mZxid = 0x20000000b
mtime = Mon Jul 17 16:11:04 CST 2017
pZxid = 0x20000000b
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 13
numChildren = 0
7)創建短暫節點
[zk: localhost:2181(CONNECTED) 9] create -e /app-emphemeral 8888
(1)在當前客戶端是能查看到的
[zk: localhost:2181(CONNECTED) 10] ls /
[app1, app-emphemeral, zookeeper]
(2)退出當前客戶端然后再重啟客戶端
[zk: localhost:2181(CONNECTED) 12] quit
[itstar@bigdata113 zookeeper-3.4.10]$ bin/zkCli.sh
(3)再次查看根目錄下短暫節點已經刪除
[zk: localhost:2181(CONNECTED) 0] ls /
[app1, zookeeper]
8)創建帶序號的節點
(1)先創建一個普通的根節點app2
[zk: localhost:2181(CONNECTED) 11] create /app2 "app2"
(2)創建帶序號的節點
[zk: localhost:2181(CONNECTED) 13] create -s /app2/aa 888
Created /app2/aa0000000000
[zk: localhost:2181(CONNECTED) 14] create -s /app2/bb 888
Created /app2/bb0000000001
[zk: localhost:2181(CONNECTED) 15] create -s /app2/cc 888
Created /app2/cc0000000002
如果原節點下有1個節點,則再排序時從1開始,以此類推。
[zk: localhost:2181(CONNECTED) 16] create -s /app1/aa 888
Created /app1/aa0000000001
接着舉例:
[zk: localhost:2181(CONNECTED) 9] create -s /Renn00000000003/renn0 "liu0"
Created /Renn00000000003/renn00000000000
[zk:localhost:2181(CONNECTED)10]create -s /Renn00000000003/renn0000000000033 "liu1"
Created /Renn00000000003/renn00000000000330000000001
9)修改節點數據值
[zk: localhost:2181(CONNECTED) 2] set /app1 999
10)節點的值變化監聽--只監聽一次
(1)在104主機上注冊監聽/app1節點數據變化
[zk: localhost:2181(CONNECTED) 26] get /app1 watch
(2)在103主機上修改/app1節點的數據
[zk: localhost:2181(CONNECTED) 5] set /app1 777
(3)觀察104主機收到數據變化的監聽
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/app1
之后按回車就恢復回去了
11)節點的子節點變化監聽(路徑變化)
(1)在104主機上注冊監聽/app1節點的子節點變化
[zk: localhost:2181(CONNECTED) 1] ls /app1 watch
[aa0000000001, server101]
(2)在103主機/app1節點上創建子節點
[zk: localhost:2181(CONNECTED) 6] create /app1/bb 666
Created /app1/bb
(3)觀察104主機收到子節點變化的監聽
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/app1
12)刪除節點
[zk: localhost:2181(CONNECTED) 4] delete /app1/bb
13)遞歸刪除節點
[zk: localhost:2181(CONNECTED) 7] rmr /app2
14)查看節點狀態
[zk: localhost:2181(CONNECTED) 12] stat /app1
cZxid = 0x20000000a
ctime = Mon Jul 17 16:08:35 CST 2017
mZxid = 0x200000018
mtime = Mon Jul 17 16:54:38 CST 2017
pZxid = 0x20000001c
cversion = 4
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 2
