How to operate zookeeper with python
- 2021-09-05 00:24:29
- OfStack
Introduction to ZooKeeper
ZooKeeper is a distributed, open source distributed application coordination service, an open source implementation of Chubby of Google, and an important component of Hadoop and Hbase. It is a software that provides one-dimensional service for distributed applications, and its functions include: configuration maintenance, domain name service, distributed synchronization, group service and so on. ZooKeeper supports most development languages, except for certain features that only support Java and C. python can operate ZooKeeper through kazoo.
Step 1 Install
This is simple and installed using the pip command
pip3 install kazoo
2. Connect ZooKeeper
You can connect ZooKeeper directly through KazooClient class, support multiple host, and the default port is 2181.
import json
from kazoo.client import KazooClient
zk = KazooClient(hosts='10.1.44.55')
zk.start()
3. Create a node
Let's look at the create () method definition first
def create(self, path, value=b"", acl=None, ephemeral=False,
sequence=False, makepath=False):
:param path: Path of node.
:param value: Initial bytes value of node.
:param acl: :class:`~kazoo.security.ACL` list.
:param ephemeral: Boolean indicating whether node is ephemeral
(tied to this session).
:param sequence: Boolean indicating whether path is suffixed
with a unique index.
:param makepath: Whether the path should be created if it
doesn't exist.
Let's explain these parameters:
path: Node path value: The value corresponding to the node. Note that the type of the value is bytes ephemeral: If it is True, a temporary node will be created, which will be automatically deleted after session interruption. Default False sequence: If it is True, add 10 digits after the node name you created (for example, if you create an testplatform/test node, you actually create testplatform/test0000000003, and this string of digits increases in sequence). Default False makepath: NoNodeError is thrown if the parent node of False does not exist. If True parent node does not exist, the parent node is created. Default FalseFor example:
from kazoo.client import KazooClient
zk = KazooClient(hosts='10.1.44.55')
zk.start()
# Create a node: makepath Set to True If the parent node does not exist, it will be created. If other parameters are not filled in, they will be the default
zk.create('/testplatform/test',b'this is test ! ',makepath=True)
# After the operation, don't forget to close zk Connect
zk.stop()
print(value)
4. View the nodes
The KazooClient class provides get_children () and get () methods to obtain values for child nodes and nodes
from kazoo.client import KazooClient
zk = KazooClient(hosts='10.1.44.55')
zk.start()
# Get all child nodes under a node
node = zk.get_children('/testplatform')
# Gets the value corresponding to a node
value = zk.get('/testplatform/mssql')
# After the operation, don't forget to close zk Connect
zk.stop()
print(node,value)
5. Change nodes
Change the node value created above and use the set () method
from kazoo.client import KazooClient
zk = KazooClient(hosts='10.1.44.55')
zk.start()
# Change the corresponding to the node value
zk.set('/testplatform/test',b'this is not test')
# Gets the value corresponding to a node
value = zk.get('/testplatform/test')
zk.stop()
print(value)
6. Delete Node
Delete the node created above, using the delete () method
from kazoo.client import KazooClient
zk = KazooClient(hosts='10.1.44.55')
zk.start()
# Delete the corresponding node value
zk.delete('/testplatform/test',recursive=False)
zk.stop()
Parameter recursive: If it is False, an exception NotEmptyError will be thrown when there are child nodes in the node to be deleted. If it is True, delete this node and delete all child nodes of this node
7. watches incident
zookeeper All read operations have watch setting options (get_children (), get (), and exists ()). watch is a trigger, which is triggered when it is detected that zookeeper has a child node change or node value changes. Let's take the get () method as an example.
from kazoo.client import KazooClient
zk = KazooClient(hosts='10.1.44.55')
zk.start()
def test(event):
print(' Trigger event ')
if __name__ == "__main__":
zk.get('/testplatform/test',watch = test)
print(" No. 1 1 Secondary acquisition value")
zk.set('/testplatform/test',b'hello')
zk.get('/testplatform/test',watch = test)
print(" No. 1 2 Secondary acquisition value")
# Output
# No. 1 1 Secondary acquisition value
# Trigger event
# No. 1 2 Secondary acquisition value
For students who need more advanced use, please refer to the official document of https: https://kazoo.readthedocs.io/en/latest/api/client.html
The above is how to use python to operate zookeeper in detail, more information about python operating zookeeper please pay attention to other related articles on this site!