標籤: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
Python ZKPython 安裝