• Zookeeper常用节点命令


    1 新增节点

    create [-s] [-e] path data #其中-s 为有序节点,-e 临时节点

    创建持久化节点并写入数据:

    [zk: localhost:2181(CONNECTED) 0] create /hadoop "123456"
    Created /hadoop
    [zk: localhost:2181(CONNECTED) 1] get /hadoop
    123456
    cZxid = 0x2
    ctime = Sat Apr 11 19:15:47 CST 2020
    mZxid = 0x2
    mtime = Sat Apr 11 19:15:47 CST 2020
    pZxid = 0x2
    cversion = 0
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 6
    numChildren = 0

     退出客户端在查看,是否能查到:

    [zk: localhost:2181(CONNECTED) 3] quit
    Quitting...
    2020-04-11 19:29:52,410 [myid:] - INFO  [main:ZooKeeper@684] - Session: 0x17168d                                                                       c8c420000 closed
    2020-04-11 19:29:52,412 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread                                                                       @519] - EventThread shut down for session: 0x17168dc8c420000
    [root@iZ1la3d1xbmukrZ bin]# ./zkCli.sh
    Connecting to localhost:2181
    2020-04-11 19:29:54,955 [myid:] - INFO  [main:Environment@100] - Client environm                                                                       ent:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on                                                                        03/23/2017 10:13 GMT
    2020-04-11 19:29:54,958 [myid:] - INFO  [main:Environment@100] - Client environm                                                                       ent:host.name=iZ1la3d1xbmukrZ
    2020-04-11 19:29:54,958 [myid:] - INFO  [main:Environment@100] - Client environm                                                                       ent:java.version=1.8.0_131
    2020-04-11 19:29:54,960 [myid:] - INFO  [main:Environment@100] - Client environm                                                                       ent:java.vendor=Oracle Corporation
    2020-04-11 19:29:54,961 [myid:] - INFO  [main:Environment@100] - Client environm                                                                       ent:java.home=/home/zookeeper/jdk1.8.0_131/jre
    2020-04-11 19:29:54,961 [myid:] - INFO  [main:Environment@100] - Client environm 
    [zk: localhost:2181(CONNECTED) 0] get /hadoop
    123456
    cZxid = 0x2
    ctime = Sat Apr 11 19:15:47 CST 2020
    mZxid = 0x2
    mtime = Sat Apr 11 19:15:47 CST 2020
    pZxid = 0x2
    cversion = 0
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 6
    numChildren = 0

    创建持久化有序节点,此时创建的节点名为指定节点名 + 自增序号:

    [zk: localhost:2181(CONNECTED) 1] create -s /a "a"
    Created /a0000000001
    [zk: localhost:2181(CONNECTED) 2] get /a0000000001
    a
    cZxid = 0x5
    ctime = Sat Apr 11 19:35:03 CST 2020
    mZxid = 0x5
    mtime = Sat Apr 11 19:35:03 CST 2020
    pZxid = 0x5
    cversion = 0
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 1
    numChildren = 0
    [zk: localhost:2181(CONNECTED) 3] create -s /b "b"
    Created /b0000000002

     创建临时节点,临时节点会在会话过期后被删除:

    Created /b0000000002
    [zk: localhost:2181(CONNECTED) 4] create -e /tmp "tmp"
    Created /tmp
    [zk: localhost:2181(CONNECTED) 5] quit
    Quitting...
    2020-04-11 20:25:24,342 [myid:] - INFO  [main:ZooKeeper@684] - Session: 0x17168dc8c420001 closed
    2020-04-11 20:25:24,344 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x17168dc8c420001
    [root@iZ1la3d1xbmukrZ bin]# ./zkCli.sh
    WATCHER::
    
    WatchedEvent state:SyncConnected type:None path:null
    
    [zk: localhost:2181(CONNECTED) 0] get /tmp
    Node does not exist: /tmp
    [zk: localhost:2181(CONNECTED) 1]

    创建临时有序节点,临时节点会在会话过期后被删除:

    [zk: localhost:2181(CONNECTED) 2] create -s -e /aa 'aaa'
    Created /aa0000000004
    [zk: localhost:2181(CONNECTED) 3] create -s -e /bb 'nnn'
    Created /bb0000000005
    [zk: localhost:2181(CONNECTED) 4]

    2 更新节点
    更新节点的命令是 set ,可以直接进行修改,如下:

    [zk: localhost:2181(CONNECTED) 4] get /hadoop
    123456
    cZxid = 0x2
    ctime = Sat Apr 11 19:15:47 CST 2020
    mZxid = 0x2
    mtime = Sat Apr 11 19:15:47 CST 2020
    pZxid = 0x2
    cversion = 0
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 6
    numChildren = 0
    [zk: localhost:2181(CONNECTED) 5] set /hadoop "3456"
    cZxid = 0x2
    ctime = Sat Apr 11 19:15:47 CST 2020
    mZxid = 0xc
    mtime = Sat Apr 11 20:27:51 CST 2020
    pZxid = 0x2
    cversion = 0
    dataVersion = 1
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 4
    numChildren = 0
    [zk: localhost:2181(CONNECTED) 6] get /hadoop
    3456
    cZxid = 0x2
    ctime = Sat Apr 11 19:15:47 CST 2020
    mZxid = 0xc
    mtime = Sat Apr 11 20:27:51 CST 2020
    pZxid = 0x2
    cversion = 0
    dataVersion = 1
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 4
    numChildren = 0
    [zk: localhost:2181(CONNECTED) 7]

    可以基于版本号进行更改,此时类似于乐观锁机制,当你传入的数据版本号(dataVersion) 和当前节点的数据版本号不符合时,zookeeper 会拒绝本次修改:

    [zk: localhost:2181(CONNECTED) 7] set /hadoop "345" 1
    cZxid = 0x2
    ctime = Sat Apr 11 19:15:47 CST 2020
    mZxid = 0xd
    mtime = Sat Apr 11 20:29:15 CST 2020
    pZxid = 0x2
    cversion = 0
    dataVersion = 2
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 3
    numChildren = 0
    [zk: localhost:2181(CONNECTED) 8] set /hadoop "345" 3
    version No is not valid : /hadoop
    [zk: localhost:2181(CONNECTED) 9]

    3 删除节点
    ​ 删除节点的语法如下:

    delete path [version]

    和更新节点数据一样,也可以传入版本号,当你传入的数据版本号 (dataVersion),当前节点的数据版本号不符合时,zookeeper 不会执行删除操作。

    [zk: localhost:2181(CONNECTED) 9] delete /hadoop 3
    version No is not valid : /hadoop
    [zk: localhost:2181(CONNECTED) 10] delete /hadoop 2
    [zk: localhost:2181(CONNECTED) 11] get /hadoop
    Node does not exist: /hadoop
    [zk: localhost:2181(CONNECTED) 12]

    要想删除某个节点及其所有后代节点,可以使用递归删除,命令为 rmr

    [zk: localhost:2181(CONNECTED) 2] create /tmp "123"
    Created /tmp
    [zk: localhost:2181(CONNECTED) 3] create /tmp/wgr "123"
    Created /tmp/wgr
    [zk: localhost:2181(CONNECTED) 4] create /tmp/wgr/qwer "123"
    Created /tmp/wgr/qwer
    [zk: localhost:2181(CONNECTED) 5] create /tmp/wgr/qwer/re "123"
    Created /tmp/wgr/qwer/re
    [zk: localhost:2181(CONNECTED) 6] delete /tmp
    Node not empty: /tmp
    [zk: localhost:2181(CONNECTED) 7] get /tmp
    123
    cZxid = 0x15
    ctime = Sat Apr 11 20:32:16 CST 2020
    mZxid = 0x15
    mtime = Sat Apr 11 20:32:16 CST 2020
    pZxid = 0x16
    cversion = 1
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 3
    numChildren = 1
    [zk: localhost:2181(CONNECTED) 8]
    [zk: localhost:2181(CONNECTED) 8] rmr /tmp
    [zk: localhost:2181(CONNECTED) 9] get /tmp
    Node does not exist: /tmp
    [zk: localhost:2181(CONNECTED) 10]

    查看节点

    get path

    [zk: localhost:2181(CONNECTED) 6] get /hadoop
    3456
    cZxid = 0x2
    ctime = Sat Apr 11 19:15:47 CST 2020
    mZxid = 0xc
    mtime = Sat Apr 11 20:27:51 CST 2020
    pZxid = 0x2
    cversion = 0
    dataVersion = 1
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 4
    numChildren = 0

    节点各个属性如下表。其中一个重要的概念是 Zxid(ZooKeeper TransactionId),ZooKeeper 节点的每一次更改都具有唯一的 Zxid,如果 Zxid1 小于 Zxid2,则Zxid1 的更改发生在 Zxid2 更改之前。

    状态属性 说明
    cZxid 数据节点创建时的事务 ID
    ctime 数据节点创建时的时间
    mZxid 数据节点最后一次更新时的事务 ID
    mtime 数据节点最后一次更新时的时间
    pZxid 数据节点的子节点最后一次被修改时的事务 ID
    cversion 子节点的更改次数
    dataVersion 节点数据的更改次数
    aclVersion 节点的 ACL 的更改次数
    ephemeralOwner 如果节点是临时节点, 则表示创建该节点的会话的
    SessionID; 如果节点是持久节点, 则该属性值为 0
    dataLength 数据内容的长度
    numChildren 数据节点当前的子节点个数

    当创建子节点的时候,pZxid会发生改变

    查看节点状态
    可以使用 stat 命令查看节点状态, 它的返回值和 get 命令类似, 但不会返回节点数据 

    [zk: localhost:2181(CONNECTED) 11] stat /hadoop
    cZxid = 0x1e
    ctime = Sat Apr 11 20:39:10 CST 2020
    mZxid = 0x1e
    mtime = Sat Apr 11 20:39:10 CST 2020
    pZxid = 0x1e
    cversion = 0
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 3
    numChildren = 0
    [zk: localhost:2181(CONNECTED) 12]

    查看节点列表
    ​ 查看节点列表有 ls path 和 ls2 path 两个命令,后者是前者的增强,不仅可以查看指定路径下的所有节点,还可以查看当前节点的信息

    [zk: localhost:2181(CONNECTED) 12] ls /
    [b0000000002, a0000000001, hadoop, zookeeper, tmp0000000006]
    [zk: localhost:2181(CONNECTED) 13] ls2 /
    [b0000000002, a0000000001, hadoop, zookeeper, tmp0000000006]
    cZxid = 0x0
    ctime = Thu Jan 01 08:00:00 CST 1970
    mZxid = 0x0
    mtime = Thu Jan 01 08:00:00 CST 1970
    pZxid = 0x1e
    cversion = 13
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 0
    numChildren = 5
    [zk: localhost:2181(CONNECTED) 14]

    4  监听器

    get path [watch]
    ​ 使用 get path [watch] 注册的监听器能够在节点内容发生改变的时候,向客户端发出通知。需要注意的是 zookeeper 的触发器是一次性的 (One-time trigger),即触发一次后就会立即失效。

    [zk: localhost:2181(CONNECTED) 18] get /hadoop watch
    456
    cZxid = 0x1e
    ctime = Sat Apr 11 20:39:10 CST 2020
    mZxid = 0x1f
    mtime = Sat Apr 11 20:46:15 CST 2020
    pZxid = 0x20
    cversion = 1
    dataVersion = 1
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 3
    numChildren = 1
    [zk: localhost:2181(CONNECTED) 19] set /hadoop 45678
    
    WATCHER::
    
    WatchedEvent state:SyncConnected type:NodeDataChanged path:/hadoop
    cZxid = 0x1e
    ctime = Sat Apr 11 20:39:10 CST 2020
    mZxid = 0x21
    mtime = Sat Apr 11 20:53:32 CST 2020
    pZxid = 0x20
    cversion = 1
    dataVersion = 2
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 5
    numChildren = 1
    [zk: localhost:2181(CONNECTED) 20] set /hadoop 43
    cZxid = 0x1e
    ctime = Sat Apr 11 20:39:10 CST 2020
    mZxid = 0x22
    mtime = Sat Apr 11 20:53:46 CST 2020
    pZxid = 0x20
    cversion = 1
    dataVersion = 3
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 2
    numChildren = 1
    [zk: localhost:2181(CONNECTED) 21]

    监听器stat path [watch]
    ​ 使用 stat path [watch] 注册的监听器能够在节点状态发生改变的时候,向客户端发出通知

    [zk: localhost:2181(CONNECTED) 0] stat /hadoop watch
    cZxid = 0x1e
    ctime = Sat Apr 11 20:39:10 CST 2020
    mZxid = 0x22
    mtime = Sat Apr 11 20:53:46 CST 2020
    pZxid = 0x20
    cversion = 1
    dataVersion = 3
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 2
    numChildren = 1
    [zk: localhost:2181(CONNECTED) 1] set /hadoop 112233
    
    WATCHER::
    
    WatchedEvent state:SyncConnected type:NodeDataChanged path:/hadoop
    cZxid = 0x1e
    ctime = Sat Apr 11 20:39:10 CST 2020
    mZxid = 0x26
    mtime = Sat Apr 11 20:56:48 CST 2020
    pZxid = 0x20
    cversion = 1
    dataVersion = 4
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 6
    numChildren = 1

    监听器lsls2 path [watch]
    ​ 使用 ls path [watch] 或 ls2 path [watch] 注册的监听器能够监听该节点下所有子节点的增加和删除操作。

    [zk: localhost:2181(CONNECTED) 3] ls /hadoop watch
    [hbase]
    [zk: localhost:2181(CONNECTED) 4] create /hadoop/node3 "node3"
    
    WATCHER::
    Created /hadoop/node3
    
    WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/hadoop
    [zk: localhost:2181(CONNECTED) 5] ls /hadoop watch
    [node3, hbase]
    [zk: localhost:2181(CONNECTED) 6] delete /hadoop/node3
    
    WATCHER::
    [zk: localhost:2181(CONNECTED) 7]
    WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/hadoop
    
    [zk: localhost:2181(CONNECTED) 7]
  • 相关阅读:
    docker安装rabbitmq
    ios 崩溃日志揭秘
    Ubuntu apache2下建立django开发环境
    Ubuntu tomcat
    MySQLdb插入后获得插入id
    使用终端实现DMG、ISO格式互转
    ios学习笔记:jison(转)
    ios开发应用内实现多语言自由切换 (转)
    获取项目的名称及版本号
    工具收集
  • 原文地址:https://www.cnblogs.com/dalianpai/p/12682368.html
Copyright © 2020-2023  润新知