准备工作
这里用单机模式启动Zookeeper.
打开/usr/local/zk/zkp01/conf/zoo.cfg文件进行编辑
注释掉server.2=xxx server.3=xxx 只保留 server.1=xxx
启动服务
sh zkServer.sh start (start启动,stop,停止,restart重启)
客户端连接
sh zkCli.sh -server 127.0.0.1:2181
连接成功之后出现 ,可以开始输入Zookeeper命令了
zookeeper.ZooKeeperMain$MyWatcher@22d8cfe0
Welcome to ZooKeeper!
2017-10-20 14:12:53,382 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@975] - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2017-10-20 14:12:53,449 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@852] - Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
2017-10-20 14:12:53,463 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1235] - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x15f3867b0c80001, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: 127.0.0.1:2181(CONNECTED) 0]
创建节点
create [-s] [-e] path data acl
其中,-s或-e分别指定节点特性,顺序或临时节点,若不指定,则表示持久节点;acl用来进行权限控制。
创建顺序节点
可以看到创建成功输出/zk-test0000000 一串数字表示节点的顺序
[zk: 127.0.0.1:2181(CONNECTED) 0] create -s /zk-test 123
Created /zk-test0000000000
创建临时节点
create -e /zk-temp 123
临时节点在客户端会话结束后,就会自动删除,使用quit命令退出客户端,在重新连接就会发现zk-temp没有了. 查看命令通过ls 可以查看
创建永久节点
create /zk-permanent 321
查看节点
查看某一个节点下面有哪些节点 (在第一次默认在 / 下有一个 叫 zookeeper的节点)
[zk: 127.0.0.1:2181(CONNECTED) 7] ls /
[zk-permanent, zk-test0000000000, zookeeper, zk-test0000000002, zk-test220000000001, zk-temp]
查看某一个节点的具体信息 get
[zk: 127.0.0.1:2181(CONNECTED) 14] get /zk-permanent
321
cZxid = 0x30000000a
ctime = Fri Oct 20 14:21:46 CST 2017
mZxid = 0x30000000b
mtime = Fri Oct 20 14:39:30 CST 2017
pZxid = 0x30000000a
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
ls2 命令来查看某个目录包含的所有文件,与ls不同的是它查看到time、version等信息
[zk: 127.0.0.1:2181(CONNECTED) 17] ls2 /
[zk-permanent, zk-test0000000000, zookeeper, zk-test0000000002, zk-test220000000001, zk-temp]
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x30000000a
cversion = 4
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 6
设置节点的值
data就是要更新的新内容,version表示数据版本
set path data [version]
先来看一下这个节点 dataversion = 1
[zk: 127.0.0.1:2181(CONNECTED) 18] get /zk-permanent
321
cZxid = 0x30000000a
ctime = Fri Oct 20 14:21:46 CST 2017
mZxid = 0x30000000b
mtime = Fri Oct 20 14:39:30 CST 2017
pZxid = 0x30000000a
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
更新数据之后 可以看到 dataversion 由1 变成了 2
[zk: 127.0.0.1:2181(CONNECTED) 19] set /zk-permanent 112233
cZxid = 0x30000000a
ctime = Fri Oct 20 14:21:46 CST 2017
mZxid = 0x30000000c
mtime = Fri Oct 20 14:43:52 CST 2017
pZxid = 0x30000000a
cversion = 0
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
删除节点
这里的version与set中的同样的意思,需要注意的是若删除节点存在子节点,那么无法删除该节点,必须先删除子节点,再删除父节点。
delete path [version]
退出客户端
[zk: 127.0.0.1:2181(CONNECTED) 34] quit
Quitting...
2017-10-20 14:49:02,676 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x15f3867b0c80001 closed
2017-10-20 14:49:02,677 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@512] - EventThread shut down