您的位置:首頁技術文章
文章詳情頁

如何用python 操作zookeeper

瀏覽:45日期:2022-07-01 08:51:24
ZooKeeper 簡介

ZooKeeper 是一個分布式的、開放源碼的分布式應用程序協調服務,是 Google 的 Chubby 一個開源的實現,是 Hadoop 和 Hbase 的重要組件。它是一個為分布式應用提供一致性服務的軟件,提供的功能包括:配置維護、域名服務、分布式同步、組服務等。ZooKeeper 支持大部分開發語言,除了某些特定的功能只支持 Java 和 C。python 通過 kazoo 可以實現操作 ZooKeeper 。

一、安裝

這個簡單,使用 pip 命令安裝

pip3 install kazoo二、連接 ZooKeeper

可通過 KazooClient 類直接連接 ZooKeeper ,支持多個 host ,端口默認 2181。

import jsonfrom kazoo.client import KazooClientzk = KazooClient(hosts=’10.1.44.55’)zk.start()三、創建節點

先看下 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.

我們來解釋下這些參數:

path: 節點路徑 value: 節點對應的值,注意值的類型是 bytes ephemeral: 若為 True 則創建一個臨時節點,session 中斷后自動刪除該節點。默認 False sequence: 若為 True 則在你創建節點名后面增加10位數字(例如:你創建一個 testplatform/test 節點,實際創建的是 testplatform/test0000000003,這串數字是順序遞增的)。默認 False makepath: 若為 False 父節點不存在時拋 NoNodeError。若為 True 父節點不存在則創建父節點。默認 False

舉個例子:

from kazoo.client import KazooClientzk = KazooClient(hosts=’10.1.44.55’)zk.start()# 創建節點:makepath 設置為 True ,父節點不存在則創建,其他參數不填均為默認zk.create(’/testplatform/test’,b’this is test!’,makepath=True)# 操作完后,別忘了關閉zk連接zk.stop()print(value)四、查看節點

KazooClient 類用提供 get_children() 和 get() 方法獲取 子節點 和 節點對應的值

from kazoo.client import KazooClientzk = KazooClient(hosts=’10.1.44.55’)zk.start()# 獲取某個節點下所有子節點node = zk.get_children(’/testplatform’)# 獲取某個節點對應的值value = zk.get(’/testplatform/mssql’)# 操作完后,別忘了關閉zk連接zk.stop()print(node,value) 五、更改節點

更改上文創建的 node 值,使用 set() 方法

from kazoo.client import KazooClientzk = KazooClient(hosts=’10.1.44.55’)zk.start()# 更改節點對應的valuezk.set(’/testplatform/test’,b’this is not test’)# 獲取某個節點對應的值value = zk.get(’/testplatform/test’)zk.stop()print(value) 六、刪除節點

刪除上文創建的節點,使用 delete() 方法

from kazoo.client import KazooClientzk = KazooClient(hosts=’10.1.44.55’)zk.start()# 刪除節點對應的valuezk.delete(’/testplatform/test’,recursive=False)zk.stop()

參數 recursive:若為 False,當需要刪除的節點存在子節點,會拋異常 NotEmptyError 。若為True,則刪除 此節點 以及 刪除該節點的所有子節點

七、watches 事件

zookeeper 所有讀操作都有設置 watch 選項(get_children() 、get() 和 exists())。watch 是一個觸發器,當檢測到 zookeeper 有子節點變動 或者 節點value發生變動時觸發。下面以 get() 方法為例。

from kazoo.client import KazooClientzk = KazooClient(hosts=’10.1.44.55’)zk.start()def test(event): print(’觸發事件’)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#觸發事件#第二次獲取value

需要更多高階使用的同學,請參考 kazoo 官方文檔:https://kazoo.readthedocs.io/en/latest/api/client.html

以上就是如何用python 操作zookeeper的詳細內容,更多關于python 操作zookeeper的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
国产综合久久一区二区三区