user7592336
user7592336

Reputation:

How to make connection with BigQuery using Python

I am new in python i had use this code

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


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

i am geting this error

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.

I had already set the environment variable. I am getting this error again and again.

I am new in python so please help me to resolve this. I don't have any single knowledge about this i.e making connection with bigquery using python. thanks

Upvotes: 1

Views: 4905

Answers (1)

bya
bya

Reputation: 46

First, you need a python package of google-api-client. You can install it using pip

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

Then set the environment variable, which points to your service account credentials. If you don't have a service account, create a new service account and download a json credentials to your local.

GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json

Now all things are set, just write a python code,

from apiclient import discovery
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()
bq_client = discovery.build('bigquery', 'v2', credentials=credentials)

Discover the BigQuery APIs and Google APIs Explorer.

Upvotes: 3

Related Questions