# eZooDB-Python-SDK使用手册
本文档维护了eZooDB-Python-SDK的安装,使用教程。
# 环境
Python3.7及以上
# 安装Python SDK
eZoo支持Python3版本的SDK,安装方法:
首先下载需要的SDK eZooDB-Python-SDK,然后安装下载好的SDK。
python3 -m pip install "xxx/ezoo-x.x.x-py3-none-any.whl"
# 使用示例
本示例实现了eZooDB-Python-SDK连接ezoodb服务(采用非连接池的方式获取数据库连接),并且查询指定节点邻居数量(api请见query_neighbour_count。
from pyezoo.connections import Connection
conn = Connection(host='localhost', port=9090, user='', password='', auth=False)
db_name = model_db
print(conn.client.open_graph(db_name,{}))
result=conn.client.query_neighbour(db_name,0,1,3,0,"")
print(result)
更多api请见 API。
# 高级特性
eZooDB-Python-SDK 同时支持连接池,并在副本集部署下支持读写分离。更多使用示例可参考SDK中的测试用例。
# 连接池
单机部署下,使用PooledDB类获取连接,示例如下:
# ssl open
pool = PooledDB(pyezoo,
# pool params
mincached=0, maxcached=5, maxconnections=10, connect_timeout=1000,
# pass to pyezoo.connection
user='root', password='123456', host="localhost", port=9090,
auth=True,
timeout=10000,
ssl_open=False,
ssl_cert_common_name="ezoodb.com",
ca_certs="/tmp/ezoodb/keys/CA.pem",
client_cert="/tmp/ezoodb/keys/client.crt",
client_key="/tmp/ezoodb/keys/client.key"
)
for i in range(1000):
conn = pool.connection()
client = conn.get_client()
if client:
res = client.hello()
print(res)
conn.close()
副本集部署下,使用PooledDistribute类获取连接,示例如下:
import random
import threading
from time import sleep
from pyezoo.pool.pooled_distribute import PooledDistribute
class ReadTask(threading.Thread):
def __init__(self, pool):
super().__init__(daemon=True)
self.pool = pool
def run(self) -> None:
conn = pool.read_connection()
if conn:
client = conn.get_client()
client.hello()
print("connected to read server ok: " + conn.ip + ":" + conn.port)
sleep(9 * random.random())
conn.close()
class WriteTask(threading.Thread):
def __init__(self, pool):
super().__init__(daemon=True)
self.pool = pool
def run(self) -> None:
conn = self.pool.write_connection()
if conn:
client = conn.get_client()
client.hello()
print("connected to write server ok: " + conn.ip + ":" + conn.port)
sleep(9 * random.random())
conn.close()
# replicaset: read_slave_ok = false, so only leader can read and write
# replicaset: read_slave_ok = true, read_write_separate = true, the leader can be written and the followers can be read.
# replicaset: read_slave_ok = true, read_write_separate = false, the leader can be written and the all can be read.
if __name__ == "__main__":
pool = PooledDistribute(
user='ezoo',
password='ezoo',
database='test',
scheme_url="ezoodb://192.168.1.99:9090,192.168.1.99:19090",
mincached=0,
maxcached=5,
maxconnections=10,
read_mincached=0,
read_maxcached=10,
read_maxconnections=10,
blocking=False,
connect_timeout=1000,
read_slave_ok=True,
read_write_separate=True,
maxusage=10,
timeout=-1,
auth=True,
ssl_open=False,
ssl_cert_common_name=None,
ca_certs=None,
client_cert=None,
client_key=None
)
for i in range(1000):
ReadTask(pool).start()
WriteTask(pool).start()
sleep(0.5)
pool.close()
# 支持ssl安全访问
若eZoo图数据库开启ssl认证,对应的SDK在获取连接时,也应传入相应的配置。可参考配置类EZooPooledConfig,查看类注释了解各个字段的含义。
class EZooPooledConfig:
"""
ezoo connection pool's config
:param user 用户名
:param password 密码
:param scheme_url ezoo-server的scheme_url,入参指定的user、password、database的优先级大于scheme_url解析后的 e.g. ezoodb://192.168.1.99:9090,192.168.1.100:9090
:param database 数据库名 暂未使用
:param maxconnections: 连接池允许的最大连接数,0和None表示不限制连接数, 使用PooledDistribute时表示写池的最大连接数
:param mincached: 初始化时,链接池中至少创建的空闲的链接,0表示不创建, 使用PooledDistribute时表示写池的初始连接数
:param maxcached: 链接池中最多闲置的链接,0和None不限制, 使用PooledDistribute时表示写池的最多闲置连接
:param read_maxconnections: 仅用于PooledDistribute,且read_slave_ok=True时有效,表示读池的最大连接数
:param read_mincached: 仅用于PooledDistribute,且read_slave_ok=True时有效,表示读池的初始连接数
:param read_maxcached: 仅用于PooledDistribute,且read_slave_ok=True时有效,表示读池的最多闲置连接
:param blocking: 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错
:param maxusage: 一个链接最多被重复使用的次数,None表示无限制
:param connect_timeout: The timeout for reading from the connection in seconds (default: None - no timeout)
:param read_slave_ok 从库是否可读
:param read_write_separate 读写分离标识
:param ssl_open: ssl open 开启ssl认证
:param auth: need auth 开启数据库鉴权认证
other params:
if enable ssl: next params need be input. example:
ssl_cert_common_name="ezoodb.com" 签名域名
ca_certs="/tmp/ezoodb/keys/CA.pem" CA证书路径
client_cert="/tmp/ezoodb/keys/client.crt" 客户端证书路径
client_key="/tmp/ezoodb/keys/client.key" 客户端证书私钥
"""
def __init__(self,...)
pass