欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

zookeeper的shell操作

程序员文章站 2023-11-18 21:14:40
一、启动zookeeper集群 进入zookeeper安装目录下的bin目录内,通过命令启动 通过命令查看节点状态 通过命令关闭 二、连接zookeeper服务 启动zookeeper服务之后,输入以下命令,连接到Zookeeper服务,注意:如果没有配置环境变量在zookeeper的安装目录下的b ......

一、启动zookeeper集群

  进入zookeeper安装目录下的bin目录内,通过命令启动

./zkServer.sh start

  通过命令查看节点状态

./zkServer.sh status

  通过命令关闭

./zkServer.sh stop

 

 

二、连接zookeeper服务

  启动zookeeper服务之后,输入以下命令,连接到Zookeeper服务,注意:如果没有配置环境变量在zookeeper的安装目录下的bin目录下执行

./zkCli.sh -server localhost:2181

 

  连接成功后显示:

zookeeper的shell操作

  到这里后我们就很开心了,接下来就可以进行操作了,下面介绍一些zookeeper的shell基本操作

三、 shell基本操作 

  •   查看节点:ls path [watch]  或   get path [watch]   或   ls2 path [watch]

ls 命令可以列出 Zookeeper 指定节点下的所有子节点,只能查看指定节点下的第一级的所有子节点;

get 命令可以获取 Zookeeper 指定节点的数据内容和属性信息

ls2命令可以列出 Zookeeper 指定节点下的所有子节点及属性信息

[zk: localhost:2181(CONNECTED) 13] get /                      

cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x800000008
cversion = 8
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 2
[zk: localhost:2181(CONNECTED) 14] ls /
[zookeeper, test0000000004]
[zk: localhost:2181(CONNECTED) 15] ls2 /
[zookeeper, test0000000004]
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x800000008
cversion = 8
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 2

 

 

  •   创建节点:create [-s] [-e] path data acl

      其中,-s 或-e 分别指定节点特性,顺序或临时节点,若不指定,则表示持久节点;acl 用来进行权限控制。

    •   创建顺序节点:

  

[zk: localhost:2181(CONNECTED) 8] create -s /test 123
Created /test0000000004
[zk: localhost:2181(CONNECTED) 9] 

 

    •  创建临时节点:

 

[zk: localhost:2181(CONNECTED) 16] create -e /test-tmp 123tmp
Created /test-tmp

 

    •  创建永久节点:    
[zk: localhost:2181(CONNECTED) 17] create /test-p 123p
Created /test-p

 

  •   查看节点内容:get path [watch]
[zk: localhost:2181(CONNECTED) 21] get /test-p
123p
cZxid = 0x80000000a
ctime = Wed Jul 04 23:57:01 CST 2018
mZxid = 0x80000000a
mtime = Wed Jul 04 23:57:01 CST 2018
pZxid = 0x80000000a
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0

 

  •   更改节点信息:set path data [version]

    data 就是要更新的新内容,version 表示数据版本。

[zk: localhost:2181(CONNECTED) 22] set /test-p 123456
cZxid = 0x80000000a
ctime = Wed Jul 04 23:57:01 CST 2018
mZxid = 0x80000000b
mtime = Thu Jul 05 00:01:59 CST 2018
pZxid = 0x80000000a
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
[zk: localhost:2181(CONNECTED) 23] get /test-p       
123456
cZxid = 0x80000000a
ctime = Wed Jul 04 23:57:01 CST 2018
mZxid = 0x80000000b
mtime = Thu Jul 05 00:01:59 CST 2018
pZxid = 0x80000000a
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0

 

  •   删除节点:delete path [version]  若删除节点存在子节点,那么无法删除该节点,必须先删除子节点,再删除父节点。

          Rmr path   递归删除节点。

 

[zk: localhost:2181(CONNECTED) 0] ls /
[test-p, zookeeper, test0000000004]
[zk: localhost:2181(CONNECTED) 1] delete /test0000000004
[zk: localhost:2181(CONNECTED) 2] ls /
[test-p, zookeeper]
[zk: localhost:2181(CONNECTED) 9] ls /            
[test-p, zookeeper, test]
[zk: localhost:2181(CONNECTED) 10] rmr /test       
[zk: localhost:2181(CONNECTED) 11] ls /     
[test-p, zookeeper]

 

  •   quota

    setquota -n|-b val path 对节点增加限制 

n:表示子节点的最大个数
b:表示数据值的最大长度
val:子节点最大个数或数据值的最大长度
path:节点路径

[zk: localhost:2181(CONNECTED) 13] setquota -n 2 /test-p
Comment: the parts are option -n val 2 path /test-p

 

    listquota path 列出指定节点的 quota

[zk: localhost:2181(CONNECTED) 14] listquota /test-p    
absolute path is /zookeeper/quota/test-p/zookeeper_limits
Output quota for /test-p count=2,bytes=-1
Output stat for /test-p count=1,bytes=6
子节点个数为 2,数据长度-1 表示没限制

    delquota [-n|-b] path 删除 quota

[zk: localhost:2181(CONNECTED) 15] delquota /test-p
[zk: localhost:2181(CONNECTED) 16] listquota /test-p
absolute path is /zookeeper/quota/test-p/zookeeper_limits
quota for /test-p does not exist.

 

  •   其他命令
    •  history : 列出命令历史
[zk: localhost:2181(CONNECTED) 17] history
7 - ls /
8 - create /test 111
9 - ls /
10 - rmr /test
11 - ls /
12 - listquota /test-p
13 - setquota -n 2 /test-p
14 - listquota /test-p
15 - delquota /test-p
16 - listquota /test-p
17 - history
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

 

    •   redo:该命令可以重新执行指定命令编号的历史命令,命令编号可以通过history 查看