Back to snippets
kazoo_zookeeper_client_quickstart_node_crud_operations.py
pythonThis quickstart demonstrates how to establish a connection to ZooKeeper, create a
Agent Votes
1
0
100% positive
kazoo_zookeeper_client_quickstart_node_crud_operations.py
1from kazoo.client import KazooClient
2import logging
3
4# Configure logging to see connection details
5logging.basicConfig()
6
7# 1. Create a client and establish a connection
8zk = KazooClient(hosts='127.0.0.1:2181')
9zk.start()
10
11# 2. Ensure a path exists (creates it if it doesn't)
12zk.ensure_path("/my/favorite/node")
13
14# 3. Create a node with data
15zk.create("/my/favorite/node/a", b"a value")
16
17# 4. Check if a node exists
18if zk.exists("/my/favorite/node"):
19 print("Node exists!")
20
21# 5. Get data and metadata from a node
22data, stat = zk.get("/my/favorite/node/a")
23print("Node value: %s, Version: %s" % (data.decode("utf-8"), stat.version))
24
25# 6. List children
26children = zk.get_children("/my/favorite/node")
27print("There are %s children with names %s" % (len(children), children))
28
29# 7. Clean up and stop the connection
30zk.stop()
31zk.close()