This post walks through the official GBase 8a Python driver: installation, connection handling, DDL, DML, DQL, multi-statement execution, batch inserts, stored procedures and data loading. The China-domestically developed GBase 8a MPP database from GBASE ships version-specific connectors, and every sample below uses Python 2 syntax so it can be copied as-is.
Driver version and installation
Python 2 and Python 3 need different driver packages. Everything here is built on gbase-connector-python2-5.0.1.tar.gz and uses py2 syntax throughout.
[gbase@vm246 python]$ tar xvf gbase-connector-python2-5.0.1.tar.gz
[root@vm246 gbase-connector-python2-5.0.1]$ python setup.py build
[root@vm246 gbase-connector-python2-5.0.1]$ python setup.py install
setup.py build copies the GBaseConnector modules into build/lib/GBaseConnector; setup.py install places them under /usr/lib/python2.7/site-packages/GBaseConnector and writes gbase_connector_python-5.0.1-py2.7.egg-info.
Connecting
[root@vm246 python]# cat testCon.py
from GBaseConnector import connect,GBaseError
if __name__ == '__​main__':
config = {'host':'10.10.10.10',
'port':5258,
'database':'testdb',
'user':'root',
'passwd':'PASSWORD_OF_ROOT',
'connection_timeout':3600,
'charset':'utf8'}
try:
conn = connect()
conn.connect(**config)
print conn
except GBaseError.DatabaseError, err:
print err
finally:
conn.close()
[root@vm246 python]# python testCon.py
<GBaseConnector.GBaseConnection.GBaseConnection object at 0x7ff39b92d990>
The connection config carries host, port 5258, database testdb, user, passwd, connection_timeout 3600 and charset utf8. The finally block closes the connection even when GBaseError.DatabaseError is raised.
DDL, DML and DQL
DDL is a single execute call; its return value is None, and desc confirms the new table:
[gbase@vm246 python]$ cat testDDL.py
from GBaseConnector import connect,GBaseError
if __name__ == '__​main__':
config = {'host':'10.10.10.10',
'port':5258,
'database':'testdb',
'user':'root',
'passwd':'Admin2O11O531'}
try:
conn = connect()
conn.connect(**config)
cur = conn.cursor()
print cur.execute("CREATE table if not exists testPython(id int)")
except GBaseError.DatabaseError, err:
print err
finally:
conn.close()
gbase> use testdb;
Query OK, 0 rows affected (Elapsed: 00:00:00.00)
gbase> desc testPython;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (Elapsed: 00:00:00.01)
DML uses the same shape. After inserting 76194278 into testPython, gccli confirms the row inside the server:
[gbase@vm246 python]$ python testDML.py
None
None
[gbase@vm246 python]$ gccli -pAdmin2O11O531 testdb
GBase client 9.5.3.29.23fba856. Copyright (c) 2004-2023, GBase. All Rights Reserved.
gbase> select * from testPython;
+----------+
| id |
+----------+
| 76194278 |
+----------+
1 row in set (Elapsed: 00:00:00.01)
A single-row query returns (76194278,) through fetchone():
[root@vm246 python]# cat testDQL.py
from GBaseConnector import connect,GBaseError
if __name__ == '__​main__':
config = {'host':'10.10.10.10',
'port':5258,
'database':'testdb',
'user':'root',
'passwd':'Admin2O11O531'}
try:
conn = connect()
conn.connect(**config)
cur = conn.cursor()
cur.execute("drop table if exists testPython")
cur.execute("CREATE table if not exists testPython(id int)")
cur.execute("insert into testPython values(76194278)")
cur.execute("select * from testPython")
row = cur.fetchone()
print row
except GBaseError.DatabaseError, err:
print err
finally:
conn.close()
[root@vm246 python]# python testDQL.py
(76194278,)
Before reusing the same connection, the previous select must be fully consumed, otherwise the driver reports Unread result found. With fetchall() and fetchmany(2) the two result sets are [(76194278,)] and [(76194278,), (22222,)]:
[root@vm246 python]# cat testDQL.py
from GBaseConnector import connect,GBaseError
if __name__ == '__​main__':
config = {'host':'10.10.10.10',
'port':5258,
'database':'testdb',
'user':'root',
'passwd':'Admin2O11O531'}
try:
conn = connect()
conn.connect(**config)
cur = conn.cursor()
cur.execute("drop table if exists testPython")
cur.execute("CREATE table if not exists testPython(id int)")
cur.execute("insert into testPython values(76194278)")
cur.execute("select * from testPython")
row = cur.fetchall()
print row
cur.execute("insert into testPython values(22222),(33333)")
cur.execute("select * from testPython")
row2 = cur.fetchmany(2)
print row2
except GBaseError.DatabaseError, err:
print err
finally:
conn.close()
[root@vm246 python]# python testDQL.py
[(76194278,)]
[(76194278,), (22222,)]
Multiple statements in one call
Pass multi_stmt=True, otherwise the driver raises Use query_iter for statements with multiple queries. DDL and DML return None, while DQL returns result sets.
[root@vm246 python]# cat testMultiSQL.py
from GBaseConnector import connect,GBaseError
if __name__ == '__​main__':
config = {'host':'10.10.10.10',
'port':5258,
'database':'testdb',
'user':'root',
'passwd':'Admin2O11O531'}
try:
conn = connect()
conn.connect(**config)
cur = conn.cursor()
cur.execute("drop table if exists testPython;CREATE table if not exists testPython(id int);insert into testPython values(76194278)", multi_stmt=True)
cur.execute("select * from testPython")
row = cur.fetchall()
print row
except GBaseError.DatabaseError, err:
print err
finally:
conn.close()
[root@vm246 python]# python testMultiSQL.py
[(76194278,), (22222,), (33333,)]
For several DQL statements, iterate over the returned result sets — [(76194278,)] and [(1,)]:
[root@vm246 python]# cat testMultiDQLSQL.py
from GBaseConnector import connect,GBaseError
if __name__ == '__​main__':
config = {'host':'10.10.10.10',
'port':5258,
'database':'testdb',
'user':'root',
'passwd':'Admin2O11O531'}
try:
conn = connect()
conn.connect(**config)
cur = conn.cursor()
cur.execute("drop table if exists testPython")
cur.execute("CREATE table if not exists testPython(id int)")
cur.execute("insert into testPython values(76194278)")
rss = cur.execute("select * from testPython;select count(*) from testPython", multi_stmt=True)
for rs in rss:
print rs.fetchall()
except GBaseError.DatabaseError, err:
print err
finally:
conn.close()
[root@vm246 python]# python testMultiDQLSQL.py
[(76194278,)]
[(1,)]
Batch insert
executemany() writes 100 rows in one call, and the follow-up query returns all of them (row0 through row99):
[root@vm246 python]# cat testPrepared.py
from GBaseConnector import connect,GBaseError
if __name__ == '__​main__':
config = {'host':'10.10.10.10',
'port':5258,
'database':'testdb',
'user':'root',
'passwd':'Admin2O11O531'}
try:
conn = connect()
conn.connect(**config)
cur = conn.cursor()
cur.execute("drop table if exists testPython2")
cur.execute("CREATE table if not exists testPython2(id int,name varchar(100))")
opfmt = "INSERT INTO testPython2(id, name) VALUES(%s, %s)"
rows = []
for i in xrange(0, 100):
rows.append((i, "row" + str(i)))
cur.executemany(opfmt, rows)
cur.execute("select * from testPython2")
row = cur.fetchall()
print row
except GBaseError.DatabaseError, err:
print err
finally:
conn.close()
[root@vm246 python]# python testPrepared.py
[(0, u'row0'), (1, u'row1'), (2, u'row2'), (3, u'row3'), (4, u'row4'), (5, u'row5'), (6, u'row6'), (7, u'row7'), (8, u'row8'), (9, u'row9'), (10, u'row10'), (11, u'row11'), (12, u'row12'), (13, u'row13'), (14, u'row14'), (15, u'row15'), (16, u'row16'), (17, u'row17'), (18, u'row18'), (19, u'row19'), (20, u'row20'), (21, u'row21'), (22, u'row22'), (23, u'row23'), (24, u'row24'), (25, u'row25'), (26, u'row26'), (27, u'row27'), (28, u'row28'), (29, u'row29'), (30, u'row30'), (31, u'row31'), (32, u'row32'), (33, u'row33'), (34, u'row34'), (35, u'row35'), (36, u'row36'), (37, u'row37'), (38, u'row38'), (39, u'row39'), (40, u'row40'), (41, u'row41'), (42, u'row42'), (43, u'row43'), (44, u'row44'), (45, u'row45'), (46, u'row46'), (47, u'row47'), (48, u'row48'), (49, u'row49'), (50, u'row50'), (51, u'row51'), (52, u'row52'), (53, u'row53'), (54, u'row54'), (55, u'row55'), (56, u'row56'), (57, u'row57'), (58, u'row58'), (59, u'row59'), (60, u'row60'), (61, u'row61'), (62, u'row62'), (63, u'row63'), (64, u'row64'), (65, u'row65'), (66, u'row66'), (67, u'row67'), (68, u'row68'), (69, u'row69'), (70, u'row70'), (71, u'row71'), (72, u'row72'), (73, u'row73'), (74, u'row74'), (75, u'row75'), (76, u'row76'), (77, u'row77'), (78, u'row78'), (79, u'row79'), (80, u'row80'), (81, u'row81'), (82, u'row82'), (83, u'row83'), (84, u'row84'), (85, u'row85'), (86, u'row86'), (87, u'row87'), (88, u'row88'), (89, u'row89'), (90, u'row90'), (91, u'row91'), (92, u'row92'), (93, u'row93'), (94, u'row94'), (95, u'row95'), (96, u'row96'), (97, u'row97'), (98, u'row98'), (99, u'row99')]
Stored procedures
callproc() yields one result set per statement inside the procedure — [(3,)] and [(datetime.datetime(2023, 9, 22, 9, 30, 31),)]:
[root@vm246 python]# cat testProcedure.py
from GBaseConnector import connect,GBaseError
if __name__ == '__​main__':
config = {'host':'10.10.10.10',
'port':5258,
'database':'testdb',
'user':'root',
'passwd':'Admin2O11O531'}
try:
conn = connect()
conn.connect(**config)
cur = conn.cursor()
cur.execute("drop procedure if exists testPython_proc")
cur.execute("create procedure testPython_proc() \
begin \
select 1+2;\
select now();\
end")
rss = cur.callproc("testPython_proc")
for rs in rss:
print rs.fetchall()
except GBaseError.DatabaseError, err:
print err
finally:
conn.close()
[root@vm246 python]# python testProcedure.py
[(3,)]
[(datetime.datetime(2023, 9, 22, 9, 30, 31),)]
Loading data
rowcount gives the loaded row count directly. The version tested here does not expose skipped rows as an attribute, so the sample parses cur.info to recover the task id and the skipped count:
[root@vm246 python]# cat testLoad.py
from GBaseConnector import connect,GBaseError
if __name__ == '__​main__':
config = {'host':'10.10.10.10',
'port':5258,
'database':'testdb',
'user':'root',
'passwd':'Admin2O11O531'}
try:
conn = connect()
conn.connect(**config)
cur = conn.cursor()
cur.execute("drop table if exists testPython2")
cur.execute("CREATE table if not exists testPython2(id int,name varchar(100))")
cur.execute("load data infile 'file://10.10.10.10/home/gbase/py.txt' into table testpython2 fields terminated by ','")
print cur.info
print("Loaded",cur.rowcount)
infors=cur.info.split()
print("TaskId",infors[1])
print("Skipped",infors[7])
except GBaseError.DatabaseError, err:
print err
finally:
conn.close()
[root@vm246 python]# python testLoad.py
Task 3237 finished, Loaded 1 records, Skipped 0 records
('Loaded', 1)
('TaskId', '3237')
('Skipped', '0')
Going forward, treat the gbase connector as a thin DB-API layer: pin multi_stmt=True for multi-statement calls, always drain a result set before reusing a cursor, and close connections in finally. Those habits keep Python clients on a gbase cluster predictable even when a load or a stored procedure returns several result sets.
Top comments (0)