exdb.connect
通过 SmartESQL 引擎创建与一个或多个数据库的连接。
Connection exdb.connect(
dbname,
port,
nodes,
nReplicas,
maxConnectAttempts,
useConnectionPool=useConnectionPool,
txBufSize=txBufSize,
localDomain=local,
connectTimeout=2000L,
readTimeout=1200*1000L,
sql_login=sql_login,
sql_password=sql_password,
ssl_params=ssl_params,
async=async,
nworkers=nWorkers
)
此方法创建与 SmartESQL 引擎的连接。该连接将属于四种 SQL 引擎类型中的一种:
- 当您指定了数据库名称(dbname)且未提供端口时,我们将返回一个连接到本地数据库的单个连接。这个本地数据库可以是常规内存或共享内存,具体取决于您加载的运行时环境。
- 当您提供了端口时,我们将从数据库名称中提取主机名,并返回一个连接到远程数据库的单个连接。
- 当您提供了节点列表(nodes)时,我们将使用节点和最大连接尝试次数(maxConnectAttempts)参数,返回一个分布式连接。这将帮助您创建 SmartEDB 高可用性配置(在主数据库和副本数据库上运行),或者构建一个 SmartEDB 集群网络(在多个数据库或数据库分片上运行)。
- 如果您设置了 async=True 参数,我们将返回一个异步分布式连接,并为您创建 nworkers 个进程,以实现多个查询的并行异步执行。
参数
dbname
数据库名称或主机 IP:如果您指定了端口号,我们将连接到远程数据库;否则,我们将根据数据库名称连接到本地数据库(常规或共享内存)。
port
端口:当您指定端口时,系统将使用从数据库名称中提取的主机和指定的端口连接到远程数据库。
nodes
节点列表:如果提供了节点列表,我们将使用其中指定的主机和端口值连接到分布式数据库。
nReplicas
高可用副本数:这是高可用副本的数量。此数量应为节点总数的除数,即 nNodes / nReplicas 的余数为 0。例如,nNodes 和 nReplicas 的值可以是(4 和 2)或(12 和 3),但不能是(6 和 4),因为 6 % 4 = 2。
maxConnectAttempts
最大连接尝试次数:这是连接到分布式节点或高可用副本的最大尝试次数。
useConnectionPool
使用连接池:如果设置为 true,我们将创建一个连接池以启用序列的并行查询执行。
txBufSize
传输缓冲区大小:这指定了用于序列化发送到服务器的请求的传输缓冲区大小,默认值为 64k。
localDomain
本地域:如果设置为 true,RSQL 通信将使用 Unix 域套接字而非网络 TCP 套接字,这仅在服务器和客户端位于同一主机时才可行。
connectTimeout
连接超时时间:每次连接尝试的超时时间(以毫秒为单位)。因此,总连接时间最多可达 connectTimeout * 最大连接尝试次数 毫秒。
readTimeout
读取超时时间:读取操作的超时时间(以毫秒为单位)。
sql_login
SQL 登录名:当需要身份验证时使用的登录用户名。
sql_password
SQL 密码:当需要身份验证时使用的登录密码。
ssl_params
SSL 参数:指向包含 SSL 连接设置的 mco_ssl_params_t 结构的指针。
async
异步模式:如果设置为 True,我们将使用 nworkers 个进程创建 AsyncDistributedConnection,以在 nodes 中指定的分布式数据库分片上执行多个查询。
nworkers
工作进程数:当异步模式设置为 True 时,我们将创建 nworkers 个进程以在 nodes 中指定的分布式数据库分片上执行多个查询。
返回
Connection
根据上述参数确定的 SQL 引擎类型的连接。
示例
以下代码片段演示了创建不同类型的连接:
''' Connect to a local in-memory database in conventional memory (with Connection)
'''
is_shm = False
is_debug = True
is_disk = False
tmgr='mursiw'
exdb.init_runtime(is_disk, tmgr, is_shm, is_debug)
conn = exdb.connect(dbname='imdb')
''' Connect to a local in-memory database in shared memory (with Connection)
'''
is_shm = True
is_debug = True
is_disk = False
tmgr='mursiw'
exdb.init_runtime(is_disk, tmgr, is_shm, is_debug)
conn = exdb.connect(dbname='shmdb')
''' Connect to a remote database (with RemoteConnection)
'''
is_shm = False
is_debug = True
is_disk = False
tmgr='mursiw'
exdb.init_runtime(is_disk, tmgr, is_shm, is_debug)
conn = exdb.connect('localhost', 5001)
''' Connect to two remote databases (with DistributedConnection)
'''
is_shm = False
is_debug = True
is_disk = False
tmgr='mursiw'
nodes = ("localhost:5001", "localhost:5002")
exdb.init_runtime(is_disk, tmgr, is_shm, is_debug)
conn = exdb.connect(nodes)
''' Connect to three remote databases (with AsyncDistributedConnection)
'''
is_shm = False
is_debug = True
is_disk = False
tmgr='mursiw'
nodes = ('bogota01:5000', 'bogota02:5001', 'bogota03:5000')
exdb.init_runtime(is_disk, tmgr, is_shm, is_debug)
conn = exdb.connect(nodes, async=True, nWorkers=5) )