简体   繁体   中英

Access data via BigQuery in python

I am trying to access data in python using bigquery api , here is my code.

I have placed the pem file inside the same folder but script returns an error "googleapiclient.errors.HttpError: https://www.googleapis.com/bigquery/v2/projects/digin-1086/queries?alt=json returned "Not found: Table 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)

The error says it can't find your table, nothing to do with the pem file. You need to make the table exits in the dataset.

To access data via BigQuery in python you can do the following:

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)}

    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM