zookeeper数据模型

2018-11-05  本文已影响8人  nextliving

在上一篇mac使用homebrew安装zookeeper中已经安装了zookeeper。本文接着讲述zookeeper的数据模型。

启动zookeeper并查看节点状态

首先使用$ zkServer start启动zookeeper,正常启动终端输出应该是:

ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Starting zookeeper ... STARTED

然后重新打开一个终端,模拟客户端连接zookeeper,连接的命令为$ zkCli,连接成功以后输出:

Connecting to localhost:2181
Welcome to ZooKeeper!
JLine support is enabled
[zk: localhost:2181(CONNECTING) 0] 
WATCHER::

WatchedEvent state:SyncConnected type:None path:null

可以使用$ ls /查看根节点/下已经注册的节点:

[zookeeper]

说明当前根目录下只有一个节点zookeeper。
然后使用命令$ get /查看根节点/的状态,终端输出

cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x112
cversion = 3
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1

zookeeper数据模型

以上部分查看了zookeep中的节点。那么到底什么是节点呢?zookeeper官网的The ZooKeeper Data Model是这样描述的:

ZooKeeper has a hierarchal name space, much like a distributed file system. The only difference is that each node in the namespace can have data associated with it as well as children. It is like having a file system that allows a file to also be a directory. Paths to nodes are always expressed as canonical, absolute, slash-separated paths; there are no relative reference.

也就是说,zookeeper有一个结构化层次化的命名空间,其实也就是一个路径结构,以/开始,命名空间中的每一个路径就是一个节点(node),一般也会被称作znode。并且每个路径都是绝对路径,没有相对路径。开发者不能使用zookeeper作为路径名,该路径名被zookeeper自身使用。

节点类型

zookeeper节点类型分为4种:

节点状态字段含义

节点状态包含下列字段:

znode数据大小和格式

znode的数据部分默认只能存储1MB数据,并且只能存储字节数据,任何数据(图像、json、语音等)都需要先序列化成字节数组才能保存到znode中,参考Can we give the path of text files as data in zookeeper znodes?How to store a list of strings in a single ZooKeeper znode using Curator

参考

上一篇下一篇

猜你喜欢

热点阅读