2017-06-12 40 views
0

我正在嘗試在其他數據服務中執行一些非常簡單的操作。我試圖做一個相對簡單的SQL查詢並將其作爲python中的數據框返回。我是在Windows 10和使用Phython 2.7(特別篷1.7.4)在Windows上從Python製作Google BigQuery

通常這將與pandas.read_sql_query但由於需做一些具體細節與BigQuery,他們需要不同的方法pandas.io.gbq.read_gbq

這種方法工作得很好,除非你想做出一個大問題。如果您在BigQuery的一大查詢你的錯誤


GenericGBQException:原因:responseTooLarge,消息:響應太大返回。考慮在作業配置中將allowLargeResults設置爲true。欲瞭解更多信息,請參閱https://cloud.google.com/bigquery/troubleshooting-errors


這是問及在此票之前回答,但無論是解決方案的有關我的情況

Python BigQuery allowLargeResults with pandas.io.gbq

一種解決方案是爲Python 3等等它是一個不起眼的人。另一個錯誤是因爲我無法將我的憑據設置爲Windows環境變量。


ApplicationDefaultCredentialsError:在應用程序默認憑據不可用。如果在Google Compute Engine中運行,則它們可用。否則,必須定義環境變量GOOGLE_APPLICATION_CREDENTIALS,指向定義憑據的文件。有關更多信息,請參閱https://developers.google.com/accounts/docs/application-default-credentials


我能下載證書文件的JSON。我將它作爲我知道,但我仍然得到上述錯誤的幾種方法的環境變量。我需要用python以某種方式加載嗎?它似乎在尋找它,但無法找到是正確的。在這種情況下是否有特殊的方法將其設置爲環境變量?

+0

我相信這是熊貓圖書館的這個錯誤:https://github.com/pydata/pandas-gbq/issues/15 –

+0

類別。 pandas_gbq中使用的憑證方法與需要用於大型查詢的方法大不相同。這將需要整個重寫。這實質上是鏈接票證中的代碼所做的。它在查詢不大的情況下使用read_gbq,否則使用JSON證書。我不認爲你沒有JSON證書就可以做到這一點,這是我陷入困境。 – Keith

+0

啊,我明白了。您可以顯式創建服務帳戶憑據,並將其指向JSON文件路徑而不是使用默認憑據。它根據您使用的庫而不同,但要使用'google-auth'使用https://google-auth.readthedocs.io/en/latest/reference/google.oauth2.service_account.html#google.oauth2。 service_account.Credentials.from_service_account_file並使用生成的憑證對象來創建'bigquery.Client(credentials = my_credentials)' –

回答

1

您可以在Python 2.7做在pd.read_gbq功能從傳統的更改默認方言標準。

pd.read_gbq(query, 'my-super-project', dialect='standard') 

事實上,你可以大查詢文檔中讀取參數AllowLargeResults:

AllowLargeResults: For standard SQL queries, this flag is ignored and large results are always allowed.

+0

非常感謝!只需要跟進,你需要改變你選擇表的方式,使用'(反標)而不是[ – Keith

1

我發現了兩種直接導入JSON證書文件的方法。雙方在原有基礎上答案Python BigQuery allowLargeResults with pandas.io.gbq

1)信用添Swast

首先

pip install google-api-python-client 
pip install google-auth 
pip install google-cloud-core 

然後 與

更換

credentials = GoogleCredentials.get_application_default() 

在create_service()

from google.oauth2 import service_account 
credentials = service_account.Credentials.from_service_account_file('path/file.json') 

2)

在代碼中手動設置環境變量等

import os,os.path 
os.environ['GOOGLE_APPLICATION_CREDENTIALS']=os.path.expanduser('path/file.json') 

我寧願方法2,因爲它不要求安裝新的模塊,並且還更接近預期的用途的JSON憑證。

注:

您必須創建一個destinationTable會和信息添加到run_query()

0

這裏是Python 2.7版中完全適用於Windows代碼:

import pandas as pd 
my_qry="<insert your big query here>" 
### Here Put the data from your credentials file of the service account - all fields are available from there### 
my_file="""{ 
    "type": "service_account", 
    "project_id": "cb4recs", 
    "private_key_id": "<id>", 
    "private_key": "<your private key>\n", 
    "client_email": "<email>", 
    "client_id": "<id>", 
    "auth_uri": "https://accounts.google.com/o/oauth2/auth", 
    "token_uri": "https://accounts.google.com/o/oauth2/token", 
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", 
    "client_x509_cert_url": "<x509 url>" 
    }""" 

df=pd.read_gbq(qry,project_id='<your project id>',private_key=my_file) 

這就是它:)