一、Zookeeper常用命令

当前安装目录

image-20201217222953208

启动服务端

[root@730bf4a97d79 bin]# ./zkServer.sh start

连接本地服务端

[root@730bf4a97d79 bin]# ./zkCli.sh
Connecting to localhost:2181
Welcome to ZooKeeper!

连接远程服务端a

[root@730bf4a97d79 bin]# ./zkCli.sh -server 172.25.0.11:2181
Connecting to 172.25.0.11:2181
Welcome to ZooKeeper!

输出所有支持的命令

[zk: 172.25.0.11:2181(CONNECTED) 5] help
ZooKeeper -server host:port -client-configuration properties-file cmd args
        addWatch [-m mode] path # optional mode is one of [PERSISTENT, PERSISTENT_RECURSIVE] - default is PERSISTENT_RECURSIVE
        addauth scheme auth
        close 
        config [-c] [-w] [-s]
        connect host:port
        create [-s] [-e] [-c] [-t ttl] path [data] [acl]
        delete [-v version] path
        deleteall path [-b batch size]
        delquota [-n|-b] path
        get [-s] [-w] path
        getAcl [-s] path
        getAllChildrenNumber path
        getEphemerals path
        history 
        listquota path
        ls [-s] [-w] [-R] path
        printwatches on|off
        quit 
        reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*]
        redo cmdno
        removewatches path [-c|-d|-a] [-l]
        set [-s] [-v version] path data
        setAcl [-s] [-v version] [-R] path acl
        setquota -n|-b val path
        stat [-w] path
        sync path
        version 

查看指定路径下包含的节点

[zk: 172.25.0.11:2181(CONNECTED) 2] ls /
[zookeeper]
[zk: 172.25.0.11:2181(CONNECTED) 3] ls /zookeeper 
[aaa, quota]

创建一个节点为node1内容为data1

[zk: 172.25.0.11:2181(CONNECTED) 7] create /node1 data1
Created /node1

获取指定路径下节点的信息

[zk: 172.25.0.11:2181(CONNECTED) 8] get /node1
data1

设置节点的内容

[zk: 172.25.0.11:2181(CONNECTED) 9] set /node1 "set-data"
[zk: 172.25.0.11:2181(CONNECTED) 10] get /node1 
set-data
[zk: 172.25.0.11:2181(CONNECTED) 11] 

删除一个节点

[zk: 172.25.0.11:2181(CONNECTED) 11] delete /node1
[zk: 172.25.0.11:2181(CONNECTED) 12] get /node1
org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /node1
[zk: 172.25.0.11:2181(CONNECTED) 13]