0
点赞
收藏
分享

微信扫一扫

深入理解zookeeper

陈情雅雅 04-01 06:30 阅读 3

1、help

[zkshell: 1] help
# a sample one
[zkshell: 2] h
ZooKeeper -server host:port cmd args
	addauth scheme auth
	close
	config [-c] [-w] [-s]
	connect host:port
	create [-s] [-e] [-c] [-t ttl] path [data] [acl]
	delete [-v version] path
	deleteall path
	delquota [-n|-b|-N|-B] path
	get [-s] [-w] path
	getAcl [-s] path
	getAllChildrenNumber path
	getEphemerals path
	history
	listquota path
	ls [-s] [-w] [-R] path
	printwatches on|off
	quit
	reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*]
	redo cmdno
	removewatches path [-c|-d|-a] [-l]
	set [-s] [-v version] path data
	setAcl [-s] [-v version] [-R] path acl
	setquota -n|-b|-N|-B val path
	stat [-w] path
	sync path
	version

2、create [-s] [-e] [-c] [-t ttl] path [data] [acl]

[zk: localhost:2181(CONNECTED) 11] create /hello
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/
Created /hello
[zk: localhost:2181(CONNECTED) 12] create /hello/world
Created /hello/world
[zk: localhost:2181(CONNECTED) 13]

3、ls [-s] [-w] [-R] path

(1)ls /znode/path 展示该node下有多少个直属字znode

[zk: localhost:2181(CONNECTED) 13] ls /hello
[world]
[zk: localhost:2181(CONNECTED) 14] ls /hello/world
[]

(2)ls –s 展示znode属性

[zk: localhost:2181(CONNECTED) 15] ls -s /hello
[world]
cZxid = 0x17e
ctime = Tue Mar 26 15:33:08 CST 2024
mZxid = 0x17e
mtime = Tue Mar 26 15:33:08 CST 2024
pZxid = 0x17f
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1

(3)ls -R 递归展示子节点

[zk: localhost:2181(CONNECTED) 21] ls  -R /config
/config
/config/brokers
/config/changes
/config/clients
/config/ips
/config/topics
/config/users
/config/topics/__consumer_offsets
/config/topics/test
[zk: localhost:2181(CONNECTED) 22]

(4)ls -w /path/znode 对于子节点变化设置watch

当新增子节点或者删除子节点的时候会触发该watch并清除watch,即watch只有一次生效,只对直属子节点生效

[zk: localhost:2181(CONNECTED) 3] ls -w /helo
[world]
[zk: localhost:2181(CONNECTED) 4] create /helo/lichf
WATCHER::
Created /helo/lichf
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/helo

5、delete /path/znode

删除一个指定的znode,该znode必须是最终子znode,即该znode没有子znode

[zk: localhost:2181(CONNECTED) 22] delete /hello
Node not empty: /hello
[zk: localhost:2181(CONNECTED) 23]
  • delete -v version /path/znode
    删除指定dataVersion版本的znode,当传入的dataVersion与当前版本号不一致时,zookeeper会拒绝删除。

6、deleteall /path/znode 删除该节点的所有子节点包括自己

[zk: localhost:2181(CONNECTED) 26] deleteall /hello
[zk: localhost:2181(CONNECTED) 27] ls /
[admin, brokers, cluster, config, consumers, controller_epoch, feature, isr_change_notification, latest_producer_id_block, log_dir_event_notification, zookeeper]
[zk: localhost:2181(CONNECTED) 28] ls /hello
Node does not exist: /hello
[zk: localhost:2181(CONNECTED) 29]

7、getAllChildrenNumber 递归获取全部子节点包含子节点的子节点的所有数量

[zk: localhost:2181(CONNECTED) 1] getAllChildrenNumber /helo
8
[zk: localhost:2181(CONNECTED) 2] ls  /helo
[baobao, lichf, liubb, queue0000000004, queue1, world]
[zk: localhost:2181(CONNECTED) 3] ls  -R /helo
/helo
/helo/baobao
/helo/lichf
/helo/liubb
/helo/queue0000000004
/helo/queue1
/helo/world
/helo/lichf/baobao
/helo/queue1/haha
[zk: localhost:2181(CONNECTED) 4]

8、getEphemerals path 获取所有临时节点

[zk: localhost:2181(CONNECTED) 7] create -e /helo/test1
Created /helo/test1
[zk: localhost:2181(CONNECTED) 10] create -e /helo/test2
Created /helo/test2
[zk: localhost:2181(CONNECTED) 11] create -e /helo/hhhh
Created /helo/hhhh
  • getEphemerals 获取所有临时节点
[zk: localhost:2181(CONNECTED) 12] getEphemerals
[/helo/hhhh, /helo/test2, /helo/test1]
  • getEphemerals /path 获取所有匹配上的临时节点
[zk: localhost:2181(CONNECTED) 13] getEphemerals  /helo/test
[/helo/test2, /helo/test1]
[zk: localhost:2181(CONNECTED) 14]
举报

相关推荐

0 条评论