Python ZKPython 安装
需求:使用Python操控ZooKeeper
1.由于python客户端依赖c的客户端所以要先安装c版本的客户端
cd zookeeper-3.4.5/src/c
./configure
make
make install
2.测试c版本客户端,需要gcc和python-devel
./cli_mt localhost:2181
Watcher SESSION_EVENT state = CONNECTED_STATE
Got a new session id: 0x23f9d77d3fe0001
ls /
time = 0 msec
/: rc = 0
consumers
storm
brokers
zookeeper
jobkeeper
time = 0 msec
这样就安装成功了
3.下载python扩展包,并且解压:
地址:https://pypi.python.org/pypi/zkpython/0.4.2
4.如果找不到Pyhon.h,可能由于没有安装python26-devel
32位直接运行
yum install python26-devel.i386
5.测试是否成功
import zookeeper
如果报错:
>>> import zookeeper
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: libzookeeper_mt.so.2: cannot open shared object file: No such file or directory
在shell中配置或者假如/etc/profile文件中:
export LD_LIBRARY_PATH=/usr/local/lib/
6.使用
>>> import zookeeper
>>> hander = zookeeper.init("dc002.tj:2181")
>>> dir(zookeeper) #方法列表
[‘APIERROR‘, ‘ASSOCIATING_STATE‘, ‘AUTHFAILED‘, ‘AUTH_FAILED_STATE‘, ‘ApiErrorException‘, ‘AuthFailedException‘, ‘BADARGUMENTS‘, ‘BADVERSION‘, ‘BadArgumentsException‘, ‘BadVersionException‘, ‘CHANGED_EVENT‘, ‘CHILD_EVENT‘, ‘CLOSING‘, ‘CONNECTED_STATE‘, ‘CONNECTING_STATE‘, ‘CONNECTIONLOSS‘, ‘CREATED_EVENT‘, ‘ClosingExceHelp on built-in function get in module zookeeper:
ption‘, ‘ConnectionLossException‘, ‘DATAINCONSISTENCY‘, ‘DELETED_EVENT‘, ‘DataInconsistencyException‘, ‘EPHEMERAL‘, ‘EXPIRED_SESSION_STATE‘, ‘INVALIDACL‘, ‘INVALIDCALLBACK‘, ‘INVALIDSTATE‘, ‘InvalidACLException‘, ‘InvalidCallbackException‘, ‘InvalidStateException‘, ‘LOG_LEVEL_DEBUG‘, ‘LOG_LEVEL_ERROR‘, ‘LOG_LEVEL_INFO‘, ‘LOG_LEVEL_WARN‘, ‘MARSHALLINGERROR‘, ‘MarshallingErrorException‘, ‘NOAUTH‘, ‘NOCHILDRENFOREPHEMERALS‘, ‘NODEEXISTS‘, ‘NONODE‘, ‘NOTEMPTY‘, ‘NOTHING‘, ‘NOTWATCHING_EVENT‘, ‘NoAuthException‘, ‘NoChildrenForEphemeralsException‘, ‘NoNodeException‘, ‘NodeExistsException‘, ‘NotEmptyException‘, ‘NothingException‘, ‘OK‘, ‘OPERATIONTIMEOUT‘, ‘OperationTimeoutException‘, ‘PERM_ADMIN‘, ‘PERM_ALL‘, ‘PERM_CREATE‘, ‘PERM_DELETE‘, ‘PERM_READ‘, ‘PERM_WRITE‘, ‘RUNTIMEINCONSISTENCY‘, ‘RuntimeInconsistencyException‘, ‘SEQUENCE‘, ‘SESSIONEXPIRED‘, ‘SESSIONMOVED‘, ‘SESSION_EVENT‘, ‘SYSTEMERROR‘, ‘SessionExpiredException‘, ‘SessionMovedException‘, ‘SystemErrorException‘, ‘UNIMPLEMENTED‘, ‘UnimplementedException‘, ‘ZooKeeperException‘, ‘__doc__‘, ‘__file__‘, ‘__name__‘, ‘__package__‘, ‘__version__‘, ‘acreate‘, ‘add_auth‘, ‘adelete‘, ‘aexists‘, ‘aget‘, ‘aget_acl‘, ‘aget_children‘, ‘aset‘, ‘aset_acl‘, ‘async‘, ‘client_id‘, ‘close‘, ‘create‘, ‘delete‘, ‘deterministic_conn_order‘, ‘exists‘, ‘get‘, ‘get_acl‘, ‘get_children‘, ‘init‘, ‘is_unrecoverable‘, ‘recv_timeout‘, ‘set‘, ‘set2‘, ‘set_acl‘, ‘set_debug_level‘, ‘set_log_stream‘, ‘set_watcher‘, ‘state‘, ‘zerror‘]
#测试get命令
>>> zookeeper.get(hander,"/jobkeeper/gamedc/job/kpi-day-pay_day")
(‘{"dependency":[{"name":"etl-day-pay_log","type":"day"}],"name":"kpi-day-pay_day","owner":"dc","path":"${SCRIPTS_HOME}/kpi/pay/pay_day.sh","type":"day","hour":"01"}‘, {‘pzxid‘: 4298844949L, ‘ctime‘: 1411896174285L, ‘aversion‘: 0, ‘mzxid‘: 4299652800L, ‘numChildren‘: 0, ‘ephemeralOwner‘: 0L, ‘version‘: 2, ‘dataLength‘: 163, ‘mtime‘: 1412055214425L, ‘cversion‘: 0, ‘czxid‘: 4298844949L})
#其他的delete,set可以自己测试了,都是api
1.由于python客户端依赖c的客户端所以要先安装c版本的客户端
cd zookeeper-3.4.5/src/c
./configure
make
make install
2.测试c版本客户端,需要gcc和python-devel
./cli_mt localhost:2181
Watcher SESSION_EVENT state = CONNECTED_STATE
Got a new session id: 0x23f9d77d3fe0001
ls /
time = 0 msec
/: rc = 0
consumers
storm
brokers
zookeeper
jobkeeper
time = 0 msec
这样就安装成功了
3.下载python扩展包,并且解压:
地址:https://pypi.python.org/pypi/zkpython/0.4.2
4.如果找不到Pyhon.h,可能由于没有安装python26-devel
32位直接运行
yum install python26-devel.i386
5.测试是否成功
import zookeeper
如果报错:
>>> import zookeeper
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: libzookeeper_mt.so.2: cannot open shared object file: No such file or directory
在shell中配置或者假如/etc/profile文件中:
export LD_LIBRARY_PATH=/usr/local/lib/
6.使用
>>> import zookeeper
>>> hander = zookeeper.init("dc002.tj:2181")
>>> dir(zookeeper) #方法列表
[‘APIERROR‘, ‘ASSOCIATING_STATE‘, ‘AUTHFAILED‘, ‘AUTH_FAILED_STATE‘, ‘ApiErrorException‘, ‘AuthFailedException‘, ‘BADARGUMENTS‘, ‘BADVERSION‘, ‘BadArgumentsException‘, ‘BadVersionException‘, ‘CHANGED_EVENT‘, ‘CHILD_EVENT‘, ‘CLOSING‘, ‘CONNECTED_STATE‘, ‘CONNECTING_STATE‘, ‘CONNECTIONLOSS‘, ‘CREATED_EVENT‘, ‘ClosingExceHelp on built-in function get in module zookeeper:
ption‘, ‘ConnectionLossException‘, ‘DATAINCONSISTENCY‘, ‘DELETED_EVENT‘, ‘DataInconsistencyException‘, ‘EPHEMERAL‘, ‘EXPIRED_SESSION_STATE‘, ‘INVALIDACL‘, ‘INVALIDCALLBACK‘, ‘INVALIDSTATE‘, ‘InvalidACLException‘, ‘InvalidCallbackException‘, ‘InvalidStateException‘, ‘LOG_LEVEL_DEBUG‘, ‘LOG_LEVEL_ERROR‘, ‘LOG_LEVEL_INFO‘, ‘LOG_LEVEL_WARN‘, ‘MARSHALLINGERROR‘, ‘MarshallingErrorException‘, ‘NOAUTH‘, ‘NOCHILDRENFOREPHEMERALS‘, ‘NODEEXISTS‘, ‘NONODE‘, ‘NOTEMPTY‘, ‘NOTHING‘, ‘NOTWATCHING_EVENT‘, ‘NoAuthException‘, ‘NoChildrenForEphemeralsException‘, ‘NoNodeException‘, ‘NodeExistsException‘, ‘NotEmptyException‘, ‘NothingException‘, ‘OK‘, ‘OPERATIONTIMEOUT‘, ‘OperationTimeoutException‘, ‘PERM_ADMIN‘, ‘PERM_ALL‘, ‘PERM_CREATE‘, ‘PERM_DELETE‘, ‘PERM_READ‘, ‘PERM_WRITE‘, ‘RUNTIMEINCONSISTENCY‘, ‘RuntimeInconsistencyException‘, ‘SEQUENCE‘, ‘SESSIONEXPIRED‘, ‘SESSIONMOVED‘, ‘SESSION_EVENT‘, ‘SYSTEMERROR‘, ‘SessionExpiredException‘, ‘SessionMovedException‘, ‘SystemErrorException‘, ‘UNIMPLEMENTED‘, ‘UnimplementedException‘, ‘ZooKeeperException‘, ‘__doc__‘, ‘__file__‘, ‘__name__‘, ‘__package__‘, ‘__version__‘, ‘acreate‘, ‘add_auth‘, ‘adelete‘, ‘aexists‘, ‘aget‘, ‘aget_acl‘, ‘aget_children‘, ‘aset‘, ‘aset_acl‘, ‘async‘, ‘client_id‘, ‘close‘, ‘create‘, ‘delete‘, ‘deterministic_conn_order‘, ‘exists‘, ‘get‘, ‘get_acl‘, ‘get_children‘, ‘init‘, ‘is_unrecoverable‘, ‘recv_timeout‘, ‘set‘, ‘set2‘, ‘set_acl‘, ‘set_debug_level‘, ‘set_log_stream‘, ‘set_watcher‘, ‘state‘, ‘zerror‘]
#测试get命令
>>> zookeeper.get(hander,"/jobkeeper/gamedc/job/kpi-day-pay_day")
(‘{"dependency":[{"name":"etl-day-pay_log","type":"day"}],"name":"kpi-day-pay_day","owner":"dc","path":"${SCRIPTS_HOME}/kpi/pay/pay_day.sh","type":"day","hour":"01"}‘, {‘pzxid‘: 4298844949L, ‘ctime‘: 1411896174285L, ‘aversion‘: 0, ‘mzxid‘: 4299652800L, ‘numChildren‘: 0, ‘ephemeralOwner‘: 0L, ‘version‘: 2, ‘dataLength‘: 163, ‘mtime‘: 1412055214425L, ‘cversion‘: 0, ‘czxid‘: 4298844949L})
#其他的delete,set可以自己测试了,都是api
文章来自:http://blog.csdn.net/u010670689/article/details/46127463