ZooKeeper客戶端 zkCli.sh 節點的增刪改查

  • zkCli.sh

在 bin 目錄下的  zkCli.sh  就是ZooKeeper客戶端java

./zkCli.sh -timeout 5000  -server 127.0.0.1:2181   客戶端與ZooKeeper創建連接node

timeout:超時時間,單位毫秒數據庫

r:只讀模式,當節點壞掉的時候,還能夠提供讀服務apache

示例:api

./zkCli.sh -timeout 5000 -server 127.0.0.1:2181session

 1 zhangliuningdeMacBook-Pro:bin sherry$ ./zkCli.sh -timeout 5000 -server 127.0.0.1:2181
 2 Connecting to 127.0.0.1:2181
 3 2016-08-27 15:07:04,036 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.8--1, built on 02/06/2016 03:18 GMT
 4 2016-08-27 15:07:04,039 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=192.168.0.105
 5 2016-08-27 15:07:04,039 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_71
 6 2016-08-27 15:07:04,041 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
 7 2016-08-27 15:07:04,041 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_71.jdk/Contents/Home/jre
 8 2016-08-27 15:07:04,042 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/Users/sherry/Applications/zk1/bin/../build/classes:/Users/sherry/Applications/zk1/bin/../build/lib/*.jar:/Users/sherry/Applications/zk1/bin/../lib/slf4j-log4j12-1.6.1.jar:/Users/sherry/Applications/zk1/bin/../lib/slf4j-api-1.6.1.jar:/Users/sherry/Applications/zk1/bin/../lib/netty-3.7.0.Final.jar:/Users/sherry/Applications/zk1/bin/../lib/log4j-1.2.16.jar:/Users/sherry/Applications/zk1/bin/../lib/jline-0.9.94.jar:/Users/sherry/Applications/zk1/bin/../zookeeper-3.4.8.jar:/Users/sherry/Applications/zk1/bin/../src/java/lib/*.jar:/Users/sherry/Applications/zk1/bin/../conf:
 9 2016-08-27 15:07:04,042 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/Users/sherry/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
10 2016-08-27 15:07:04,042 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/var/folders/_n/d05bph5x5bl6k5p0pkx2g66m0000gn/T/
11 2016-08-27 15:07:04,042 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
12 2016-08-27 15:07:04,042 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Mac OS X
13 2016-08-27 15:07:04,042 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=x86_64
14 2016-08-27 15:07:04,042 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=10.11.5
15 2016-08-27 15:07:04,043 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=sherry
16 2016-08-27 15:07:04,043 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/Users/sherry
17 2016-08-27 15:07:04,043 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/Users/sherry/Applications/zk1/bin
18 2016-08-27 15:07:04,044 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=5000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@531d72ca
19 Welcome to ZooKeeper!
20 2016-08-27 15:07:04,081 [myid:] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
21 JLine support is enabled
22 2016-08-27 15:07:04,187 [myid:] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@876] - Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
23 [zk: 127.0.0.1:2181(CONNECTING) 0] 2016-08-27 15:07:04,245 [myid:] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x156cacb918b0000, negotiated timeout = 5000
24 
25 WATCHER::
26 
27 WatchedEvent state:SyncConnected type:None path:null

如上,表示咱們和ZooKeeper創建了鏈接,能夠經過命令與ZooKeeper進行交互了數據結構


 

h:幫助列表socket

h
ZooKeeper -server host:port cmd args
    stat path [watch]
    set path data [version]
    ls path [watch]
    delquota [-n|-b] path
    ls2 path [watch]
    setAcl path acl
    setquota -n|-b val path
    history 
    redo cmdno
    printwatches on|off
    delete path [version]
    sync path
    listquota path
    rmr path
    get path [watch]
    create [-s] [-e] path data acl
    addauth scheme auth
    quit 
    getAcl path
    close 
    connect host:port

顯示可以執行的命令分佈式

命令分類和數據庫類似,大概能夠分爲增刪改查四類ui

ZooKeeper數據結構:

大多數時候,咱們隊ZooKeeper的操做都是在對節點進行增刪改查

 

經常使用命令:

 

查詢相關指令

ls path:列出path下的文件

[zk: 127.0.0.1:2181(CONNECTED) 4] ls /
[zookeeper]

如上:列出根目錄節點下的全部文件,目前就只有一個系統自帶的zookeeper節點

 

stat path:查看節點狀態

[zk: 127.0.0.1:2181(CONNECTED) 6] stat /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
cZxid:建立節點時的事務id
pZxid:子節點列表最後一次被修改的事務id
cversion:節點版本號
dataCersion:數據版本號
aclVerson:acl權限版本號

......

get path:獲取指定節點的內容

ls2 path:列出path節點的子節點及狀態信息


建立指令

create [-s] [-e] path data acl

1 [zk: 127.0.0.1:2181(CONNECTED) 10] create /node_1 123
2 Created /node_1

如上:在根目錄建立了node_1節點,攜帶數據 123

使用 get /node_1 驗證是否添加節點及其數據成功

[zk: 127.0.0.1:2181(CONNECTED) 15] create -e /node_1/node_1_1 234
Created /node_1/node_1_1

建立了一個臨時節點(-e),而且攜帶數據234,使用stat命令查看這個新建的臨時節點

[zk: 127.0.0.1:2181(CONNECTED) 16] stat /node_1/node_1_1
cZxid = 0x200000003
ctime = Sat Aug 27 15:27:30 CST 2016
mZxid = 0x200000003
mtime = Sat Aug 27 15:27:30 CST 2016
pZxid = 0x200000003
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x156cacb918b0000
dataLength = 3
numChildren = 0
ephemeralOwner值再也不是0,表示這個臨時節點的版本號,若是是永久節點則其值爲 0x0
1 [zk: 127.0.0.1:2181(CONNECTED) 1] create -s /node_1/node_1_1 234
2 Created /node_1/node_1_10000000001

經過使用-s參數,建立一個順序節點,咱們雖然指定的節點名是node_1_1,可是實際上,名稱倒是 node_1_10000000001,若是咱們重複執行:

[zk: 127.0.0.1:2181(CONNECTED) 2] create -s /node_1/node_1_1 234
Created /node_1/node_1_10000000002

效果如上

 

這個特性,咱們能夠利用一下,生成在分佈式環境下的主鍵生成器

 

-s 和 -e 能夠同時使用

 



退出:
quit
[zk: 127.0.0.1:2181(CONNECTED) 19] quit
Quitting...
2016-08-27 15:29:44,760 [myid:] - INFO  [main:ZooKeeper@684] - Session: 0x156cacb918b0000 closed
2016-08-27 15:29:44,763 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x156cacb918b0000

這個時候若是從新登陸,執行 ls /node_1,發現其子節點是空的,建立的node_1_1子節點不見了,那是由於以前建立的時候就已經指定了,咱們建立的是一個臨時節點。當客戶端會話結束後,臨時節點是會被刪除了。

 

修改相關指定:

set path data [version]

 

[zk: 127.0.0.1:2181(CONNECTED) 14] set /node_1 998
cZxid = 0x200000002
ctime = Sat Aug 27 15:24:39 CST 2016
mZxid = 0x20000000a
mtime = Sat Aug 27 15:38:40 CST 2016
pZxid = 0x200000008
cversion = 5
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 3

若是咱們屢次修改,會發現  dataVersion ,也就是數據版本,在不停得發生變化(自增)

 

若是咱們在set的時候手動去指定了版本號,就必須和上一次查詢出來的結果一致,不然 就會報錯。

這個能夠用於咱們在修改節點數據的時候,保證咱們修改前數據沒被別人修改過。由於若是別人修改過了,咱們此次修改是不會成功的

 

 

刪除指令:

delete path [version]

刪除指定節點數據,其version參數的做用於set指定一致

 

delete /node_1/node_1_10000000001

整個節點全刪除

注意:delete只能刪除不包含子節點的節點,若是要刪除的節點包含子節點,使用rmr命令

 

rmr /node_1
相關文章
相關標籤/搜索