在啓動Zookeeper服務以後,輸入如下命令,鏈接到Zookeeper服務:java
[hadoop@hadoop1 ~]$ zkCli.sh -server hadoop2:2181
1 [hadoop@hadoop1 ~]$ zkCli.sh -server hadoop2:2181 2 Connecting to hadoop2:2181 3 2018-03-21 19:55:53,744 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT 4 2018-03-21 19:55:53,748 [myid:] - INFO [main:Environment@100] - Client environment:host.name=hadoop1 5 2018-03-21 19:55:53,749 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_73 6 2018-03-21 19:55:53,751 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation 7 2018-03-21 19:55:53,751 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/local/jdk1.8.0_73/jre 8 2018-03-21 19:55:53,751 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/home/hadoop/apps/zookeeper-3.4.10/bin/../build/classes:/home/hadoop/apps/zookeeper-3.4.10/bin/../build/lib/*.jar:/home/hadoop/apps/zookeeper-3.4.10/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/hadoop/apps/zookeeper-3.4.10/bin/../lib/slf4j-api-1.6.1.jar:/home/hadoop/apps/zookeeper-3.4.10/bin/../lib/netty-3.10.5.Final.jar:/home/hadoop/apps/zookeeper-3.4.10/bin/../lib/log4j-1.2.16.jar:/home/hadoop/apps/zookeeper-3.4.10/bin/../lib/jline-0.9.94.jar:/home/hadoop/apps/zookeeper-3.4.10/bin/../zookeeper-3.4.10.jar:/home/hadoop/apps/zookeeper-3.4.10/bin/../src/java/lib/*.jar:/home/hadoop/apps/zookeeper-3.4.10/bin/../conf::/usr/local/jdk1.8.0_73/lib:/usr/local/jdk1.8.0_73/jre/lib 9 2018-03-21 19:55:53,751 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 10 2018-03-21 19:55:53,751 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp 11 2018-03-21 19:55:53,751 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA> 12 2018-03-21 19:55:53,752 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux 13 2018-03-21 19:55:53,752 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64 14 2018-03-21 19:55:53,752 [myid:] - INFO [main:Environment@100] - Client environment:os.version=2.6.32-573.el6.x86_64 15 2018-03-21 19:55:53,752 [myid:] - INFO [main:Environment@100] - Client environment:user.name=hadoop 16 2018-03-21 19:55:53,752 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/home/hadoop 17 2018-03-21 19:55:53,752 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/home/hadoop 18 2018-03-21 19:55:53,755 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=hadoop2:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@5c29bfd 19 Welcome to ZooKeeper! 20 2018-03-21 19:55:53,789 [myid:] - INFO [main-SendThread(hadoop2:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server hadoop2/192.168.123.103:2181. Will not attempt to authenticate using SASL (unknown error) 21 JLine support is enabled 22 2018-03-21 19:55:53,931 [myid:] - INFO [main-SendThread(hadoop2:2181):ClientCnxn$SendThread@876] - Socket connection established to hadoop2/192.168.123.103:2181, initiating session 23 2018-03-21 19:55:53,977 [myid:] - INFO [main-SendThread(hadoop2:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server hadoop2/192.168.123.103:2181, sessionid = 0x262486284b70000, negotiated timeout = 30000 24 25 WATCHER:: 26 27 WatchedEvent state:SyncConnected type:None path:null 28 [zk: hadoop2:2181(CONNECTED) 0]
鏈接成功以後,系統會輸出Zookeeper的相關環境及配置信息,並在屏幕輸出「welcome to Zookeeper!」等信息。node
輸入help以後,屏幕會輸出可用的Zookeeper命令,以下圖所示shell
(1) 使用ls命令查看當前Zookeeper中所包含的內容:ls /apache
[zk: hadoop2:2181(CONNECTED) 1] ls / [zookeeper] [zk: hadoop2:2181(CONNECTED) 2]
(2) 建立一個新的Znode節點"aa",以及和它相關字符,執行命令:create /aa "my first zk",默認是不帶編號的api
[zk: hadoop2:2181(CONNECTED) 2] create /aa "my first zk" Created /aa [zk: hadoop2:2181(CONNECTED) 3]
建立帶編號的持久性節點"bb",session
[zk: localhost:2181(CONNECTED) 1] create -s /bb "bb" Created /bb0000000001 [zk: localhost:2181(CONNECTED) 2]
建立不帶編號的臨時節點"cc"app
[zk: localhost:2181(CONNECTED) 2] create -e /cc "cc" Created /cc [zk: localhost:2181(CONNECTED) 3]
建立帶編號的臨時節點"dd"socket
[zk: localhost:2181(CONNECTED) 3] create -s -e /dd "dd" Created /dd0000000003 [zk: localhost:2181(CONNECTED) 4]
(3) 再次使用ls命令來查看如今Zookeeper的中所包含的內容:ls /ide
[zk: localhost:2181(CONNECTED) 4] ls /
[cc, dd0000000003, zookeeper, bb0000000001]
[zk: localhost:2181(CONNECTED) 5] 工具
此時看到,aa節點已經被建立。
關閉本次鏈接回話session,再從新打開一個鏈接
[zk: localhost:2181(CONNECTED) 5] close 2018-03-22 13:03:29,137 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x1624c10e8d90000 closed [zk: localhost:2181(CLOSED) 6] 2018-03-22 13:03:29,139 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x1624c10e8d90000 [zk: localhost:2181(CLOSED) 6] ls / Not connected [zk: localhost:2181(CLOSED) 7] connect hadoop1:2181
從新查看,臨時節點已經隨着上一次的會話關閉自動刪除了
[zk: hadoop1:2181(CONNECTED) 8] ls / [zookeeper, bb0000000001] [zk: hadoop1:2181(CONNECTED) 9]
(4) 使用get命令來確認第二步中所建立的Znode是否包含咱們建立的字符串,執行命令:get /aa
[zk: hadoop2:2181(CONNECTED) 4] get /aa my first zk cZxid = 0x100000002 ctime = Wed Mar 21 20:01:02 CST 2018 mZxid = 0x100000002 mtime = Wed Mar 21 20:01:02 CST 2018 pZxid = 0x100000002 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 11 numChildren = 0 [zk: hadoop2:2181(CONNECTED) 5]
(5) 接下來經過set命令來對zk所關聯的字符串進行設置,執行命令:set /aa haha123
[zk: hadoop2:2181(CONNECTED) 6] set /aa haha123 cZxid = 0x100000002 ctime = Wed Mar 21 20:01:02 CST 2018 mZxid = 0x100000004 mtime = Wed Mar 21 20:04:10 CST 2018 pZxid = 0x100000002 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 7 numChildren = 0 [zk: hadoop2:2181(CONNECTED) 7]
(6) 再次使用get命令來查看,上次修改的內容,執行命令:get /aa
[zk: hadoop2:2181(CONNECTED) 7] get /aa haha123 cZxid = 0x100000002 ctime = Wed Mar 21 20:01:02 CST 2018 mZxid = 0x100000004 mtime = Wed Mar 21 20:04:10 CST 2018 pZxid = 0x100000002 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 7 numChildren = 0 [zk: hadoop2:2181(CONNECTED) 8]
(7) 下面咱們將剛纔建立的Znode刪除,執行命令:delete /aa
[zk: hadoop2:2181(CONNECTED) 8] delete /aa [zk: hadoop2:2181(CONNECTED) 9]
(8) 最後再次使用ls命令查看Zookeeper中的內容,執行命令:ls /
[zk: hadoop2:2181(CONNECTED) 9] ls / [zookeeper] [zk: hadoop2:2181(CONNECTED) 10]
通過驗證,zk節點已經刪除。
(9) 退出,執行命令:quit
[zk: hadoop2:2181(CONNECTED) 10] quit Quitting... 2018-03-21 20:07:11,133 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x262486284b70000 closed 2018-03-21 20:07:11,139 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x262486284b70000 [hadoop@hadoop1 ~]$
查看一個文件的狀態信息
[zk: localhost:2181(CONNECTED) 1] stat /a cZxid = 0x200000009 ctime = Thu Mar 22 13:07:19 CST 2018 mZxid = 0x200000009 mtime = Thu Mar 22 13:07:19 CST 2018 pZxid = 0x200000009 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 1 numChildren = 0 [zk: localhost:2181(CONNECTED) 2]
詳細解釋:
zxid: 一個事務編號,zookeeper集羣內部的全部事務,都有一個全局的惟一的順序的編號
它由兩部分組成: 就是一個 64位的長整型 long
高32位: 用來標識leader關係是否改變,如 0x2
低32位: 用來作當前這個leader領導期間的全局的遞增的事務編號,如 00000009
狀態屬性 | 說明 |
cZxid | 數據節點建立時的事務ID |
ctime | 數據節點建立時的時間 |
mZxid | 數據節點最後一次更新時的事務ID |
mtime | 數據節點最後一次更新時的時間 |
pZxid | 數據節點的子節點列表最後一次被修改(是子節點列表變動,而不是子節點內容變動)時的事務ID |
cversion | 子節點的版本號 |
dataVersion | 數據節點的版本號 |
aclVersion | 數據節點的ACL版本號 |
ephemeralOwner | 若是節點是臨時節點,則表示建立該節點的會話的SessionID;若是節點是持久節點,則該屬性值爲0 |
dataLength | 數據內容的長度 |
numChildren | 數據節點當前的子節點個數 |
(1)修改節點a的數據,mZxid、dataVersion、dataLength 存儲信息發生變化
[zk: localhost:2181(CONNECTED) 2] set /a 'aaa' cZxid = 0x200000009 ctime = Thu Mar 22 13:07:19 CST 2018 mZxid = 0x20000000a mtime = Thu Mar 22 13:12:53 CST 2018 pZxid = 0x200000009 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 3 numChildren = 0 [zk: localhost:2181(CONNECTED) 3]
(2)建立新的節點b,狀態信息都會發生變化,zxid的事物ID也會增長
[zk: localhost:2181(CONNECTED) 5] stat /b cZxid = 0x20000000b ctime = Thu Mar 22 13:15:56 CST 2018 mZxid = 0x20000000b mtime = Thu Mar 22 13:15:56 CST 2018 pZxid = 0x20000000b cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 1 numChildren = 0 [zk: localhost:2181(CONNECTED) 6]
(3)在a節點下面新增節點c,pZxid、cversion、numChildren 發生改變
[zk: localhost:2181(CONNECTED) 6] create /a/c 'c' Created /a/c [zk: localhost:2181(CONNECTED) 7] stat /a cZxid = 0x200000009 ctime = Thu Mar 22 13:07:19 CST 2018 mZxid = 0x20000000a mtime = Thu Mar 22 13:12:53 CST 2018 pZxid = 0x20000000c cversion = 1 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 3 numChildren = 1 [zk: localhost:2181(CONNECTED) 8]
(4)ephemeralOwner 持久性的節點信息是0x0臨時的幾點信息是本次會話的sessionid,如圖
(5) 將leader幹掉,此時第二臺機器成爲leader,從新建立一個文件y,此時發現czxid的前3位和以前發生變化,說明換了leader
[zk: localhost:2181(CONNECTED) 0] create /y 'yy' Created /y [zk: localhost:2181(CONNECTED) 2] stat /y cZxid = 0x300000003 ctime = Thu Mar 22 13:25:51 CST 2018 mZxid = 0x300000003 mtime = Thu Mar 22 13:25:51 CST 2018 pZxid = 0x300000003 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 2 numChildren = 0 [zk: localhost:2181(CONNECTED) 3]