您的位置:首页 > 其它

ZooKeeper 客户端的使用

2016-08-21 11:04 323 查看
连接zk
1

2
cd
bin

zkCli.sh -timeout 5000 -server 27.154.242.214:5091


输入h,回车查看帮助

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23
[zk: 27.154.242.214:5091(CONNECTED) 2] h

ZooKeeper -server host:port cmd args

stat path [
watch
]

set
path data [version]

ls
path [
watch
]

delquota [-n|-b] path

ls2 path [
watch
]

setAcl path acl

setquota -n|-b val path

history

redo cmdno

printwatches on|off

delete path [version]

sync
path

listquota path

rmr path

get path [
watch
]

create [-s] [-e] path data acl

addauth scheme auth

quit

getAcl path

close

connect host:port


创建节点
1

2
[zk: 27.154.242.214:5091(CONNECTED) 11]create
/note1
123

Created
/note1


-s 表示是顺序节点 -e表示为临时节点

创建临时节点
(会话结束节点就被删除,qiut(退出) 然后在登陆 )

1

2
[zk: 27.154.242.214:5091(CONNECTED) 11]create
-e
/note2
123

Created
/note2


顺序节点
(应用场景:分布式的主键生成器)

1

2

3

4
[zk: 27.154.242.214:5091(CONNECTED) 1] create -s
/note1/note13
1333

Created
/note1/note130000000002

[zk: 27.154.242.214:5091(CONNECTED) 2] create -s
/note1/note13
1333

Created
/note1/note130000000003


查看节点状态

1

2

3

4

5

6

7

8

9

10

11

12
[zk: 27.154.242.214:5091(CONNECTED) 12] stat
/note1

cZxid = 0xc0b

ctime = Sun Aug 21 07:42:06 CST 2016

mZxid = 0xc0b

mtime = Sun Aug 21 07:42:06 CST 2016

pZxid = 0xc0b

cversion = 0

dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 3

numChildren = 0


说明:每一次操作都是一个事务,每一个事务都有一个事务id
2. cZxid 该节点被创建的时候的事务id
3. ctime 节点被创建的时间
4. mZxid 最后一次更新的事务id
5. mtime 最后一次更新的时间
6. pZxid 该节点的子节点最后一次修改的事务id(修改子节点的内容不算)
7. cversion 子节点的版本号
8. dataVersion 数据版本号
9. aclVersion acl权限版本号
10. ephemeralOwner 创建临时节点的事务id(永久节点的话 这个值固定为0)
11. dataLength 当前节点存放数据长度
12. numChinldren 当前节点拥有子节点的个数

获取当前节点存储的数据的内容

1

2

3

4

5

6

7

8

9

10

11

12

13
[zk: 27.154.242.214:5091(CONNECTED) 13] get
/note1

123

cZxid = 0xc0b

ctime = Sun Aug 21 07:42:06 CST 2016

mZxid = 0xc0b

mtime = Sun Aug 21 07:42:06 CST 2016

pZxid = 0xc0b

cversion = 0

dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 3

numChildren = 0


ls2列出当前节点的子节点同时输出stat信息

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
[zk: 27.154.242.214:5091(CONNECTED) 16] create
/note1/note11
456

[zk: 27.154.242.214:5091(CONNECTED) 18] create
/note1/note12
789

[zk: 27.154.242.214:5091(CONNECTED) 19]ls2
/note1

[note12, note11]

cZxid = 0xc0b

ctime = Sun Aug 21 07:42:06 CST 2016

mZxid = 0xc0b

mtime = Sun Aug 21 07:42:06 CST 2016

pZxid = 0xc0d

cversion = 2

dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 3

numChildren = 2


修改

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
[zk: 27.154.242.214:5091(CONNECTED) 8]
set
/note1/note11
111

cZxid = 0xc0c

ctime = Sun Aug 21 07:47:28 CST 2016

mZxid = 0xc15

mtime = Sun Aug 21 09:12:07 CST 2016

pZxid = 0xc0c

cversion = 0

dataVersion = 1

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 3

numChildren = 0

[zk: 27.154.242.214:5091(CONNECTED) 9] get
/note1/note11

111

cZxid = 0xc0c

ctime = Sun Aug 21 07:47:28 CST 2016

mZxid = 0xc15

mtime = Sun Aug 21 09:12:07 CST 2016

pZxid = 0xc0c

cversion = 0

dataVersion = 1

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 3

numChildren = 0


再进行修改,dataversion会一直增加上去

删除节点

(rmr 循环删除节点及其子节点)

1

2

3

4

5

6

7

8
[zk: 27.154.242.214:5091(CONNECTED) 10] delete
/note1/note11

[zk: 27.154.242.214:5091(CONNECTED) 11]
ls
/note1

[note12, note130000000002, note130000000003]

[zk: 27.154.242.214:5091(CONNECTED) 12] delete
/note1

Node not empty:
/note1

[zk: 27.154.242.214:5091(CONNECTED) 13] rmr
/note1

[zk: 27.154.242.214:5091(CONNECTED) 14]
ls
/

[server, zookeeper, config]


配额相关的指令

设置配额

[setquota -n|-b val path]

-n限制子节点的个数 –b限制数据的长度

1

2

3

4

5

6

7

8

9

10
[zk: 27.154.242.214:5091(CONNECTED) 18] create
/note1
123

Created
/note1

[zk: 27.154.242.214:5091(CONNECTED) 19]
setquota -n 2
/note1

Comment: the parts are option -n val 2 path
/note1

[zk: 27.154.242.214:5091(CONNECTED) 20] create
/note1/note11
11

Created
/note1/note11

[zk: 27.154.242.214:5091(CONNECTED) 21] create
/note1/note12
12

Created
/note1/note12

[zk: 27.154.242.214:5091(CONNECTED) 22] create
/note1/note13
13

Created
/note1/note13


发现超限了仅仅在日志记录超限信息的警告不会有异常如下:

1

2

3

4

5

6

7

8

9

10

11

12

13
[lzmhdev@lzmh01 zookeeper-3.4.8]$
cd
bin

[lzmhdev@lzmh01 bin]$
ls

README.txt  zkCleanup.sh  zkCli.cmd  zkCli.sh  zkEnv.cmd  zkEnv.sh  zkServer.cmd  zkServer.sh  zookeeper.out

[lzmhdev@lzmh01 bin]$
tail
-f zookeeper.out

at java.lang.Thread.run(Thread.java:745)

2016-08-21 08:54:07,402 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0
/0
.0.0.0:5091:NIOServerCnxn@1008] - Closed socket connection
for
client
/123
.59.54.18:34285
which
had sessionid 0x156aa06f8d60001

2016-08-21 08:54:12,000 [myid:] - INFO  [SessionTracker:ZooKeeperServer@355] - Expiring session 0x156aa06f8d60001, timeout of 5000ms exceeded

2016-08-21 08:54:12,001 [myid:] - INFO  [ProcessThread(sid:0 cport:5091)::PrepRequestProcessor@489] - Processed session termination
for
sessionid: 0x156aa06f8d60001

2016-08-21 08:54:21,201 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0
/0
.0.0.0:5091:NIOServerCnxnFactory@192] - Accepted socket connection from
/123
.59.54.18:34338

2016-08-21 08:54:21,205 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0
/0
.0.0.0:5091:ZooKeeperServer@900] - Client attempting to establish new session at
/123
.59.54.18:34338

2016-08-21 08:54:21,223 [myid:] - INFO  [SyncThread:0:ZooKeeperServer@645] - Established session 0x156aa06f8d60002 with negotiated timeout 5000
for
client
/123
.59.54.18:34338

2016-08-21 09:14:54,075 [myid:] - INFO  [ProcessThread(sid:0 cport:5091)::PrepRequestProcessor@651] - Got user-level KeeperException when processing sessionid:0x156aa06f8d60002
type
:delete cxid:0x15 zxid:0xc17 txntype:-1 reqpath:n
/a
Error Path:
/note1
Error:KeeperErrorCode = Directory not empty
for
/note1

2016-08-21 10:10:41,080 [myid:] - WARN  [SyncThread:0:DataTree@389] -
Quota exceeded:
/note1
count=3 limit=2


查看指定数据节点的配额情况

1

2

3

4
[zk: 27.154.242.214:5091(CONNECTED) 23] listquota
/note1

absolute path is
/zookeeper/quota/note1/zookeeper_limits

Output
quota
for
/note1
count=2,bytes=-1

Outputstat 
for
/note1
count=4,bytes=9


-1 长度没有限制

删除配额

1

2

3

4

5
[zk: 27.154.242.214:5091(CONNECTED) 24] delquota -n
/note1

[zk: 27.154.242.214:5091(CONNECTED) 25] listquota
/note1

absolute path is
/zookeeper/quota/note1/zookeeper_limits

Output
quota
for
/note1
count=-1,bytes=-1

Outputstat 
for
/note1
count=4,bytes=9


查看历史

并重复执行

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
[zk: 27.154.242.214:5091(CONNECTED) 26]
history

16 - create
/note1

17 - setquota -n 2
/note1

18 - create
/note1
123

19 - setquota -n 2
/note1

20 - create
/note1/note11
11

21 - create
/note1/note12
12

22 - create
/note1/note13
13

23 - listquota
/note1

24 - delquota -n
/note1

25 - listquota
/note1

26 -
history

[zk: 27.154.242.214:5091(CONNECTED) 27] redo 25

absolute path is
/zookeeper/quota/note1/zookeeper_limits

Output
quota
for
/note1
count=-1,bytes=-1

Outputstat 
for
/note1
count=4,bytes=9


参考
极客学院视频【链接:http://pan.baidu.com/s/1qYKPNyO 密码:nlz3】
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: