如何用python 操作zookeeper
ZooKeeper 是一個(gè)分布式的、開放源碼的分布式應(yīng)用程序協(xié)調(diào)服務(wù),是 Google 的 Chubby 一個(gè)開源的實(shí)現(xiàn),是 Hadoop 和 Hbase 的重要組件。它是一個(gè)為分布式應(yīng)用提供一致性服務(wù)的軟件,提供的功能包括:配置維護(hù)、域名服務(wù)、分布式同步、組服務(wù)等。ZooKeeper 支持大部分開發(fā)語(yǔ)言,除了某些特定的功能只支持 Java 和 C。python 通過 kazoo 可以實(shí)現(xiàn)操作 ZooKeeper 。
一、安裝這個(gè)簡(jiǎn)單,使用 pip 命令安裝
pip3 install kazoo二、連接 ZooKeeper
可通過 KazooClient 類直接連接 ZooKeeper ,支持多個(gè) host ,端口默認(rèn) 2181。
import jsonfrom kazoo.client import KazooClientzk = KazooClient(hosts=’10.1.44.55’)zk.start()三、創(chuàng)建節(jié)點(diǎn)
先看下 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é)點(diǎn)路徑 value: 節(jié)點(diǎn)對(duì)應(yīng)的值,注意值的類型是 bytes ephemeral: 若為 True 則創(chuàng)建一個(gè)臨時(shí)節(jié)點(diǎn),session 中斷后自動(dòng)刪除該節(jié)點(diǎn)。默認(rèn) False sequence: 若為 True 則在你創(chuàng)建節(jié)點(diǎn)名后面增加10位數(shù)字(例如:你創(chuàng)建一個(gè) testplatform/test 節(jié)點(diǎn),實(shí)際創(chuàng)建的是 testplatform/test0000000003,這串?dāng)?shù)字是順序遞增的)。默認(rèn) False makepath: 若為 False 父節(jié)點(diǎn)不存在時(shí)拋 NoNodeError。若為 True 父節(jié)點(diǎn)不存在則創(chuàng)建父節(jié)點(diǎn)。默認(rèn) False舉個(gè)例子:
from kazoo.client import KazooClientzk = KazooClient(hosts=’10.1.44.55’)zk.start()# 創(chuàng)建節(jié)點(diǎn):makepath 設(shè)置為 True ,父節(jié)點(diǎn)不存在則創(chuàng)建,其他參數(shù)不填均為默認(rèn)zk.create(’/testplatform/test’,b’this is test!’,makepath=True)# 操作完后,別忘了關(guān)閉zk連接zk.stop()print(value)四、查看節(jié)點(diǎn)
KazooClient 類用提供 get_children() 和 get() 方法獲取 子節(jié)點(diǎn) 和 節(jié)點(diǎn)對(duì)應(yīng)的值
from kazoo.client import KazooClientzk = KazooClient(hosts=’10.1.44.55’)zk.start()# 獲取某個(gè)節(jié)點(diǎn)下所有子節(jié)點(diǎn)node = zk.get_children(’/testplatform’)# 獲取某個(gè)節(jié)點(diǎn)對(duì)應(yīng)的值value = zk.get(’/testplatform/mssql’)# 操作完后,別忘了關(guān)閉zk連接zk.stop()print(node,value) 五、更改節(jié)點(diǎn)
更改上文創(chuàng)建的 node 值,使用 set() 方法
from kazoo.client import KazooClientzk = KazooClient(hosts=’10.1.44.55’)zk.start()# 更改節(jié)點(diǎn)對(duì)應(yīng)的valuezk.set(’/testplatform/test’,b’this is not test’)# 獲取某個(gè)節(jié)點(diǎn)對(duì)應(yīng)的值value = zk.get(’/testplatform/test’)zk.stop()print(value) 六、刪除節(jié)點(diǎn)
刪除上文創(chuàng)建的節(jié)點(diǎn),使用 delete() 方法
from kazoo.client import KazooClientzk = KazooClient(hosts=’10.1.44.55’)zk.start()# 刪除節(jié)點(diǎn)對(duì)應(yīng)的valuezk.delete(’/testplatform/test’,recursive=False)zk.stop()
參數(shù) recursive:若為 False,當(dāng)需要?jiǎng)h除的節(jié)點(diǎn)存在子節(jié)點(diǎn),會(huì)拋異常 NotEmptyError 。若為True,則刪除 此節(jié)點(diǎn) 以及 刪除該節(jié)點(diǎn)的所有子節(jié)點(diǎn)
七、watches 事件zookeeper 所有讀操作都有設(shè)置 watch 選項(xiàng)(get_children() 、get() 和 exists())。watch 是一個(gè)觸發(fā)器,當(dāng)檢測(cè)到 zookeeper 有子節(jié)點(diǎn)變動(dòng) 或者 節(jié)點(diǎn)value發(fā)生變動(dòng)時(shí)觸發(fā)。下面以 get() 方法為例。
from kazoo.client import KazooClientzk = 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é),請(qǐng)參考 kazoo 官方文檔:https://kazoo.readthedocs.io/en/latest/api/client.html
以上就是如何用python 操作zookeeper的詳細(xì)內(nèi)容,更多關(guān)于python 操作zookeeper的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Python基于requests庫(kù)爬取網(wǎng)站信息2. vscode運(yùn)行php報(bào)錯(cuò)php?not?found解決辦法3. Python使用Selenium自動(dòng)進(jìn)行百度搜索的實(shí)現(xiàn)4. Java commons-httpclient如果實(shí)現(xiàn)get及post請(qǐng)求5. 一文帶你徹底理解Java序列化和反序列化6. 微信小程序?qū)崿F(xiàn)商品分類頁(yè)過程結(jié)束7. PHP laravel實(shí)現(xiàn)導(dǎo)出PDF功能8. JS中6個(gè)對(duì)象數(shù)組去重的方法9. 資深程序員:給Python軟件開發(fā)測(cè)試的25個(gè)忠告!10. python中文本字符處理的簡(jiǎn)單方法記錄

網(wǎng)公網(wǎng)安備