1、创建节点
create [-s] [-e] path data acl
-s 顺序节点; -e 临时节点;如果不指定,为持久节点;
1.1 创建顺序节点 --断开连接后节点不会消失
[zk: localhost:2183(CONNECTED) 4] ls /
[zookeeper]
[zk: localhost:2183(CONNECTED) 5] create -s /zk-test 123
Created /zk-test0000000000
[zk: localhost:2183(CONNECTED) 6] ls /
[zk-test0000000000, zookeeper]
1.2 创建临时节点 --断开连接后,节点会消失
[zk: localhost:2181(CONNECTED) 9] ls /
[zk-test0000000000, zookeeper]
[zk: localhost:2181(CONNECTED) 10] create -e /zk-temp 123
Created /zk-temp
[zk: localhost:2181(CONNECTED) 11] ls /
[zk-test0000000000, zookeeper, zk-temp]
[zk: localhost:2181(CONNECTED) 12] close
2019-12-14 16:17:06,567 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x16f033a788b0002 closed
[zk: localhost:2181(CLOSED) 13] 2019-12-14 16:17:06,567 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x16f033a788b0002
[zk: localhost:2181(CLOSED) 13] connect localhost:2181
2019-12-14 16:17:15,378 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@c2e1f26
[zk: localhost:2181(CONNECTING) 14] 2019-12-14 16:17:15,380 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2019-12-14 16:17:15,380 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@876] - Socket connection established to localhost/127.0.0.1:2181, initiating session
2019-12-14 16:17:15,384 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x16f033a788b0003, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 14] ls /
[zk-test0000000000, zookeeper]
1.3 持久节点
[zk: localhost:2181(CONNECTED) 15] create /zk-test 1234
Created /zk-test
[zk: localhost:2181(CONNECTED) 16] ls /
[zk-test, zk-test0000000000, zookeeper]
2、读取节点
ls/get/ls2 path [watch]
[zk: localhost:2181(CONNECTED) 17] ls /zk-test
[]
[zk: localhost:2181(CONNECTED) 18] get /zk-test
1234
cZxid = 0x10000000d
ctime = Sat Dec 14 16:20:32 CST 2019
mZxid = 0x10000000d
mtime = Sat Dec 14 16:20:32 CST 2019
pZxid = 0x10000000d
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
[zk: localhost:2181(CONNECTED) 19] ls2 /zk-test
[]
cZxid = 0x10000000d
ctime = Sat Dec 14 16:20:32 CST 2019
mZxid = 0x10000000d
mtime = Sat Dec 14 16:20:32 CST 2019
pZxid = 0x10000000d
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
3、更新节点
set path data [version]
[zk: localhost:2181(CONNECTED) 21] set /zk-test hellozkworld
cZxid = 0x10000000d
ctime = Sat Dec 14 16:20:32 CST 2019
mZxid = 0x10000000e
mtime = Sat Dec 14 16:25:20 CST 2019
pZxid = 0x10000000d
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 12
numChildren = 0
[zk: localhost:2181(CONNECTED) 22] get /zk-test
hellozkworld
cZxid = 0x10000000d
ctime = Sat Dec 14 16:20:32 CST 2019
mZxid = 0x10000000e
mtime = Sat Dec 14 16:25:20 CST 2019
pZxid = 0x10000000d
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 12
numChildren = 0
4、删除节点
delete path [version]
[zk: localhost:2181(CONNECTED) 23] delete /zk-test
[zk: localhost:2181(CONNECTED) 24] ls /
[zk-test0000000000, zookeeper]
[zk: localhost:2181(CONNECTED) 25] delete /zk-test0000000000
[zk: localhost:2181(CONNECTED) 26] ls /
[zookeeper]