[zookeeper]2.zookeeper的常用API

2021-01-24 00:47发布

列出节点节点

ls /path

  • 递归查看目录

    ls -R /path

[zk: localhost:2181(CONNECTED) 6] ls -R /sanguo
/sanguo
/sanguo/shuguo
/sanguo/shuguo0000000002
/sanguo/shuguo0000000003
/sanguo/shuguo0000000004
/sanguo/shuguo0000000005

创建znode节点

语法:create [-s] [-e] [-c] [-t ttl] path [data] [acl]
参数:使用[]包含的是可选参数。
[-s] : 创建有序节点。
[-e] : 创建临时节点。
[-c] : 创建一个容器节点。
[t ttl] : 创建一个TTL节点, -t 时间(单位毫秒)。
path: 路径 ,因为没有中括号,所以是必须参数。
[data]:节点的数据,可选,如果不使用时,节点数据就为null。
[acl] :权限相关,后面文章讲。

  • 创建临时节点

    create -e /path

create -e /sanguo/wuguo "zhouyu"
  • 创建有序节点
    create -s /sanguo/shuguo zhugeliang
    [zk: localhost:2181(CONNECTED) 9] get /sanguo/shuguo
    liubei
    [zk: localhost:2181(CONNECTED) 10] create -s /sanguo/shuguo zhugeliang
    Created /sanguo/shuguo0000000002
    [zk: localhost:2181(CONNECTED) 11] create -s /sanguo/shuguo zhangfei
    Created /sanguo/shuguo0000000003
    [zk: localhost:2181(CONNECTED) 12] create -s /sanguo/shuguo guanyu
    Created /sanguo/shuguo0000000004

查看znode节点

get /path

查看节点数据加元信息

get -s /path

[zk: localhost:2181(CONNECTED) 0] get -s /sanguo
中国
cZxid = 0x9
ctime = Thu Jan 21 23:21:22 CST 2021
mZxid = 0x16
mtime = Thu Jan 21 23:50:57 CST 2021
pZxid = 0x12
cversion = 6
dataVersion = 3
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 4
  • 查看节点元数据信息

    get -s /path

[zk: localhost:2181(CONNECTED) 0] get -s /sanguo
中国
cZxid = 0x9
ctime = Thu Jan 21 23:21:22 CST 2021
mZxid = 0x16
mtime = Thu Jan 21 23:50:57 CST 2021
pZxid = 0x12
cversion = 6
dataVersion = 3
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 4

修改znode节点

set /path value

[zk: localhost:2181(CONNECTED) 15] get /sanguo
刘备
[zk: localhost:2181(CONNECTED) 16] set /sanguo "古代中国"
[zk: localhost:2181(CONNECTED) 17] get /sanguo
古代中国
标签: