如何用python 操作zookeeper
ZooKeeper 是一個分布式的、開放源碼的分布式應用程序協(xié)調(diào)服務,是 Google 的 Chubby 一個開源的實現(xiàn),是 Hadoop 和 Hbase 的重要組件。它是一個為分布式應用提供一致性服務的軟件,提供的功能包括:配置維護、域名服務、分布式同步、組服務等。ZooKeeper 支持大部分開發(fā)語言,除了某些特定的功能只支持 Java 和 C。python 通過 kazoo 可以實現(xiàn)操作 ZooKeeper 。
一、安裝這個簡單,使用 pip 命令安裝
pip3 install kazoo二、連接 ZooKeeper
可通過 KazooClient 類直接連接 ZooKeeper ,支持多個 host ,端口默認 2181。
import jsonfrom kazoo.client import KazooClientzk = 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é)點對應的值,注意值的類型是 bytes ephemeral: 若為 True 則創(chuàng)建一個臨時節(jié)點,session 中斷后自動刪除該節(jié)點。默認 False sequence: 若為 True 則在你創(chuàng)建節(jié)點名后面增加10位數(shù)字(例如:你創(chuàng)建一個 testplatform/test 節(jié)點,實際創(chuàng)建的是 testplatform/test0000000003,這串數(shù)字是順序遞增的)。默認 False makepath: 若為 False 父節(jié)點不存在時拋 NoNodeError。若為 True 父節(jié)點不存在則創(chuàng)建父節(jié)點。默認 False舉個例子:
from kazoo.client import KazooClientzk = KazooClient(hosts=’10.1.44.55’)zk.start()# 創(chuàng)建節(jié)點:makepath 設置為 True ,父節(jié)點不存在則創(chuàng)建,其他參數(shù)不填均為默認zk.create(’/testplatform/test’,b’this is test!’,makepath=True)# 操作完后,別忘了關閉zk連接zk.stop()print(value)四、查看節(jié)點
KazooClient 類用提供 get_children() 和 get() 方法獲取 子節(jié)點 和 節(jié)點對應的值
from kazoo.client import KazooClientzk = KazooClient(hosts=’10.1.44.55’)zk.start()# 獲取某個節(jié)點下所有子節(jié)點node = zk.get_children(’/testplatform’)# 獲取某個節(jié)點對應的值value = zk.get(’/testplatform/mssql’)# 操作完后,別忘了關閉zk連接zk.stop()print(node,value) 五、更改節(jié)點
更改上文創(chuàng)建的 node 值,使用 set() 方法
from kazoo.client import KazooClientzk = KazooClient(hosts=’10.1.44.55’)zk.start()# 更改節(jié)點對應的valuezk.set(’/testplatform/test’,b’this is not test’)# 獲取某個節(jié)點對應的值value = zk.get(’/testplatform/test’)zk.stop()print(value) 六、刪除節(jié)點
刪除上文創(chuàng)建的節(jié)點,使用 delete() 方法
from kazoo.client import KazooClientzk = KazooClient(hosts=’10.1.44.55’)zk.start()# 刪除節(jié)點對應的valuezk.delete(’/testplatform/test’,recursive=False)zk.stop()
參數(shù) recursive:若為 False,當需要刪除的節(jié)點存在子節(jié)點,會拋異常 NotEmptyError 。若為True,則刪除 此節(jié)點 以及 刪除該節(jié)點的所有子節(jié)點
七、watches 事件zookeeper 所有讀操作都有設置 watch 選項(get_children() 、get() 和 exists())。watch 是一個觸發(fā)器,當檢測到 zookeeper 有子節(jié)點變動 或者 節(jié)點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
需要更多高階使用的同學,請參考 kazoo 官方文檔:https://kazoo.readthedocs.io/en/latest/api/client.html
以上就是如何用python 操作zookeeper的詳細內(nèi)容,更多關于python 操作zookeeper的資料請關注好吧啦網(wǎng)其它相關文章!
相關文章:
1. CSS3中Transition屬性詳解以及示例分享2. ASP基礎入門第八篇(ASP內(nèi)建對象Application和Session)3. jsp文件下載功能實現(xiàn)代碼4. XMLHTTP資料5. asp.net core項目授權流程詳解6. html中的form不提交(排除)某些input 原創(chuàng)7. ASP常用日期格式化函數(shù) FormatDate()8. CSS3實現(xiàn)動態(tài)翻牌效果 仿百度貼吧3D翻牌一次動畫特效9. ASP動態(tài)網(wǎng)頁制作技術經(jīng)驗分享10. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法
