一、Zookeeper的四字命令 Zookeeper支持某些特定的四字命令字母與其的交互。他們大多數是查詢命令,用來獲取Zookeeper服務的當前狀態及相關信息。用戶在客戶端可以通過telnet或nc向Zookeeper提交相應的命令。Zookeeper常用的四字命令見下圖所示。 上圖,是Zook ...
一、Zookeeper的四字命令
Zookeeper支持某些特定的四字命令字母與其的交互。他們大多數是查詢命令,用來獲取Zookeeper服務的當前狀態及相關信息。用戶在客戶端可以通過telnet或nc向Zookeeper提交相應的命令。Zookeeper常用的四字命令見下圖所示。
上圖,是Zookeeper四字命令的一個簡單用例。
[root@hadoop ~]# echo ruok|nc localhost 2181
[root@hadoop ~]# zkServer.sh start zoo1.cfg
JMX enabled by default
Using config: /usr/local/zk/bin/../conf/zoo1.cfg
Starting zookeeper ... STARTED
[root@hadoop ~]# zkServer.sh start zoo2.cfg
JMX enabled by default
Using config: /usr/local/zk/bin/../conf/zoo2.cfg
Starting zookeeper ... STARTED
[root@hadoop ~]# zkServer.sh start zoo3.cfg
JMX enabled by default
Using config: /usr/local/zk/bin/../conf/zoo3.cfg
Starting zookeeper ... STARTED
[root@hadoop ~]# echo ruok|nc localhost 2181
imok[root@hadoop ~]# echo ruok|nc localhost 2182
imok[root@hadoop ~]# echo ruok|nc localhost 2183
imok[root@hadoop ~]# echo conf|nc localhost 2181
clientPort=2181
dataDir=/usr/local/zk/data_1/version-2
dataLogDir=/usr/local/zk/logs_1/version-2
tickTime=2000
maxClientCnxns=60
minSessionTimeout=4000
maxSessionTimeout=40000
serverId=0
initLimit=10
syncLimit=5
electionAlg=3
electionPort=3387
quorumPort=2287
peerType=0
[root@hadoop ~]#
二、Zookeeper的簡單操作
2.1 Zookeeper的shell操作
2.1.1 Zookeeper命令工具
再啟動Zookeeper服務之後,輸入以下命令,連接到Zookeeper服務:
zkCli.sh -server localhost:2181
執行結果如下所示:
[root@hadoop ~]# zkCli.sh -server localhost:2181
Connecting to localhost:2181
2014-10-17 03:35:51,051 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT
2014-10-17 03:35:51,055 [myid:] - INFO [main:Environment@100] - Client environment:host.name=hadoop
2014-10-17 03:35:51,057 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.6.0_24
2014-10-17 03:35:51,057 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Sun Microsystems Inc.
2014-10-17 03:35:51,066 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/local/jdk/jre
2014-10-17 03:35:51,079 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/usr/local/zk/bin/../build/classes:/usr/local/zk/bin/../build/lib/*.jar:/usr/local/zk/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zk/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zk/bin/../lib/netty-3.2.2.Final.jar:/usr/local/zk/bin/../lib/log4j-1.2.15.jar:/usr/local/zk/bin/../lib/jline-0.9.94.jar:/usr/local/zk/bin/../zookeeper-3.4.5.jar:/usr/local/zk/bin/../src/java/lib/*.jar:/usr/local/zk/bin/../conf:
2014-10-17 03:35:51,083 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/local/jdk/jre/lib/i386/client:/usr/local/jdk/jre/lib/i386:/usr/local/jdk/jre/../lib/i386:/usr/java/packages/lib/i386:/lib:/usr/lib
2014-10-17 03:35:51,084 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2014-10-17 03:35:51,086 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2014-10-17 03:35:51,099 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2014-10-17 03:35:51,100 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=i386
2014-10-17 03:35:51,101 [myid:] - INFO [main:Environment@100] - Client environment:os.version=2.6.32-358.el6.i686
2014-10-17 03:35:51,101 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2014-10-17 03:35:51,102 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2014-10-17 03:35:51,106 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/root
2014-10-17 03:35:51,120 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@b02e7a
Welcome to ZooKeeper!
JLine support is enabled
2014-10-17 03:35:51,233 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@966] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (Unable to locate a login configuration)
2014-10-17 03:35:51,247 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@849] - Socket connection established to localhost/127.0.0.1:2181, initiating session
[zk: localhost:2181(CONNECTING) 0] 2014-10-17 03:35:51,290 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1207] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x491da0e20b0000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
連接成功之後,系統會輸出Zookeeper的相關環境及配置信息,併在屏幕輸出“welcome to Zookeeper!”等信息。輸入help之後,屏幕會輸出可用的Zookeeper命令,如下圖所示
2.1.2 使用Zookeeper命令的簡單操作步驟
(1) 使用ls命令查看當前Zookeeper中所包含的內容:ls /
[zk: localhost:2181(CONNECTED) 1] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 2]
(2) 創建一個新的Znode節點"zk",以及和它相關字元,執行命令:create /zk myData
[zk: localhost:2181(CONNECTED) 2] create /zk myData
Created /zk
(3) 再次使用ls命令來查看現在Zookeeper的中所包含的內容:ls /
[zk: localhost:2181(CONNECTED) 3] ls /
[zk, zookeeper]
此時看到,zk節點已經被創建。
(4) 使用get命令來確認第二步中所創建的Znode是否包含我們創建的字元串,執行命令:get /zk
[zk: localhost:2181(CONNECTED) 4] get /zk
myData
cZxid = 0x500000006
ctime = Fri Oct 17 03:54:20 PDT 2014
mZxid = 0x500000006
mtime = Fri Oct 17 03:54:20 PDT 2014
pZxid = 0x500000006
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
(5) 接下來通過set命令來對zk所關聯的字元串進行設置,執行命令:set /zk jiang1234
[zk: localhost:2181(CONNECTED) 5] set /zk jiang2014
cZxid = 0x500000006
ctime = Fri Oct 17 03:54:20 PDT 2014
mZxid = 0x500000007
mtime = Fri Oct 17 03:55:50 PDT 2014
pZxid = 0x500000006
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 0
(6) 再次使用get命令來查看,上次修改的內容,執行命令:get /zk
[zk: localhost:2181(CONNECTED) 6] get /zk
jiang2014
cZxid = 0x500000006
ctime = Fri Oct 17 03:54:20 PDT 2014
mZxid = 0x500000007
mtime = Fri Oct 17 03:55:50 PDT 2014
pZxid = 0x500000006
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 0
(7) 下麵我們將剛纔創建的Znode刪除,執行命令:delete /zk
[zk: localhost:2181(CONNECTED) 7] delete /zk
(8) 最後再次使用ls命令查看Zookeeper中的內容,執行命令:ls /
[zk: localhost:2181(CONNECTED) 8] ls /
[zookeeper]
經過驗證,zk節點已經刪除。
2.2 Zookeeper的api的簡單使用
2.2.1 ZookeeperAPI簡介
Zookeeper API共包含五個包,分別為:
(1)org.apache.zookeeper
(2)org.apache.zookeeper.data
(3)org.apache.zookeeper.server
(4)org.apache.zookeeper.server.quorum
(5)org.apache.zookeeper.server.upgrade
其中org.apache.zookeeper,包含Zookeeper類,他是我們編程時 最常用的類文件。這個類是Zookeeper客戶端的主要類文件。如果要使用Zookeeper服務,應用程式首先必須創建一個Zookeeper實例, 這時就需要使用此類。一旦客戶端和Zookeeper服務建立起了連接,Zookeeper系統將會給次連接會話分配一個ID值,並且客戶端將會周期性的 向伺服器端發送心跳來維持會話連接。只要連接有效,客戶端就可以使用Zookeeper API來做相應處理了。
Zookeeper類提供瞭如下圖所示的幾類主要方法