python 訪問 zookeeper
1.安裝:
zookeeper python客戶端依賴c客戶端,所以要先安裝c版本客戶端
> wget -c http://apache.fayea.com/zookeeper/zookeeper-3.4.10/zookeeper-3.4.12.tar.gz
> tar -zxvf zookeeper-3.4.12.tar.gz
> cd zookeeper-3.4.12/src/c
> ./configure
> make
> make install
安裝支持包: pip install zkpython
2.測試
[root@otter c]# python
Python 2.7.5 (default, Oct 30 2018, 23:45:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import zookeeper
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.linux-x86_64/egg/zookeeper.py", line 7, in <module>
File "build/bdist.linux-x86_64/egg/zookeeper.py", line 6, in __bootstrap__
ImportError: libzookeeper_mt.so.2: cannot open shared object file: No such file or directory
>>>
解決方法:
將下面這句加入到 /etc/profile 最後一行(libzookeeper_mt.so在/usr/local/lib下面):
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
(在crontab 中添加 自動作業,也需要在sh 文件中添加此行腳本,不然不會執行)
然後執行:
$ . /etc/profile
import zookeeper
hader=zookeeper.init("localhost:2181") #登錄到客戶端
channels=zookeeper.get_children(hader,'/otter/channel') #獲取目錄
stat=zookeeper.get(hader,'/otter/channel/1') #獲取節點信息
python3 安裝zookeeper
>>> import zookeeper
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /usr/local/lib/python3.6/site-packages/zkpython-0.4.2-py3.6-linux-x86_64.egg/zookeeper.cpython-36m-x86_64-linux-gnu.so: undefined symbol: PyInt_AsLong
>>> quit()
解決方法:
用 zookeeper安裝包 的 zookeeper.c 文件替換掉 zkpython 的包文件 zookeeper.c
[root@otter c]# cp /opt/download/zookeeper-3.4.12/src/contrib/zkpython/src/c/zookeeper.c /opt/download/zkpython-0.4.2/zookeeper.c
然後進入zkpythton目錄
python setup.py install 進行安裝
之後import zookeeper測試通過
[root@otter zookeeper-3.4.12]# python3
Python 3.6.0a1 (default, Apr 15 2019, 14:50:24)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import zookeeper
>>>
示例代碼:(列出 /otter/channel ,並查詢其狀態)
hader=zookeeper.init("localhost:2181")
channels=zookeeper.get_children(hader,'/otter/channel')
for temp in channels:
stat=zookeeper.get(hader,'/otter/channel/'+temp)
zookeeper 命令不多,也比較簡單,這裏不一一測試。