zookeeper常用命令
进入 zookeeper的bin目录,
1. 启动服务:./zkServer.sh start , ./zkServer.sh restart 先关闭服务再启动服务
2.启动客户端 ./zkCli.sh
3.ls 显示当前节点的子节点
[zk: localhost:2181(CONNECTED) 1] ls /
[zookeeper]
ls2 显示状态目录
[zk: localhost:2181(CONNECTED) 4] ls2 /
[zookeeper]
cZxid = 0x0 ##cZxid 创建的当前节点
ctime = Thu Jan 01 08:00:00 CST 1970 ##ctime 创建时间
mZxid = 0x0 ##mZxid 修改后的节点
mtime = Thu Jan 01 08:00:00 CST 1970 ##mtime 修改时间 若没有修改,修改时间和创建时间一模一样
pZxid = 0x0 ##pZxid 子节点ID
cversion = -1 ##cversion 子节点版本号
dataVersion = 0 ##dataVersion 当前节点的版本号,修改一次加1
aclVersion = 0 ##aclVersion 权限控制 一般是累计加1
ephemeralOwner = 0x0 ##ephemeralOwner 0x0表示持久节点,如果是0x10老长一段字符就是临时节点
dataLength = 0 ##dataLength 数据长度
numChildren = 1 ##numChildren 表示子节点有多少个
4.get stat
stat / 显示节点状态
'ls /'+ 'stat /'= 'ls2 /'
get /获取当前节点内容
[zk: localhost:2181(CONNECTED) 5] stat / 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
[zk: localhost:2181(CONNECTED) 1] get / ##当前内容为空 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
5.create 创建节点
create[-s] [-e] path data acl -e 创建临时节点 断开连接一段时间后,自动清空节点数据
[zk: localhost:2181(CONNECTED) 1] create /boss theCat Created /boss [zk: localhost:2181(CONNECTED) 3] ls2 / [boss, zookeeper] cZxid = 0x0 ctime = Thu Jan 01 08:00:00 CST 1970 mZxid = 0x0 mtime = Thu Jan 01 08:00:00 CST 1970 pZxid = 0x6 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 2
[zk: localhost:2181(CONNECTED) 4] get /boss theCat cZxid = 0x6 ctime = Thu Dec 27 20:32:10 CST 2018 mZxid = 0x6 mtime = Thu Dec 27 20:32:10 CST 2018 pZxid = 0x6 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 6 numChildren = 0
-s 创建顺序节点
6. set delete
set path data [version] set时使用了乐观锁,先获取version,修改成功后,version+=1;如果获取的version小于修改节点version则报错,修改失败
delete path [version] verseion与set一样,可加也可不加。
这里经常会出现 删除数据失败,因为路径下 不为空。这个情况,我们使用rmr /path 删除