ZooKeeper常用命令行操作

  • 通过./zkCli.sh 打开zk的客户端进入命令行后台

    • ls/ls2 列出当前节点下的子节点 ls2还会列出当前节点的状态
    [zk: localhost:2181(CONNECTED) 5] ls2 /
    [cluster, controller_epoch, brokers, zookeeper, admin, isr_change_notification, consumers, log_dir_event_notification, latest_producer_id_block, config]
    cZxid = 0x0
    ctime = Thu Jan 01 08:00:00 CST 1970
    mZxid = 0x0
    mtime = Thu Jan 01 08:00:00 CST 1970
    pZxid = 0x4e
    cversion = 14
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 0
    numChildren = 10
    • get 与 stat命令

      • stat 列出节点状态 ls2相当于 ls + stat
    [zk: localhost:2181(CONNECTED) 6] stat /
    cZxid = 0x0
    ctime = Thu Jan 01 08:00:00 CST 1970
    mZxid = 0x0
    mtime = Thu Jan 01 08:00:00 CST 1970
    pZxid = 0x4e
    cversion = 14
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 0
    numChildren = 10
    • get命令 获取节点内容和节点状态
    [zk: localhost:2181(CONNECTED) 8] get /zookeeper
    
    cZxid = 0x0 #该节点创建(Create)时的id
    ctime = Thu Jan 01 08:00:00 CST 1970
    mZxid = 0x0 #该节点修改(Modify)后的id
    mtime = Thu Jan 01 08:00:00 CST 1970
    pZxid = 0x0 #子节点列表最后一次被修改的事务id
    cversion = -1 #子节点的版本号
    dataVersion = 0 #当前节点数据的版本号 节点数据改变时这个值会变化
    aclVersion = 0 # 权限版本, 权限发生变化时版本号回发生变化
    ephemeralOwner = 0x0
    dataLength = 0 #数据长度
    numChildren = 1 #子节点数量

4.2 zk特性 session的基本原理与create命令使用

  • 客户端与服务端之间的链接存在Session(会话)

  • 每个会话可以设置一个超时时间

  • 心跳机制: 客户端向服务端的ping包请求

  • 心跳结束, session则过期

  • Session过期, 则临时节点znode会被抛弃

  • create命令

    [zk: localhost:2181(CONNECTED) 10] create /czk czk-data #创建永久节点
    Created /czk
    [zk: localhost:2181(CONNECTED) 11] get /czk
    czk-data
    cZxid = 0x50
    ctime = Sat Dec 29 17:17:53 CST 2018
    mZxid = 0x50
    mtime = Sat Dec 29 17:17:53 CST 2018
    pZxid = 0x50
    cversion = 0
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 12
    numChildren = 0
    • 创建临时节点 &session过期临时节点删除
    [zk: localhost:2181(CONNECTED) 12] create -e /czk/tmp czk-data
    Created /itheima/tmp
    [zk: localhost:2181(CONNECTED) 13] get /czk
    itheima-data
    cZxid = 0x50
    ctime = Sat Dec 29 17:17:53 CST 2018
    mZxid = 0x50
    mtime = Sat Dec 29 17:17:53 CST 2018
    pZxid = 0x51
    cversion = 1
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 12
    numChildren = 1
    • 创建顺序节点
    [zk: localhost:2181(CONNECTED) 1] ls /czk
    []
    [zk: localhost:2181(CONNECTED) 2] create -s /czk/sec seq
    Created /itheima/sec0000000001
    [zk: localhost:2181(CONNECTED) 3] create -s /czk/sec seq
    Created /itheima/sec0000000002
    [zk: localhost:2181(CONNECTED) 4] ls /czk
    [sec0000000001, sec0000000002]

4.3 set/delete 命令

  • set命令:

     set path data [version] # set 路径 数据 [版本号]       []内为可选参数
    • 不传入版本号
    [zk: localhost:2181(CONNECTED) 17] get /czk
    czk-data1
    cZxid = 0x5b
    ctime = Sat Dec 29 17:45:26 CST 2018
    mZxid = 0x66
    mtime = Sat Dec 29 18:00:12 CST 2018
    pZxid = 0x65
    cversion = 5
    dataVersion = 1
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 13
    numChildren = 3
    [zk: localhost:2181(CONNECTED) 18] set /czk czk-data2
    cZxid = 0x5b
    ctime = Sat Dec 29 17:45:26 CST 2018
    mZxid = 0x67
    mtime = Sat Dec 29 18:02:15 CST 2018
    pZxid = 0x65
    cversion = 5
    dataVersion = 2
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 13
    numChildren = 3
    [zk: localhost:2181(CONNECTED) 19] get /czk
    czk-data2
    cZxid = 0x5b
    ctime = Sat Dec 29 17:45:26 CST 2018
    mZxid = 0x67
    mtime = Sat Dec 29 18:02:15 CST 2018
    pZxid = 0x65
    cversion = 5
    dataVersion = 2
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 13
    numChildren = 3
    • 不传入版本号可能会有并发修改问题 可以通过指定版本号类似加锁功能
    [zk: localhost:2181(CONNECTED) 22] set /czk czk-data3 1
    version No is not valid : /czk #版本不匹配 修改失败 当前版本2
    [zk: localhost:2181(CONNECTED) 23] set /czk czk-data3 2
    cZxid = 0x5b
    ctime = Sat Dec 29 17:45:26 CST 2018
    mZxid = 0x69
    mtime = Sat Dec 29 18:29:21 CST 2018
    pZxid = 0x65
    cversion = 5
    dataVersion = 3
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 13
    numChildren = 3
    [zk: localhost:2181(CONNECTED) 24] get /czk
    itheima-data3
    cZxid = 0x5b
    ctime = Sat Dec 29 17:45:26 CST 2018
    mZxid = 0x69
    mtime = Sat Dec 29 18:29:21 CST 2018
    pZxid = 0x65
    cversion = 5
    dataVersion = 3
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 13
    numChildren = 3
    • delete 命令: delete path [version]
    [zk: localhost:2181(CONNECTED) 25] ls /czk
    [sec0000000003, sec0000000001, sec0000000002]
    [zk: localhost:2181(CONNECTED) 27] delete /czk/sec000000000
    sec0000000003 sec0000000001 sec0000000002
    [zk: localhost:2181(CONNECTED) 27] delete /czk/sec0000000001
    [zk: localhost:2181(CONNECTED) 28] ls /czk
    [sec0000000003, sec0000000002]
    [zk: localhost:2181(CONNECTED) 29] set /czk/sec000000000
    sec0000000003 sec0000000002
    [zk: localhost:2181(CONNECTED) 29] set /czk/sec0000000002 test2
    cZxid = 0x64
    ctime = Sat Dec 29 17:49:38 CST 2018
    mZxid = 0x6b
    mtime = Sat Dec 29 18:54:33 CST 2018
    pZxid = 0x64
    cversion = 0
    dataVersion = 1
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 5
    numChildren = 0
    [zk: localhost:2181(CONNECTED) 30] delete /czk/sec000000000
    sec0000000003 sec0000000002
    [zk: localhost:2181(CONNECTED) 30] delete /czk/sec0000000002 0
    version No is not valid : /czk/sec0000000002
  • 总结 使用set / delete命令的时候尽量使用版本号 避免误操作

4.4 watcher机制

  • 针对每个节点的操作, 都会有一个监督者->watcher

  • 当监控的某个对象(znode)发生了变化, 则触发watcher事件

  • zk中的watcher是一次性的, 触发后立即销毁

  • 父节点, 子节点增删改都能触发其watcher

  • 针对不同类型的操作, 触发的watcher事件也不同:

    • (子)节点创建事件
    • (子)节点删除事件
    • (子)节点数据变化事件
  • watcher命令行

    • 通过get path [watch]设置watcher
    • 父节点 增 删 改操作触发 watcher
    • 子节点 增 删 改操作触发 watcher
  • 创建父节点触发: NodeCreated

    • stat path [watch]
    [zk: localhost:2181(CONNECTED) 33] stat /czk watch
    Node does not exist: /itcast
    [zk: localhost:2181(CONNECTED) 35] create /czk 123 WATCHER::Created /czk WatchedEvent state:SyncConnected type:NodeCreated path:/czk
    • get path [watch]
    [zk: localhost:2181(CONNECTED) 36] get /czk watch
    123
    cZxid = 0x6d
    ctime = Sat Dec 29 19:43:13 CST 2018
    mZxid = 0x6d
    mtime = Sat Dec 29 19:43:13 CST 2018
    pZxid = 0x6d
    cversion = 0
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 3
    numChildren = 0
    [zk: localhost:2181(CONNECTED) 37] set /itcast 789 WATCHER:: WatchedEvent state:SyncConnected type:NodeDataChanged path:/czk
    cZxid = 0x6d
    ctime = Sat Dec 29 19:43:13 CST 2018
    mZxid = 0x6e
    mtime = Sat Dec 29 19:47:24 CST 2018
    pZxid = 0x6d
    cversion = 0
    dataVersion = 1
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 3
    numChildren = 0
    • ls/ls2 path [watch]
    [zk: localhost:2181(CONNECTED) 38] ls /czk watch
    []
    [zk: localhost:2181(CONNECTED) 39] delete /czk WATCHER:: WatchedEvent state:SyncConnected type:NodeDeleted path:/czk
  • ls为父节点设置watcher, 创建子节点触发:NodeChildrenChanged

  • ls为父节点设置watcher, 删除子节点触发:NodeChildrenChanged

  • ls为父节点设置watcher, 修改子节点不触发事件

    [zk: localhost:2181(CONNECTED) 49] ls /czk watch
    [sec0000000003, dir1, sec0000000002]
    [zk: localhost:2181(CONNECTED) 51] create /czk/dir2 123 WATCHER::Created /czk/dir2 WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/czk
    [zk: localhost:2181(CONNECTED) 52] ls /czk watch
    [sec0000000003, dir2, dir1, sec0000000002]
    [zk: localhost:2181(CONNECTED) 53] delete /czk/dir2 WATCHER:: WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/czk
    [zk: localhost:2181(CONNECTED) 54]

ZooKeeper常用命令行操作的更多相关文章

  1. 第4章 ZK基本特性与基于Linux的ZK客户端命令行学习 4-1 zookeeper常用命令行操作

    ls path [watch] watch是一个监督者.quota是zookeeper的子目录.目录就是节点的意思,对于zookeeper来说它是以一个节点来说的,所以说/就是根节点,zookeepe ...

  2. Zookeeper常用命令行及API

    一.Zookeeper常用命令行 1.启动zookeeper客户端(在启动zookeeper集群后启动进行调试) zkCli.sh 2.查看帮助.操作历史 help.history 3.查看当前Zno ...

  3. Zookeeper的命令行操作(三)

    Zookeeper的命令行操作 1. ZooKeeper服务命令 在准备好相应的配置之后,可以直接通过zkServer.sh 这个脚本进行服务的相关操作 1. 启动ZK服务: sh bin/zkSer ...

  4. git 常用命令行操作

    git常用命令行操作: 点击 git bash 进入到命令行: git clone https://github.com/wlz1244/qingoo.git   //下载一个master分支代码 g ...

  5. HDFS分布式文件系统的常用命令行操作

    一.HDFS的客户端种类 1.网页形式  =>用于测试 网址为你的namenode节点的ip+50070的端口号,如: 192.168.50.128:50070 2.命令行形式 =>用于测 ...

  6. HBase表的基本结构和常用命令行操作

    一.HBase表的基本结构 1.概述: 平时常见的MySQL.Oracle数据库都是传统型关系数据库,它们都是按行查询.按行存储:而HBase为非关系型数据库,它是按列存储的的. 在HBase插入数据 ...

  7. ZooKeeper常用命令行工具及使用(转)

    一.服务端 bin目录下常用的脚本解释 zkCleanup:清理Zookeeper历史数据,包括食物日志文件和快照数据文件 zkCli:Zookeeper的一个简易客户端 zkEnv:设置Zookee ...

  8. zookeeper客户端命令行操作

    一.命令行 (1)使用zookeeper安装bin目录下的./zkCli.sh连接到zookeeper服务器上,基本语法如下: ./zkCli.sh -timeout 0 -r -server ip: ...

  9. mysql常用命令行操作

    1.linux下彻底卸载mysql sudo rm /var/lib/mysql/ -Rsudo rm /etc/mysql/ -R sudo apt-get autoremove mysql* -- ...

随机推荐

  1. 整合到 Mockito 2

    为了能够持续改进 Mockito 和在未来提升测试体验,我们希望你能够升级到 Mockito 2.10!Mockito 按照语义化版本(semantic versioning)的方式对版本进行编排,并 ...

  2. JQuery事件和动画(7)

    一.事件: 1.鼠标事件 click( ) 触发或将函数绑定到指定元素的click事件 单击鼠标时 dblclick( ) 触发或将函数绑定到指定元素的dblclick事件 双击鼠标时 mouseov ...

  3. 26.Python三目运算符(三元运算符)用法详解

    Python 可通过 if 语句来实现三目运算符的功能,因此可以近似地把这种 if 语句当成三目运算符.作为三目运算符的 if 语句的语法格式如下: True_statements if expres ...

  4. React 番外篇

    小技巧:如果我们想了解一门技术,不知道如何学习,那就在 BOSS 直聘上,来看看对这门技术的要求 这篇给大家讲的是 React 1.0 的初始版本,仅仅是让大家有个了解,毕竟回顾历史,我们才能找到他最 ...

  5. isEmpty和isBlank区别

    isEmpty 判断某字符串是否为空,为空的标准是 str==null或 str.length()==0 StringUtils.isEmpty(null) = true StringUtils.is ...

  6. iframe 跨域传参

    parent-index.html: (本地起服务,放在5000端口上) <div class="content"> <iframe src="http ...

  7. php post请求

    public function file_get_contents_post($url, $post){ $options = array( 'http'=> array( 'method'=& ...

  8. python--nolocal

    Compare this, without using nonlocal: x = 0def outer(): x = 1 def inner(): x = 2 print("inner:& ...

  9. ASP.NET Core-Docs:在 ASP.NET Core 中启用跨域请求(CORS)

    ylbtech-ASP.NET Core-Docs:在 ASP.NET Core 中启用跨域请求(CORS) 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 1. ...

  10. Python——GUI编程 控件及常用信号

    QSlider类中的常用信号 valueChanged: 当滑块位置发生改变时触发此信号 sliderPressed: 当用户按下滑块时触发此信号 sliderMoved: 当用户拖动滑块时触发此信号 ...