如何用python 操作zookeeper
ZooKeeper 是一個分布式的、開放源碼的分布式應(yīng)用程序協(xié)調(diào)服務(wù),是 Google 的 Chubby 一個開源的實(shí)現(xiàn),是 Hadoop 和 Hbase 的重要組件。它是一個為分布式應(yīng)用提供一致性服務(wù)的軟件,提供的功能包括:配置維護(hù)、域名服務(wù)、分布式同步、組服務(wù)等。ZooKeeper 支持大部分開發(fā)語言,除了某些特定的功能只支持 Java 和 C。python 通過 kazoo 可以實(shí)現(xiàn)操作 ZooKeeper 。
一、安裝這個簡單,使用 pip 命令安裝
pip3 install kazoo二、連接 ZooKeeper
可通過 KazooClient 類直接連接 ZooKeeper ,支持多個 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)對應(yīng)的值,注意值的類型是 bytes ephemeral: 若為 True 則創(chuàng)建一個臨時節(jié)點(diǎn),session 中斷后自動刪除該節(jié)點(diǎn)。默認(rèn) False sequence: 若為 True 則在你創(chuàng)建節(jié)點(diǎn)名后面增加10位數(shù)字(例如:你創(chuàng)建一個 testplatform/test 節(jié)點(diǎn),實(shí)際創(chuàng)建的是 testplatform/test0000000003,這串?dāng)?shù)字是順序遞增的)。默認(rèn) False makepath: 若為 False 父節(jié)點(diǎn)不存在時拋 NoNodeError。若為 True 父節(jié)點(diǎn)不存在則創(chuàng)建父節(jié)點(diǎn)。默認(rèn) False舉個例子:
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)對應(yīng)的值
from kazoo.client import KazooClientzk = KazooClient(hosts=’10.1.44.55’)zk.start()# 獲取某個節(jié)點(diǎn)下所有子節(jié)點(diǎn)node = zk.get_children(’/testplatform’)# 獲取某個節(jié)點(diǎn)對應(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)對應(yīng)的valuezk.set(’/testplatform/test’,b’this is not test’)# 獲取某個節(jié)點(diǎn)對應(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)對應(yīng)的valuezk.delete(’/testplatform/test’,recursive=False)zk.stop()
參數(shù) recursive:若為 False,當(dāng)需要刪除的節(jié)點(diǎn)存在子節(jié)點(diǎn),會拋異常 NotEmptyError 。若為True,則刪除 此節(jié)點(diǎn) 以及 刪除該節(jié)點(diǎn)的所有子節(jié)點(diǎn)
七、watches 事件zookeeper 所有讀操作都有設(shè)置 watch 選項(get_children() 、get() 和 exists())。watch 是一個觸發(fā)器,當(dāng)檢測到 zookeeper 有子節(jié)點(diǎn)變動 或者 節(jié)點(diǎn)value發(fā)生變動時觸發(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é),請參考 kazoo 官方文檔:https://kazoo.readthedocs.io/en/latest/api/client.html
以上就是如何用python 操作zookeeper的詳細(xì)內(nèi)容,更多關(guān)于python 操作zookeeper的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 怎樣才能用js生成xmldom對象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?2. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案3. jsp文件下載功能實(shí)現(xiàn)代碼4. CSS3中Transition屬性詳解以及示例分享5. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法6. ASP腳本組件實(shí)現(xiàn)服務(wù)器重啟7. css進(jìn)階學(xué)習(xí) 選擇符8. XHTML 1.0:標(biāo)記新的開端9. ASP基礎(chǔ)入門第三篇(ASP腳本基礎(chǔ))10. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera
