通过python中的BigQuery访问数据

时间:2015-10-02 10:14:18

标签: python file google-bigquery

我正在尝试使用bigquery api访问python中的数据,这是我的代码。

我已将pem文件放在同一文件夹中但脚本返回错误" googleapiclient.errors.HttpError:https://www.googleapis.com/bigquery/v2/projects/digin-1086 / queries?alt = json返回"未找到:表digin-1086:dataset.my_table">

 from bigquery import get_client
    # BigQuery project id as listed in the Google Developers Console.
    project_id = 'digin-1086'
    # Service account email address as listed in the Google Developers Console.
    service_account = '77441948210-4fhu1kc1driicjecriqupndkr60npnh@developer.gserviceaccount.com'
    # PKCS12 or PEM key provided by Google.
    key = 'Digin-d6387c00c5a'
    client = get_client(project_id, service_account=service_account,
                        private_key_file=key, readonly=True)
    # Submit an async query.
    job_id, _results = client.query('SELECT * FROM dataset.my_table LIMIT 1000')
    # Check if the query has finished running.
    complete, row_count = client.check_job(job_id)
    # Retrieve the results.
    results = client.get_query_rows(job_id)

2 个答案:

答案 0 :(得分:0)

错误说它无法找到你的表,与pem文件无关。您需要在数据集中退出表。

答案 1 :(得分:0)

要通过python中的BigQuery访问数据,您可以执行以下操作:

from google.cloud import bigquery
from google.oauth2 import service_account
from google.auth.transport import requests


credentials = service_account.Credentials.from_service_account_file(
    r'filelocation\xyz.json')

project_id = 'abc'

client = bigquery.Client(credentials= credentials,project=project_id)


query_job = client.query("""
  SELECT *
  FROM tabename
  LIMIT 10""")
results = query_job.result()

for row in results:
        print(row)}