zookeeper shell客户端命令简介

  • 1.客户端连接
[root@hadoop01 soft]# ./zookeeper-3.4.5/bin/zkCli.sh
  • 2.查看所有命令
    ZooKeeper -server host:port cmd args
    connect host:port
    get path [watch]
    ls path [watch]
    set path data [version]
    rmr path
    delquota [-n|-b] path
    quit 
    printwatches on|off
    create [-s] [-e] path data acl
    stat path [watch]
    close 
    ls2 path [watch]
    history 
    listquota path
    setAcl path acl
    getAcl path
    sync path
    redo cmdno
    addauth scheme auth
    delete path [version]
    setquota -n|-b val path
  • 3.命令简介
#ls 查看
[zk: hadoop02:2181(CONNECTED) 8] ls /
[zookeeper]

#create 创建节点并存储数据
[zk: hadoop02:2181(CONNECTED) 9] create /app1 "this is app1"         
Created /app1
[zk: hadoop02:2181(CONNECTED) 10] ls /
[app1, zookeeper]

#获取内容
[zk: hadoop02:2181(CONNECTED) 14] get /app1/server01
"192.168.80.151"
cZxid = 0x100000005
ctime = Tue Nov 15 17:22:49 EST 2016
mZxid = 0x100000005
mtime = Tue Nov 15 17:22:49 EST 2016
pZxid = 0x100000005
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 16
numChildren = 0

#quit 退出
  • 4.znode

    1)短暂节点(ephemeral) 断开连接会被删除 create -e
    2)持久节点 (persistent) 断开不会被删除 create

持久带序号 create -s
持久不带序号 create
短暂带序号 create -s -e
短暂不带序号 create -e

[zk: localhost:2181(CONNECTED) 2] create /lijie/test 234
Created /lijie/test

#-s创建带序号的节点
[zk: localhost:2181(CONNECTED) 3] create -s /lijie/test 345
Created /lijie/test0000000001

[zk: localhost:2181(CONNECTED) 4] create -s /lijie/test 345
Created /lijie/test0000000002

#-e -s, -e表示创建临时节点,退出就没有了
[zk: localhost:2181(CONNECTED) 5] create -e -s /lijie/test 345
Created /lijie/test0000000003
  • 5.修改数据
#查看 /lijie/test的数据 为234
[zk: localhost:2181(CONNECTED) 12] get /lijie/test     
234
cZxid = 0x10000000b
ctime = Tue Nov 15 17:34:08 EST 2016
mZxid = 0x10000000b
mtime = Tue Nov 15 17:34:08 EST 2016
pZxid = 0x10000000b
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0

#重新设置 /lijie/test
[zk: localhost:2181(CONNECTED) 13] set /lijie/test   66666
cZxid = 0x10000000b
ctime = Tue Nov 15 17:34:08 EST 2016
mZxid = 0x100000010
mtime = Tue Nov 15 17:46:23 EST 2016
pZxid = 0x10000000b
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0

#再此获取 /lijie/test 为 66666
[zk: localhost:2181(CONNECTED) 14] get /lijie/test
66666
cZxid = 0x10000000b
ctime = Tue Nov 15 17:34:08 EST 2016
mZxid = 0x100000010
mtime = Tue Nov 15 17:46:23 EST 2016
pZxid = 0x10000000b
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0
  • 6.监听通知

get 一个数据的时候带一个watch监听

#获取 /lijie/test的内容,并且注册监听
[zk: localhost:2181(CONNECTED) 15] get /lijie/test watch
66666
cZxid = 0x10000000b
ctime = Tue Nov 15 17:34:08 EST 2016
mZxid = 0x100000010
mtime = Tue Nov 15 17:46:23 EST 2016
pZxid = 0x10000000b
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0

#在另外一台服务器登陆zookeeper修改这个路径的值
[zk: localhost:2181(CONNECTED) 0] set /lijie/test 99999
cZxid = 0x10000000b
ctime = Tue Nov 15 17:34:08 EST 2016
mZxid = 0x100000012
mtime = Tue Nov 15 17:51:30 EST 2016
pZxid = 0x10000000b
cversion = 0
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0

#之前那个监听响应
[zk: localhost:2181(CONNECTED) 16] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeDataChanged path:/lijie/test

ls 带监听(子节点变化监听)

#一台服务器客户端监听子节点
[zk: localhost:2181(CONNECTED) 1] ls /lijie/test watch

#另外一台在该节点添加一个子节点
[zk: localhost:2181(CONNECTED) 16] create /lijie/test/tt 888888
Created /lijie/test/tt  

#监听的客户端发生响应
[zk: localhost:2181(CONNECTED) 2] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/lijie/test

你可能感兴趣的:(zookeeper)