Dfhaa_DK
Dfhaa_DK

Reputation: 131

Trying to run a BigQuery example with google cloud, but getting issues

I am trying to run an example with using BigQuery with python, but i keep getting the same error. It says to set the path by writing this in the terminal (in pycharm):

set GOOGLE_APPLICATION_CREDENTIALS=[PATH]

Where my path is: C:\Users\Ejer\PycharmProjects\pythonProject\the name of my json file.json

Not sure what my problem is.

Input:


from google.cloud import bigquery

# Construct a BigQuery client object.
client = bigquery.Client()

query = """
    SELECT name, SUM(number) as total_people
    FROM `bigquery-public-data.usa_names.usa_1910_2013`
    WHERE state = 'TX'
    GROUP BY name, state
    ORDER BY total_people DESC
    LIMIT 20
"""
query_job = client.query(query)  # Make an API request.

print("The query data:")
for row in query_job:
    # Row values can be accessed by field name or index.
    print("name={}, count={}".format(row[0], row["total_people"]))

Output:

C:\Users\Ejer\anaconda3\envs\pythonProject\python.exe C:/Users/Ejer/PycharmProjects/pythonProject/CoinMarketCap.py
Traceback (most recent call last):
  File "C:/Users/Ejer/PycharmProjects/pythonProject/CoinMarketCap.py", line 7, in <module>
    client = bigquery.Client()
  File "C:\Users\Ejer\anaconda3\envs\pythonProject\lib\site-packages\google\cloud\bigquery\client.py", line 176, in __init__
    super(Client, self).__init__(
  File "C:\Users\Ejer\anaconda3\envs\pythonProject\lib\site-packages\google\cloud\client.py", line 249, in __init__
    _ClientProjectMixin.__init__(self, project=project)
  File "C:\Users\Ejer\anaconda3\envs\pythonProject\lib\site-packages\google\cloud\client.py", line 201, in __init__
    project = self._determine_default(project)
  File "C:\Users\Ejer\anaconda3\envs\pythonProject\lib\site-packages\google\cloud\client.py", line 216, in _determine_default
    return _determine_default_project(project)
  File "C:\Users\Ejer\anaconda3\envs\pythonProject\lib\site-packages\google\cloud\_helpers.py", line 186, in _determine_default_project
    _, project = google.auth.default()
  File "C:\Users\Ejer\anaconda3\envs\pythonProject\lib\site-packages\google\auth\_default.py", line 356, in default
    raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started

Upvotes: 0

Views: 411

Answers (2)

Vibhor Gupta
Vibhor Gupta

Reputation: 709

Try out following working example

from dateutil.parser import parse
from google.cloud import bigquery

stream_query = """create table `proj.dataset.YOUR_NEW_TABLE4`  partition                                                                              by date(_time) as SELECT * FROM `proj.dataset.YOUR_NEW_TABLE1` WHERE 1=                                                                             2"""

stream_client = bigquery.Client()

stream_Q = stream_client.query(stream_query)
stream_data_df = stream_Q.to_dataframe()

Upvotes: 1

Vikrant chaudhary
Vikrant chaudhary

Reputation: 140

Kindly do include the below code, it worked for me

import os
SERVICE_ACCOUNT_FILE='<enter your_json file here>'

## for example 
## SERVICE_ACCOUNT_FILE='adh_client.json'

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]=SERVICE_ACCOUNT_FILE

Upvotes: 1

Related Questions