python 访问 zookeeper

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 命令不多,也比较简单,这里不一一测试。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章