1.连接成功之后界面

[root@localhost zookeeper-3.4.10]# ./bin/zkCli.sh -server 127.0.0.1:2181
........................................................................
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: 127.0.0.1:2181(CONNECTED) 0] 

2.zookeeper客户端命令

[zk: 127.0.0.1:2181(CONNECTED) 0]
后面执行下面的命令

2.1.help命令

查看所有的zookeeper客户端命令

[zk: 127.0.0.1:2181(CONNECTED) 0] 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
[zk: 127.0.0.1:2181(CONNECTED) 1] 

2.2.close命令

[zk: 127.0.0.1:2181(CONNECTED) 1] close
2018-06-08 19:10:43,873 [myid:] - INFO  [main:ZooKeeper@684] - Session: 0x163e23d55f20002 closed
[zk: 127.0.0.1:2181(CLOSED) 2] 2018-06-08 19:10:43,874 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x163e23d55f20002

[zk: 127.0.0.1:2181(CLOSED) 2] 

close命令执行之后,CONNECTED状态变为CLOSED

2.3.connect命令

connect 127.0.0.1:2181

[zk: 127.0.0.1:2181(CLOSED) 2] connect 127.0.0.1:2181
2018-06-08 19:12:44,683 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@4cdf35a9
[zk: 127.0.0.1:2181(CONNECTING) 3] 2018-06-08 19:12:44,685 [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)
2018-06-08 19:12:44,687 [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
2018-06-08 19:12:44,711 [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 = 0x163e23d55f20003, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null

[zk: 127.0.0.1:2181(CONNECTED) 3] 

其中客户端连接状态CLOSED关闭状态变为CONNECTED连接成功状态;

2.4.get命令

get命令用于获取节点的信息,注意节点的路径必须是以/开头的绝对路径。如get /

[zk: 127.0.0.1:2181(CONNECTED) 3] get /

cZxid = 0x0
ctime = Wed Dec 31 16:00:00 PST 1969
mZxid = 0x0
mtime = Wed Dec 31 16:00:00 PST 1969
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1
[zk: 127.0.0.1:2181(CONNECTED) 4] 

节点解释

其中:

cZxid:节点创建时的zxid

ctime:节点创建时间

mZxid:节点最近一次更新时的zxid

mtime:节点最近一次更新的时间

cversion:子节点数据更新次数

dataVersion:本节点数据更新次数

aclVersion:节点ACL(授权信息)的更新次数

ephemeralOwner:如果该节点为临时节点,ephemeralOwner值表示与该节点绑定的session id. 如果该节点不是临时节点,ephemeralOwner值为0

dataLength:节点数据长度,本例中为hello world的长度

numChildren:子节点个数
[zk: 127.0.0.1:2181(CONNECTED) 10] create /node_1 123
Created /node_1
[zk: 127.0.0.1:2181(CONNECTED) 11] ls /
[zookeeper, node_1]
[zk: 127.0.0.1:2181(CONNECTED) 12] get node_1
Command failed: java.lang.IllegalArgumentException: Path must start with / character
[zk: 127.0.0.1:2181(CONNECTED) 13] get /node_1
123
cZxid = 0xd
ctime = Fri Jun 08 19:31:36 PDT 2018
mZxid = 0xd
mtime = Fri Jun 08 19:31:36 PDT 2018
pZxid = 0xd
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 14] 

2.4.stat命令

stat命令用于查看节点的状态信息,如stat /

[zk: 127.0.0.1:2181(CONNECTED) 4] stat /
cZxid = 0x0
ctime = Wed Dec 31 16:00:00 PST 1969
mZxid = 0x0
mtime = Wed Dec 31 16:00:00 PST 1969
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1
[zk: 127.0.0.1:2181(CONNECTED) 5] 

该命令的结果参数说明同get命令

2.5.set命令

set命令用于设置节点的数据,如:

[zk: 127.0.0.1:2181(CONNECTED) 8] set /usergrid hellUsergrid

2.6.ls命令

ls命令用于获取路径下的节点信息,注意路径为绝对路径,如:ls /storm

[assignments, storms, errors, supervisors, workerbeats]

[zk: 127.0.0.1:2181(CONNECTED) 9] ls /
[zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 10] 
如上:列出根目录节点下的所有文件,目前就只有一个系统自带的zookeeper节点

[zk: 127.0.0.1:2181(CONNECTED) 10] create /node_1 123
Created /node_1
[zk: 127.0.0.1:2181(CONNECTED) 11] ls /
[zookeeper, node_1]
[zk: 127.0.0.1:2181(CONNECTED) 12] 
node1 已经创建

2.7.ls2命令

ls2命令是ls命令的增强版,比ls命令多输出本节点信息,如:ls2 /storm

[assignments, storms, errors, supervisors, workerbeats]
cZxid = 0x1469
ctime = Tue Nov 14 11:32:09 CST 2017
mZxid = 0x1469
mtime = Tue Nov 14 11:32:09 CST 2017
pZxid = 0x1470
cversion = 5
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 1
numChildren = 5
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐