ZooKeeper基本数据模型
3.1 基本数据模型介绍
- zk的数据模型也可以理解为linux/unix的文件目录: /usr/local/...
- 每一个节点都称之为znode, 它可以有子节点, 也可以有数据
- 每个节点分为临时节点和永久节点,临时节点在客户端断开后消失
- 每一个zk节点都有各自的版本号, 可以通过命令行来显示节点信息
- 每当节点数据发生变化, 那么该节点的版本号会累加(乐观锁)
- 删除/修改过时节点, 版本号不匹配会报错
- 每个zk节点存储的数据不宜过大, 几k即可
- 节点可以设置权限acl, 可以通过权限来限制用户的访问
3.2 ZooKeeper数据模型的基本操作
客户端连接
$ZOOKEEPER_HOME/bin 运行zkServer.sh start 开启zookeeper
进入客户端
./zkCli.sh 2018-12-29 02:23:22,904 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1235] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x167f60ba5ea0002, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:None path:null [zk: localhost:2181(CONNECTED) 0]
- ls 列出目录 help 查看帮助
WatchedEvent state:SyncConnected type:None path:null [zk: localhost:2181(CONNECTED) 0] ls / [zookeeper] [zk: localhost:2181(CONNECTED) 1] ls /zookeeper [quota] [zk: localhost:2181(CONNECTED) 2] ls /zookeeper/quota [] [zk: localhost:2181(CONNECTED) 3] help 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
查看znode结构
ls 列出目录 help 查看帮助
WatchedEvent state:SyncConnected type:None path:null [zk: localhost:2181(CONNECTED) 0] ls / [zookeeper] [zk: localhost:2181(CONNECTED) 1] ls /zookeeper [quota] [zk: localhost:2181(CONNECTED) 2] ls /zookeeper/quota [] [zk: localhost:2181(CONNECTED) 3] help 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
关闭客户端连接 ctrl+c