Fork me on GitHub

Zookeeper 客户端命令行

操作zookeeper

1)启动zookeeper
[liucw@hadoop102 zookeeper-3.4.10]$ bin/zkServer.sh start

2)查看进程是否启动

1
2
3
[liucw@hadoop102 zookeeper-3.4.10]$ jps
4020 Jps
4001 QuorumPeerMain

3)查看状态:

1
2
3
4
[liucw@hadoop102 zookeeper-3.4.10]$ bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: standalone # 单结点,就是本地模式运行

4)启动客户端:
[liucw@hadoop102 zookeeper-3.4.10]$ bin/zkCli.sh

5)退出客户端:
[zk: localhost:2181(CONNECTED) 0] quit

6)停止zookeeper
[liucw@hadoop102 zookeeper-3.4.10]$ bin/zkServer.sh stop


客户端命令行操作

1)启动客户端
[liucw@hadoop103 zookeeper-3.4.10]$ bin/zkCli.sh

2)显示所有操作命令:help
[zk: localhost:2181(CONNECTED) 1] help

3)查看当前znode中所包含的内容: ls

1
2
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]

4)查看当前节点数据并能看到更新次数等数据: ls2

1
2
3
4
5
6
7
8
9
10
11
12
13
[zk: localhost:2181(CONNECTED) 1] ls2 /
[zookeeper]
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

5)创建普通节点:create

1
2
3
4
5
[zk: localhost:2181(CONNECTED) 2] create /app1 "hello app1"
Created /app1
[zk: localhost:2181(CONNECTED) 4] create /app1/server101 "192.168.1.101"
Created /app1/server101

6)获得节点的值: get

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[zk: localhost:2181(CONNECTED) 6] get /app1
hello app1
cZxid = 0x20000000a
ctime = Mon Jul 17 16:08:35 CST 2017
mZxid = 0x20000000a
mtime = Mon Jul 17 16:08:35 CST 2017
pZxid = 0x20000000b
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 10
numChildren = 1
[zk: localhost:2181(CONNECTED) 8] get /app1/server101
192.168.1.101
cZxid = 0x20000000b
ctime = Mon Jul 17 16:11:04 CST 2017
mZxid = 0x20000000b
mtime = Mon Jul 17 16:11:04 CST 2017
pZxid = 0x20000000b
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 13
numChildren = 0

7)创建短暂节点: create -e

1
2
3
4
5
6
7
8
9
10
11
12
13
[zk: localhost:2181(CONNECTED) 9] create -e /app-emphemeral 8888
(1)在当前客户端是能查看到的
[zk: localhost:2181(CONNECTED) 10] ls /
[app1, app-emphemeral, zookeeper]
(2)退出当前客户端然后再重启启动客户端
[zk: localhost:2181(CONNECTED) 12] quit
[liucw@hadoop104 zookeeper-3.4.10]$ bin/zkCli.sh
(3)再次查看根目录下短暂节点已经删除
[zk: localhost:2181(CONNECTED) 0] ls /
[app1, zookeeper]

8)创建带序号的节点:create -s
编号默认是有000开始的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(1)先创建一个普通的根节点app2
[zk: localhost:2181(CONNECTED) 11] create /app2 "app2"
(2)创建带序号的节点
[zk: localhost:2181(CONNECTED) 13] create -s /app2/aa 888
Created /app2/aa0000000000
[zk: localhost:2181(CONNECTED) 14] create -s /app2/bb 888
Created /app2/bb0000000001
[zk: localhost:2181(CONNECTED) 15] create -s /app2/cc 888
Created /app2/cc0000000002
如果原节点下有1个节点,则再排序时从1开始,以此类推。
[zk: localhost:2181(CONNECTED) 16] create -s /app1/aa 888
Created /app1/aa0000000001

9)修改节点数据值:set
[zk: localhost:2181(CONNECTED) 2] set /app1 999

10)节点的值变化监听:watch
只会监听一次
WatchedEvent state:SyncConnected type:NodeDataChanged path:/app1

1
2
3
4
5
6
7
8
9
(1)在104主机上注册监听/app1节点数据变化
[zk: localhost:2181(CONNECTED) 26] get /app1 watch
(2)在103主机上修改/app1节点的数据
[zk: localhost:2181(CONNECTED) 5] set /app1 777
(3)观察104主机收到数据变化的监听
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/app1

11)节点的子节点变化监听(路径变化)
只会监听一次
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/app1

1
2
3
4
5
6
7
8
9
10
11
(1)在104主机上注册监听/app1节点的子节点变化
[zk: localhost:2181(CONNECTED) 1] ls /app1 watch
[aa0000000001, server101]
(2)在103主机/app1节点上创建子节点
[zk: localhost:2181(CONNECTED) 6] create /app1/bb 666
Created /app1/bb
(3)观察104主机收到子节点变化的监听
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/app1

12)删除节点:delete
[zk: localhost:2181(CONNECTED) 4] delete /app1/bb

13)递归删除节点:rmr
[zk: localhost:2181(CONNECTED) 7] rmr /app2

14)查看节点状态:stat

1
2
3
4
5
6
7
8
9
10
11
12
[zk: localhost:2181(CONNECTED) 12] stat /app1
cZxid = 0x20000000a
ctime = Mon Jul 17 16:08:35 CST 2017
mZxid = 0x200000018
mtime = Mon Jul 17 16:54:38 CST 2017
pZxid = 0x20000001c
cversion = 4
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 2

-----------------本文结束,感谢您的阅读-----------------