与BigQuery的Pyodbc

问题描述 投票:0回答:1

我想让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 Error文件夹中的文件。

有帮助吗?

google-bigquery odbc pyodbc unixodbc
1个回答
1
投票

我用一种非常密集的尝试和错误方法解决了问题。现在很清楚了。我使用本地用户配置文件,以避免权限问题。 / 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连接,我会得到上面的错误。首先修复配置文件中表示的安装路径以及指定的here的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())

我用这种配置苦苦挣扎。希望它能帮助别人

© www.soinside.com 2019 - 2024. All rights reserved.