前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Zookeeper Python调用

Zookeeper Python调用

作者头像
用户5760343
发布2019-10-29 14:45:56
6560
发布2019-10-29 14:45:56
举报
文章被收录于专栏:sktjsktj

安装:pip install kazoo 1、链接 from kazoo.client import KazooClient

zk = KazooClient(hosts='127.0.0.1:2181') zk.start()

zk.stop()

2、创建节点

Ensure a path, create if necessary

zk.ensure_path("/my/favorite")

Create a node with data

zk.create("/my/favorite/node", b"a value")

3、获取数据

Determine if a node exists

if zk.exists("/my/favorite"): # Do something

Print the version of a node and its data

data, stat = zk.get("/my/favorite") print("Version: %s, data: %s" % (stat.version, data.decode("utf-8")))

List the children

children = zk.get_children("/my/favorite") print("There are %s children with names %s" % (len(children), children))

4、更新数据 zk.set("/my/favorite", b"some data")

5、递归删除节点 zk.delete("/my/favorite/node", recursive=True)

6、监视节点变化 @zk.ChildrenWatch("/my/favorite/node") def watch_children(children): print("Children are now: %s" % children)

Above function called immediately, and from then on

@zk.DataWatch("/my/favorite") def watch_node(data, stat): print("Version: %s, data: %s" % (stat.version, data.decode("utf-8")))

7、监听事件:LOST/CONNECTED/SUSPENDED from kazoo.client import KazooState

def my_listener(state): if state == KazooState.LOST: # Register somewhere that the session was lost elif state == KazooState.SUSPENDED: # Handle being disconnected from Zookeeper else: # Handle being connected/reconnected to Zookeeper

zk.add_listener(my_listener)

8、kazoo支持异步 9、API DOCUMENT:https://kazoo.readthedocs.io/en/latest/api/client.html

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.10.25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Ensure a path, create if necessary
  • Create a node with data
  • Determine if a node exists
  • Print the version of a node and its data
  • List the children
  • Above function called immediately, and from then on
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档