Zookeeper 简明教程
Zookeeper - CLI
ZooKeeper命令行界面(CLI)用于出于开发目的与ZooKeeper组合进行交互。这对于调试和与不同选项协同工作很有用。
ZooKeeper Command Line Interface (CLI) is used to interact with the ZooKeeper ensemble for development purpose. It is useful for debugging and working around with different options.
若要执行ZooKeeper CLI操作,请首先启用ZooKeeper服务器(“bin/zkServer.sh start”),然后启用ZooKeeper客户端(“bin/zkCli.sh”)。客户端启动后,你可以执行以下操作−
To perform ZooKeeper CLI operations, first turn on your ZooKeeper server (“bin/zkServer.sh start”) and then, ZooKeeper client (“bin/zkCli.sh”). Once the client starts, you can perform the following operation −
-
Create znodes
-
Get data
-
Watch znode for changes
-
Set data
-
Create children of a znode
-
List children of a znode
-
Check Status
-
Remove / Delete a znode
现在让我们逐个查看以上命令,并使用一个示例。
Now let us see above command one by one with an example.
Create Znodes
使用给定的路径创建一个znode。 flag 参数指定创建的znode为临时性、持久性还是连续性。默认情况下,所有znode都是持久的。
Create a znode with the given path. The flag argument specifies whether the created znode will be ephemeral, persistent, or sequential. By default, all znodes are persistent.
-
Ephemeral znodes (flag: e) will be automatically deleted when a session expires or when the client disconnects.
-
Sequential znodes guaranty that the znode path will be unique.
-
ZooKeeper ensemble will add sequence number along with 10 digit padding to the znode path. For example, the znode path /myapp will be converted to /myapp0000000001 and the next sequence number will be /myapp0000000002. If no flags are specified, then the znode is considered as persistent.
Output
[zk: localhost:2181(CONNECTED) 0] create /FirstZnode “Myfirstzookeeper-app”
Created /FirstZnode
要创建一个 Sequential znode ,添加 -s flag ,如下所示。
To create a Sequential znode, add -s flag as shown below.
Output
[zk: localhost:2181(CONNECTED) 2] create -s /FirstZnode “second-data”
Created /FirstZnode0000000023
要创建一个 Ephemeral Znode ,添加 -e flag ,如下所示。
To create an Ephemeral Znode, add -e flag as shown below.
Output
[zk: localhost:2181(CONNECTED) 2] create -e /SecondZnode “Ephemeral-data”
Created /SecondZnode
请记住,当客户端连接丢失时,短暂节点将被删除。你可以尝试退出 ZooKeeper CLI,然后重新打开 CLI,来进行验证。
Remember when a client connection is lost, the ephemeral znode will be deleted. You can try it by quitting the ZooKeeper CLI and then re-opening the CLI.
Get Data
它返回与指定节点相关联的数据和元数据。你将获得一些信息,例如数据最后一次修改的时间、以及修改的地点,和有关数据的信息。此 CLI 还用于分配监视,以显示有关数据的通知。
It returns the associated data of the znode and metadata of the specified znode. You will get information such as when the data was last modified, where it was modified, and information about the data. This CLI is also used to assign watches to show notification about the data.
Output
[zk: localhost:2181(CONNECTED) 1] get /FirstZnode
“Myfirstzookeeper-app”
cZxid = 0x7f
ctime = Tue Sep 29 16:15:47 IST 2015
mZxid = 0x7f
mtime = Tue Sep 29 16:15:47 IST 2015
pZxid = 0x7f
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 22
numChildren = 0
要访问序列节点,你必须输入节点的完整路径。
To access a sequential znode, you must enter the full path of the znode.
Watch
监视在指定节点或节点的子节点数据发生更改时显示通知。你只能在 get 命令中设置 watch 。
Watches show a notification when the specified znode or znode’s children data changes. You can set a watch only in get command.
Output
[zk: localhost:2181(CONNECTED) 1] get /FirstZnode 1
“Myfirstzookeeper-app”
cZxid = 0x7f
ctime = Tue Sep 29 16:15:47 IST 2015
mZxid = 0x7f
mtime = Tue Sep 29 16:15:47 IST 2015
pZxid = 0x7f
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 22
numChildren = 0
输出类似于正常的 get 命令,但它将在后台等待节点更改。<从这里开始>
The output is similar to normal get command, but it will wait for znode changes in the background. <Start here>
Set Data
设置指定节点的数据。完成此设置操作后,你可以使用 get CLI 命令检查数据。
Set the data of the specified znode. Once you finish this set operation, you can check the data using the get CLI command.
Output
[zk: localhost:2181(CONNECTED) 1] get /SecondZnode “Data-updated”
cZxid = 0x82
ctime = Tue Sep 29 16:29:50 IST 2015
mZxid = 0x83
mtime = Tue Sep 29 16:29:50 IST 2015
pZxid = 0x82
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x15018b47db00000
dataLength = 14
numChildren = 0
如果你在 get 命令中分配了 children 选项(如前一个命令中所示),则输出将类似于如下所示:
If you assigned watch option in get command (as in previous command), then the output will be similar as shown below −
Output
[zk: localhost:2181(CONNECTED) 1] get /FirstZnode “Mysecondzookeeper-app”
WATCHER: :
WatchedEvent state:SyncConnected type:NodeDataChanged path:/FirstZnode
cZxid = 0x7f
ctime = Tue Sep 29 16:15:47 IST 2015
mZxid = 0x84
mtime = Tue Sep 29 17:14:47 IST 2015
pZxid = 0x7f
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 23
numChildren = 0
Create Children / Sub-znode
创建子节点类似于创建新节点。唯一不同的是子节点的路径也包含父路径。
Creating children is similar to creating new znodes. The only difference is that the path of the child znode will have the parent path as well.
List Children
此命令用于列出和显示节点的 children 。
This command is used to list and display the children of a znode.
Check Status
Status 描述指定节点的元数据。它包含诸如时间戳、版本号、ACL、数据长度和子节点等详细信息。
Status describes the metadata of a specified znode. It contains details such as Timestamp, Version number, ACL, Data length, and Children znode.
Remove a Znode
删除指定节点及其所有子节点。仅当此类节点可用时才会执行此操作。
Removes a specified znode and recursively all its children. This would happen only if such a znode is available.
Output
[zk: localhost:2181(CONNECTED) 10] rmr /FirstZnode
[zk: localhost:2181(CONNECTED) 11] get /FirstZnode
Node does not exist: /FirstZnode
删除 (delete /path) 命令类似于 remove 命令,不同之处在于它仅适用于没有子节点的节点。
Delete (delete /path) command is similar to remove command, except the fact that it works only on znodes with no children.