如何用python 操作zookeeper
ZooKeeper 簡介
ZooKeeper 是一個分布式的、開放源碼的分布式應(yīng)用程序協(xié)調(diào)服務(wù),是 Google 的 Chubby 一個開源的實現(xiàn),是 Hadoop 和 Hbase 的重要組件。它是一個為分布式應(yīng)用提供一致性服務(wù)的軟件,提供的功能包括:配置維護(hù)、域名服務(wù)、分布式同步、組服務(wù)等。ZooKeeper 支持大部分開發(fā)語言,除了某些特定的功能只支持 Java 和 C。python 通過 kazoo 可以實現(xiàn)操作 ZooKeeper 。
一、安裝
這個簡單,使用 pip 命令安裝
pip3 install kazoo
二、連接 ZooKeeper
可通過 KazooClient 類直接連接 ZooKeeper ,支持多個 host ,端口默認(rèn) 2181。
import json from kazoo.client import KazooClient zk = KazooClient(hosts='10.1.44.55') zk.start()
三、創(chuàng)建節(jié)點
先看下 create() 方法定義
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.
我們來解釋下這些參數(shù):
- path: 節(jié)點路徑
- value: 節(jié)點對應(yīng)的值,注意值的類型是 bytes
- ephemeral: 若為 True 則創(chuàng)建一個臨時節(jié)點,session 中斷后自動刪除該節(jié)點。默認(rèn) False
- sequence: 若為 True 則在你創(chuàng)建節(jié)點名后面增加10位數(shù)字(例如:你創(chuàng)建一個 testplatform/test 節(jié)點,實際創(chuàng)建的是 testplatform/test0000000003,這串?dāng)?shù)字是順序遞增的)。默認(rèn) False
- makepath: 若為 False 父節(jié)點不存在時拋 NoNodeError。若為 True 父節(jié)點不存在則創(chuàng)建父節(jié)點。默認(rèn) False
舉個例子:
from kazoo.client import KazooClient
zk = KazooClient(hosts='10.1.44.55')
zk.start()
# 創(chuàng)建節(jié)點:makepath 設(shè)置為 True ,父節(jié)點不存在則創(chuàng)建,其他參數(shù)不填均為默認(rèn)
zk.create('/testplatform/test',b'this is test!',makepath=True)
# 操作完后,別忘了關(guān)閉zk連接
zk.stop()
print(value)
四、查看節(jié)點
KazooClient 類用提供 get_children() 和 get() 方法獲取 子節(jié)點 和 節(jié)點對應(yīng)的值
from kazoo.client import KazooClient
zk = KazooClient(hosts='10.1.44.55')
zk.start()
# 獲取某個節(jié)點下所有子節(jié)點
node = zk.get_children('/testplatform')
# 獲取某個節(jié)點對應(yīng)的值
value = zk.get('/testplatform/mssql')
# 操作完后,別忘了關(guān)閉zk連接
zk.stop()
print(node,value)
五、更改節(jié)點
更改上文創(chuàng)建的 node 值,使用 set() 方法
from kazoo.client import KazooClient
zk = KazooClient(hosts='10.1.44.55')
zk.start()
# 更改節(jié)點對應(yīng)的value
zk.set('/testplatform/test',b'this is not test')
# 獲取某個節(jié)點對應(yīng)的值
value = zk.get('/testplatform/test')
zk.stop()
print(value)
六、刪除節(jié)點
刪除上文創(chuàng)建的節(jié)點,使用 delete() 方法
from kazoo.client import KazooClient
zk = KazooClient(hosts='10.1.44.55')
zk.start()
# 刪除節(jié)點對應(yīng)的value
zk.delete('/testplatform/test',recursive=False)
zk.stop()
參數(shù) recursive:若為 False,當(dāng)需要刪除的節(jié)點存在子節(jié)點,會拋異常 NotEmptyError 。若為True,則刪除 此節(jié)點 以及 刪除該節(jié)點的所有子節(jié)點
七、watches 事件
zookeeper 所有讀操作都有設(shè)置 watch 選項(get_children() 、get() 和 exists())。watch 是一個觸發(fā)器,當(dāng)檢測到 zookeeper 有子節(jié)點變動 或者 節(jié)點value發(fā)生變動時觸發(fā)。下面以 get() 方法為例。
from kazoo.client import KazooClient
zk = KazooClient(hosts='10.1.44.55')
zk.start()
def test(event):
print('觸發(fā)事件')
if __name__ == "__main__":
zk.get('/testplatform/test',watch = test)
print("第一次獲取value")
zk.set('/testplatform/test',b'hello')
zk.get('/testplatform/test',watch = test)
print("第二次獲取value")
# 輸出
#第一次獲取value
#觸發(fā)事件
#第二次獲取value
需要更多高階使用的同學(xué),請參考 kazoo 官方文檔:https://kazoo.readthedocs.io/en/latest/api/client.html
以上就是如何用python 操作zookeeper的詳細(xì)內(nèi)容,更多關(guān)于python 操作zookeeper的資料請關(guān)注腳本之家其它相關(guān)文章!
- SpringCloud用Zookeeper搭建配置中心的方法
- springcloud集成zookeeper的方法示例
- SpringBoot+Dubbo+Zookeeper實現(xiàn)簡單分布式開發(fā)的應(yīng)用詳解
- 關(guān)于idea+centos7+zookeeper報錯connectionloss,timeout問題
- Zookeeper全局唯一ID生成方案解析
- SpringBoot中dubbo+zookeeper實現(xiàn)分布式開發(fā)的應(yīng)用詳解
- 基于Spring Cloud Zookeeper實現(xiàn)服務(wù)注冊與發(fā)現(xiàn)
- 2020最新IDEA SpringBoot整合Dubbo的實現(xiàn)(zookeeper版)
- SpringBoot系列教程之dubbo和Zookeeper集成方法
- Python通過zookeeper實現(xiàn)分布式服務(wù)代碼解析
- 在Java中操作Zookeeper的示例代碼詳解
- SpringCloud使用Zookeeper作為注冊中心
相關(guān)文章
詳解PyTorch手寫數(shù)字識別(MNIST數(shù)據(jù)集)
這篇文章主要介紹了詳解PyTorch手寫數(shù)字識別(MNIST數(shù)據(jù)集),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Python編程深度學(xué)習(xí)繪圖庫之matplotlib
今天小編就為大家分享一篇關(guān)于Python編程深度學(xué)習(xí)繪圖庫之matplotlib,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
Python函數(shù)式編程中itertools模塊詳解
這篇文章主要介紹了在Python中使用itertools模塊中的組合函數(shù)的教程,來自IBM官方技術(shù)文檔,需要的朋友可以參考下,希望能夠給你帶來幫助2021-09-09
用python按照圖像灰度值統(tǒng)計并篩選圖片的操作(PIL,shutil,os)
這篇文章主要介紹了用python按照圖像灰度值統(tǒng)計并篩選圖片的操作(PIL,shutil,os),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Python3的高階函數(shù)map,reduce,filter的示例詳解
這篇文章主要介紹了Python3的高階函數(shù)map,reduce,filter的示例代碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07

