客户端连接
zkServer.sh start 启动zookeeper服务,运行 zkCli.sh –server ip 进入命令行工具
连接localhost的zookeeper的shell窗口
zkCli.sh -server localhost
连接hadoop01的zookeeper的shell窗口
zkCli.sh -server hadoop01
输入help,输出zk shell提示:
[zk: localhost:2181(CONNECTED) 7] help
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
shell基本操作
创建节点
create [-s] [-e] path data acl
其中,-s或-e分别指定节点特性,顺序或临时节点,若不指定,则表示持久节点;acl用来进行权限控制。
创建顺序节点:
[zk: hadoop01(CONNECTED) 0] create -s /test 123
Created /test0000000000
创建临时节点:
[zk: hadoop01(CONNECTED) 1] create -e /test-temp 123temp
Created /test-temp
创建永久节点:
[zk: hadoop01(CONNECTED) 2] create /test-p 123p
Created /test-p
读取节点
与读取相关的命令有ls 命令和get 命令,ls命令可以列出Zookeeper指定节点下的所有子节点,只能查看指定节点下的第一级的所有子节点;get命令可以获取Zookeeper指定节点的数据内容和属性信息。
ls path [watch]
get path [watch]
ls2 path [watch]
[zk: hadoop01(CONNECTED) 3] ls /
[test0000000000, test-p, zookeeper, test-temp]
[zk: hadoop01(CONNECTED) 4] get /
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x200000004
cversion = 2
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 4
[zk: hadoop01(CONNECTED) 5] ls2 /
[test0000000000, test-p, zookeeper, test-temp]
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x200000004
cversion = 2
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 4
[zk: hadoop01(CONNECTED) 6]
更新节点
set path data [version]
data就是要更新的新内容,version表示数据版本。
[zk: hadoop01(CONNECTED) 6] set /test-p 123pset 0
cZxid = 0x200000004
ctime = Mon Dec 09 15:49:52 CST 2019
mZxid = 0x200000005
mtime = Mon Dec 09 15:56:50 CST 2019
pZxid = 0x200000004
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0
现在dataVersion已经变为1了,表示进行了更新。
删除节点
delete path [version]
若删除节点存在子节点,那么无法删除该节点,必须先删除子节点,再删除父节点。
Rmr path
可以递归删除节点。
quota
setquota -n|-b val path 对节点增加限制。
n:表示子节点的最大个数
b:表示数据值的最大长度
val:子节点最大个数或数据值的最大长度
path:节点路径
[zk: localhost:2181(CONNECTED) 2] create -e /quota quota
Created /quota
[zk: localhost:2181(CONNECTED) 3] setquota -n 2 /quota
Comment: the parts are option -n val 2 path /quota
listquota path 列出指定节点的quota
[zk: localhost:2181(CONNECTED) 4] listquota /quota
absolute path is /zookeeper/quota/quota/zookeeper_limits
Output quota for /quota count=2,bytes=-1
Output stat for /quota count=1,bytes=5
子节点个数为2,数据长度-1表示没限制
delquota [-n | -b] path 删除quota
其它命令
history:列出历史命令
[zk: localhost:2181(CONNECTED) 5] history
0 - create -e /qupta quota
1 - setquota -n 2 /quota
2 - create -e /quota quota
3 - setquota -n 2 /quota
4 - listquota /quota
5 - history
redo:该命令可以重新执行指定命令编号的历史命令,命令编号可以通过history查看。
[zk: localhost:2181(CONNECTED) 6] redo 5
0 - create -e /qupta quota
1 - setquota -n 2 /quota
2 - create -e /quota quota
3 - setquota -n 2 /quota
4 - listquota /quota
5 - history
6 - history
来源:CSDN
作者:大数据AIRDE
链接:https://blog.csdn.net/weixin_42072754/article/details/103458318