尝试使用google云运行BigQuery示例,但遇到问题

2024-10-03 15:33:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在python中使用BigQuery来运行一个示例,但我一直收到相同的错误。它表示通过在终端中写入以下内容来设置路径(在pycharm中):

set GOOGLE_APPLICATION_CREDENTIALS=[PATH]

我的路径是: C:\Users\Ejer\PycharmProjects\pythonProject\my json file.json的名称

不知道我的问题是什么

输入:


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"]))

输出:

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


Tags: inpyprojectclientdefaultcloudgoogleline
2条回答

尝试下面的工作示例

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

请包括下面的代码,它为我工作

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

相关问题 更多 >