Pyodbc with BigQuery



我正在尝试让Pyodbc与Google BigQuery一起工作。 我安装的ODBC管理器是unixodbc (ubuntu) Simba 驱动程序的配置应该没问题,因为 SQL 命令有效,我可以从那里执行查询。

但是,在使用Pyodbc时,我卡住了。 这是代码:

import pyodbc
dbname = 'testdb'
driver_str = '{Simba ODBC Driver for Google BigQuery 64-bit}'
cnxn = pyodbc.connect(driver=driver_str, database=dbname)
c = conn.cursor()
c.execute('SELECT * FROM tablename')
print(c.fetchone())

它会产生以下错误:

Traceback (most recent call last):
File "/home/virus/work/lutech/wind/usecase3/test_odbc.py", line 48, in <module>
cnxn = pyodbc.connect(driver=driver_str, database=dbname)
pyodbc.OperationalError: ('08001', '[08001] [unixODBC][Simba][DSI] An error occurred while attempting to retrieve the error message for key 'UnableToEstConn' and component ID 1: Could not open error message files - Check that "/home/virus/work/lutech/wind/simba/googlebigqueryodbc/lib/64/$(INSTALLDIR)/ErrorMessages/en-US/ODBCMessages.xml" or "/home/virus/work/lutech/wind/simba/googlebigqueryodbc/lib/64/$(INSTALLDIR)/ErrorMessages/ODBCMessages_en-US.xml" exists and are accessible. MessageParameters=["{[Catalog] [OAuthMechanism]}"] (-1) (SQLDriverConnect)')

我不明白这是什么意思,但它指的是 Simba 错误文件夹中的文件。

有什么帮助吗?

我用非常密集的尝试和错误方法解决了。现在很清楚了。 我使用了本地用户配置文件,以避免权限问题。/etc/中的那些是空的。

这是我的.odbcinst.ini文件的内容:

$ cat .odbcinst.ini 
[ODBC Drivers]
Simba ODBC Driver for Google BigQuery 64-bit=Installed
[Simba ODBC Driver for Google BigQuery 64-bit]
Description=Simba ODBC Driver for Google BigQuery (64-bit)
Driver=<local user installation path>/simba/googlebigqueryodbc/lib/64/libgooglebigqueryodbc_sb64.so

这是我的.odbc.ini:

$ cat .odbc.ini 
[bigquery_odbc]
Driver=Simba ODBC Driver for Google BigQuery 64-bit
Catalog=<gcp project id>
OAuthMechanism=0
Email= <email service account>
KeyFilePath=<path to the json file downloaded when creating the service account>

在这里你应该能够成功执行 ISQL -V bigquery_odbc

现在,如果我尝试使用 pyodbc 连接,则会出现上述错误。 首先修复配置文件中表示的安装路径以及此处指定的 UTF 编码

$ cat <local user installation path>/simba/googlebigqueryodbc/lib/64/simba.googlebigqueryodbc.ini
# To use this INI file, replace $(INSTALLDIR) with the
# directory the tarball was extracted to.
[Driver]
DriverManagerEncoding=UTF-16
ErrorMessagesPath=<local user installation path>simba/googlebigqueryodbc/ErrorMessages
LogLevel=0
LogPath=<log path>
LogFileCount=5
LogFileSize=10

调用pyodbc时,它起作用了:

dataset_name = <bigquery dataset name>
DSN = 'bigquery_odbc'
conn_str = "DSN={}".format(DSN)
cnxn = pyodbc.connect(conn_str, autocommit=True) # DO NOT forget autocommit param
cursor = cnxn.cursor()
cursor.execute('select * from {}.table;'.format(dataset_name))
print(cursor.fetchone())

我在这种配置上挣扎了很多。希望它能帮助他人

这在没有额外配置的情况下对我有用。 首先,从此处下载并配置 ODBC 驱动程序:

接下来 - 像这样使用连接(注意 IgnoreTransactions 参数):

import pyodbc
import pandas as pd
conn = pyodbc.connect(r'Driver={Simba ODBC Driver for Google BigQuery};OAuthMechanism=0;Catalog=<projectID>;KeyFilePath=<path to json credentials>;Email=<email of service account>;IgnoreTransactions=1')
qry = 'select * from <path to your table>'
data = pd.read_sql(qry,conn)

最新更新