Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common operations of ZooKeeper API

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about the common operation of ZooKeeper API, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

ZooKeeper is a distributed open source coordination service for distributed applications. It uses a set of simple operation primitives to enable distributed applications to achieve higher-level services-such as synchronization, configuration maintenance, groups and naming management. ZK has the characteristics of high performance, high availability (replication), order and so on.

1. Create

Class:org.apache.zookeeper.ZooKeeper

Public String create (String path, byte [] data, List acl, CreateMode createMode) throws KeeperException, InterruptedException

Public void create (String path, byte [] data, List acl, CreateMode createMode, AsyncCallback.StringCallback cb, Object ctx)

Create a node with a given path (path) and set up data (data) and access control list (acl) for it. The nodes in ZooKeeper are both "directory" and "regular file" relative to the directory structure in the file system. The second create method is the asynchronous version of create, and the asynchronous callback is called when the creation is complete.

(1)。 Org.apache.zookeeper.data.ACL

This excerpt talks about ACL in Zookeeper

ZooKeeper controls access to ZNode through ACL. The ZooKeeper client specifies the ACL list for the znode, and the ZooKeeper server determines whether a client requesting ZNode has the permission to operate based on the ACL list.

An ACL object consists of schema:ID and Permissions.

a)。 Scheme: scheme corresponds to which scheme is used for privilege management. Zookeeper implements an ACL scheme for pluggable, which can extend the mechanism of ACL by extending scheme. Zookeeper-3.4.4 supports the following scheme by default:

World: there is only one id under it, called anyone, and world:anyone represents anyone. The node in zookeeper that has permission for everyone belongs to world:anyone.

Auth: it does not require id, but has permissions as long as it is through the user of authentication (zookeeper supports authencation through kerberos, as well as authentication in the form of username/password)

Digest: its corresponding id is username:BASE64 (SHA1 (password)). It needs to go through authentication in the form of username:password first.

Ip: its corresponding id is the IP address of the client. You can set an ip segment, such as ip:192.168.1.0/16, to match the IP segment of the first 16 bit.

Super: in this case of scheme, the corresponding id has super permissions and can do anything (cdrwa)

b)。 Perm. There are five kinds of permissions in ZooKeeper. The Permissions of READ, WRITE, CREATE, DELETE and ADMIN,ACL from low to high can be one or more of the five permissions. They mean:

* READ: allows you to get the value of this node and list child nodes.

* WRITE: allows you to set the value of this node.

* CREATE: allows the creation of child nodes.

* DELETE: child nodes can be deleted.

* ADMIN: allow permissions to be set for this node.

(2)。 Org.apache.zookeeper.CreateMode

Org.apache.zookeeper.CreateMode can set whether znode is EPHEMERAL or SEQUENTIAL. You can have the following four values:

PERSISTENT persistence directory znode

PERSISTENT_SEQUENTIAL sequentially automatically numbered directory znode. This directory node is incremented according to the number of nodes that already exist.

The EPHEMERAL temporary directory znode, which is automatically deleted once the client and server that created the znode are disconnected. Temporary node (EPHEMERAL) cannot have child node data

EPHEMERAL_SEQUENTIAL temporarily automatically numbers znode.

(3)。 ZkCli command

The command encapsulation of create is implemented in zkCli, which can be used for user testing and data management:

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

"- s" means to create a sequentially automatically numbered node, and "- e" means to create a temporary node. The default is persistent node

For example:

Create a permanent node and a temporary node

Create / test nullCreated / testcreate-e / test0 nullCreated / test0

Create a sequentially auto-numbered node with ACL using digest (username: test password: debugo) and permissions of all (rwcda). About the generation of digest, you can refer to the DigestAuthenticationProvider.generateDigest (String ipName) method in zookeeper; by specifying the original user name and password to this method, you can get the string after "digest". For example, if you pass in "test:test", you will get "test:V28q/NynI4JI3Rk54h0r8O5kMug=". The internal principle is to MD5 + sha1 the "password" part.

Create-s / test0/test null digest:test:V28q/NynI4JI3Rk54h0r8O5kMug=:rwcdaEphemerals cannot have children: / test0/testcreate-s / test/test null digest:test:V28q/NynI4JI3Rk54h0r8O5kMug=:rwcdaCreated / test/test0000000000

Create a node whose ACL uses ip (172.19.17.0 Universe 24) to have read access only

Create / test/test1 "hello world" ip:172.19.17.0/24:rCreated / test/test1

2. Exist

Class:org.apache.zookeeper.ZooKeeper

Public Stat exists (String path, Watcher watcher) throws KeeperException, InterruptedException

Returns whether the znode of a path exists. And set whether to monitor this node (the second parameter, boolean watcher). When the second parameter is true and the statement executes successfully, the listener (watcher) is triggered when the successful execution of the create node / delete node / modify the node data.

Exists (String, Watcher)

Overloaded method, where you can specify a specific listener (watcher) object.

Exists (String, Watcher, AsyncCallback.StatCallback, Object)

Exists (String, boolean, AsyncCallback.StatCallback, Object)

Asynchronous implementation of exist

3. Delete

Public void delete (String path, int version) throws InterruptedException, KeeperException

Delete path with a znode,version of-1 to match any version, that is, delete all data from this node. In addition, an asynchronous version of delete also exists.

Delete (String path, int version, AsyncCallback.VoidCallback cb, Object ctx)

The asynchronous version of delete.

For example, delete a node in zkCli:

Delete / test/test1

4. GetChildren

Public List getChildren (String path, boolean watch) throws KeeperException, InterruptedException

Get all the child znode under the specified path. This method, like exist, can also set the watcher/ to specify a specific Watcher object.

5. SetData & getData

Stat setData (String path, byte [] data, int version)

You can specify the version number of the data that sets the data for the path when the node of the given path exists. If version is-1, any version can be matched.

Void setData (String path, byte [] data, int version, AsyncCallback.StatCallback cb, Object ctx)

The asynchronous version of setData.

Byte [] getData (String path, Watcher watcher, Stat stat)

Get the data of the znode node corresponding to this path, and the data version and other information can be specified through stat.

Void getData (String path, Watcher watcher, AsyncCallback.DataCallback cb, Object ctx)

The asynchronous version of getData.

6. SetACL and getACL

Stat setACL (String path, List acl, int version)

To reset the access rights to a znode node, it should be noted that the permissions of the directory nodes in the ZooKeeper are not transitive, and the permissions of the parent znode node cannot be passed to the child directory nodes. The ACL setting method has been introduced in create, and you can set a series of ACL rules (that is, specify a series of ACL objects).

Void setACL (String path, List acl, int version, AsyncCallback.StatCallback cb, Object ctx)

Asynchronous version of setACL

List getACL (String path, Stat stat)

Returns a list of ACL objects for a znode node.

Void getACL (String path, Stat stat, AsyncCallback.ACLCallback cb, Object ctx)

Asynchronous version of getACL

For example, set an ACL rule in zkCli:

[zk: localhost:2181 (CONNECTED) 43] setAcl / test world:anyone:rcZxid = 0xf000500edctime = Wed Sep 24 15:13:29 CST 2014. [zk: localhost:2181 (CONNECTED) 44] getAcl / test'world,'anyone: r after reading the above, do you have any further understanding of the common operation of API in ZooKeeper? if you want more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report