Zookeeper 从入门到入土②:常用命令

1. ls path [watch]

查看某个路径下有多少个节点

1
2
3
4
[zk: localhost:2181(CONNECTED) 2] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 3] ls /zookeeper
[quota]

2. ls2 path [watch]

= ls + stat

3. get path [watch]

获取值

1
2
3
4
5
6
7
8
9
10
11
12
13
[zk: localhost:2181(CONNECTED) 10] get /test
test
cZxid = 0x8
ctime = Mon May 27 14:12:38 CST 2019
mZxid = 0x8
mtime = Mon May 27 14:12:38 CST 2019
pZxid = 0x8
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0

4. stat path [watch]

1
2
3
4
5
6
7
8
9
10
11
12
[zk: localhost:2181(CONNECTED) 4] 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
  • cZxid:Create ZXID,表示节点被创建时的事务 ID。
  • ctime:Create Time,表示节点创建时间。
  • mZxid:Modified ZXID,表示节点最后⼀次被修改时的事务 ID。
  • mtime:Modified Time,表示节点最后⼀次被修改的时间。
  • pZxid:该节点的⼦节点列表最后⼀次被修改时的事务 ID。只有⼦节点列表变更才会更新 pZxid, ⼦节点内容变更不会更新。
  • cversion:⼦节点的版本号。
  • dataVersion:内容版本号。
  • aclVersion:标识 acl 版本
  • ephemeralOwner:创建该临时节点时的会话 sessionID,如果是持久性节点那么值为 0
  • dataLength:数据⻓度。
  • numChildren:直系⼦节点数。

5. create [-s] [-e] path data acl

-s: sequence。顺序节点,为节点自动添加

1
2
3
4
5
6
7
8
[zk: localhost:2181(CONNECTED) 15] create -s /test/sec seq
Created test/sec0000000001
[zk: localhost:2181(CONNECTED) 16] ls /test
[sec0000000001]
[zk: localhost:2181(CONNECTED) 17] create -s /test/sec seq
Created test/sec0000000002
[zk: localhost:2181(CONNECTED) 18] ls /test
[sec0000000001, sec0000000002]

-e: ephemeral。Session 创建临时节点

  • 非持久化,临时节点
  • 客户端与服务端之间维持心跳联系,无心跳则删除
  • ephemeralOwner 为非 0x0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [zk: localhost:2181(CONNECTED) 12] create -e /test/tmp test-data
    Created test/tmp
    [zk: localhost:2181(CONNECTED) 13] get /test/tmp
    test-data
    cZxid = 0x9
    ctime = Mon May 27 14:26:43 CST 2019
    mZxid = 0x9
    mtime = Mon May 27 14:26:43 CST 2019
    pZxid = 0x9
    cversion = 0
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x16adeeddbe80003
    dataLength = 9
    numChildren = 0

6. set path data [version]

dataVersion 会 +1

1
2
3
4
5
6
7
8
9
10
11
12
[zk: localhost:2181(CONNECTED) 23] set /test new-test-data
cZxid = 0x8
ctime = Mon May 27 14:12:38 CST 2019
mZxid = 0xc
mtime = Mon May 27 14:53:38 CST 2019
pZxid = 0xb
cversion = 3
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 13
numChildren = 3

version: 验证版本号
若填写的版本号与当前版本号不一致则 set 失败

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[zk: localhost:2181(CONNECTED) 25] set /test new-test-data-2 1
cZxid = 0x8
ctime = Mon May 27 14:12:38 CST 2019
mZxid = 0xd
mtime = Mon May 27 15:15:12 CST 2019
pZxid = 0xb
cversion = 3
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 15
numChildren = 3
[zk: localhost:2181(CONNECTED) 26] set /test new-test-data-2 1
version No is not valid : /test
[zk: localhost:2181(CONNECTED) 27] set /test new-test-data-2 3
version No is not valid : /test

7. delete path [version]

1
2
3
[zk: localhost:2181(CONNECTED) 31] delete /test/sec
[zk: localhost:2181(CONNECTED) 32] ls /test
[tmp]

version: 验证版本号
若填写的版本号与当前版本号不一致则 delete 失败( 同 set)