【问题标题】:How to make connection with BigQuery using Python如何使用 Python 与 BigQuery 建立连接
【发布时间】:2017-04-12 08:51:43
【问题描述】:

我是 python 新手,我用过这段代码

from google.cloud import bigquery
from google.cloud.bigquery import  SchemaField


client = bigquery.Client(project='xxxxxxxxxxxxx')

我收到了这个错误

C:\Python27\python.exe "C:/Users/Vikas Chauhan/python_programs/bigQuery.py"
Traceback (most recent call last):
  File "C:/Users/Vikas Chauhan/python_programs/bigQuery.py", line 5, in <module>
    client = bigquery.Client(project='stellar-display-145814')
  File "C:\Python27\lib\site-packages\google\cloud\bigquery\client.py", line 83, in __init__
    project=project, credentials=credentials, _http=_http)
  File "C:\Python27\lib\site-packages\google\cloud\client.py", line 212, in __init__
    Client.__init__(self, credentials=credentials, _http=_http)
  File "C:\Python27\lib\site-packages\google\cloud\client.py", line 128, in __init__
    credentials = get_credentials()
  File "C:\Python27\lib\site-packages\google\cloud\credentials.py", line 39, in get_credentials
    credentials, _ = google.auth.default()
  File "C:\Python27\lib\site-packages\google\auth\_default.py", line 282, in default
    raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or
explicitly create credential and re-run the application. For more
information, please see
https://developers.google.com/accounts/docs/application-default-credentials.

我已经设置了环境变量。我一次又一次地收到这个错误。

我是 python 新手,所以请帮我解决这个问题。我对此一无所知,即使用 python 与 bigquery 建立联系。 谢谢

【问题讨论】:

    标签: python-2.7 google-bigquery


    【解决方案1】:

    首先,您需要一个google-api-client 的python 包。您可以使用 pip 安装它

    pip install --upgrade google-api-python-client
    

    然后设置指向您的服务帐户凭据的环境变量。如果您没有服务帐户,请创建一个新的服务帐户并将 json 凭据下载到您的本地。

    GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json
    

    现在一切都设置好了,写一段python代码就行了,

    from apiclient import discovery
    from oauth2client.client import GoogleCredentials
    
    credentials = GoogleCredentials.get_application_default()
    bq_client = discovery.build('bigquery', 'v2', credentials=credentials)
    

    探索 BigQuery API 和 Google API Explorer。

    【讨论】:

    • 谢谢它的工作。但是有什么方法可以使用 Oauth url 进行连接。
    猜你喜欢
    • 1970-01-01
    • 2021-08-02
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    相关资源
    最近更新 更多