How to query BigQuery programmatically from Python without end-user interaction?

前端 未结 3 378
迷失自我
迷失自我 2021-02-04 11:00

This question seems like it should be so simple to answer, but after days of research and several dead ends, I can\'t seem to get query results out of BigQuery with

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-04 11:43

    If you login locally with gcloud:

    gcloud auth application-default login
    

    Then a credentials file will be stored in ~/.config/gcloud/, which can be loaded with:

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

    Or you can just load the BQ security key file directly with:

    from google.cloud import bigquery
    client = bigquery.Client.from_service_account_json(path_to_key.json)
    

提交回复
热议问题