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

Python 尝试使用google云运行BigQuery示例,但遇到问题,python,google-cloud-platform,google-bigquery,Python,Google Cloud Platform,Google Bigquery,我试图在python中使用BigQuery来运行一个示例,但我一直收到相同的错误。它表示通过在终端中写入以下内容来设置路径(在pycharm中): 我的路径是: C:\Users\Ejer\PycharmProjects\pythonProject\my json file.json的名称 不知道我的问题是什么 输入: from google.cloud import bigquery # Construct a BigQuery client object. client = bigque

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

我的路径是: 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

C:\Users\Ejer\anaconda3\envs\pythonProject\python.exe C:/Users/Ejer/PycharmProjects/pythonProject/CoinMarketCap.py
回溯(最近一次呼叫最后一次):
文件“C:/Users/Ejer/PycharmProjects/pythonProject/CoinMarketCap.py”,第7行,在
client=bigquery.client()
文件“C:\Users\Ejer\anaconda3\envs\pythonProject\lib\site packages\google\cloud\bigquery\client.py”,第176行,在\uu init中__
超级(客户端,自身)。\uuu初始化__(
文件“C:\Users\Ejer\anaconda3\envs\pythonProject\lib\site packages\google\cloud\client.py”,第249行,在\uuu init中__
_ClientProjectMixin.\uuuuu init\uuuuuu(self,project=project)
文件“C:\Users\Ejer\anaconda3\envs\pythonProject\lib\site packages\google\cloud\client.py”,第201行,在\uuu init中__
项目=自身。确定默认值(项目)
文件“C:\Users\Ejer\anaconda3\envs\pythonProject\lib\site packages\google\cloud\client.py”,第216行,默认为
返回\u确定\u默认\u项目(项目)
文件“C:\Users\Ejer\anaconda3\envs\pythonProject\lib\site packages\google\cloud\\u helpers.py”,第186行,在默认项目中
_,project=google.auth.default()
文件“C:\Users\Ejer\anaconda3\envs\pythonProject\lib\site packages\google\auth\\u default.py”,默认为第356行
引发异常。DefaultCredentialsError(\u帮助\u消息)
google.auth.exceptions.DefaultCredentialsError:无法自动确定凭据。请设置google_应用程序_凭据或显式创建凭据并重新运行应用程序。有关详细信息,请参阅https://cloud.google.com/docs/authentication/getting-started

请务必包含以下代码,它对我有效

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
导入操作系统
服务\帐户\文件=“”
##比如说
##服务\u帐户\u文件='adh\u client.json'
os.environ[“谷歌应用程序凭据”]=服务帐户文件

请务必包含以下代码,它对我有效

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
导入操作系统
服务\帐户\文件=“”
##比如说
##服务\u帐户\u文件='adh\u client.json'
os.environ[“谷歌应用程序凭据”]=服务帐户文件

尝试以下工作示例

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

尝试下面的工作示例

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

谢谢。但这似乎是一个新的错误:google.api_core.exceptions.Forbidden:403 POST:Access Denied:Project ace-sight-299913:用户没有在Project ace-sight-299913中的bigquery.jobs.create权限。你有一个正确的服务帐户文件,即“google_应用程序_凭据”吗?是的,我创建了一个,如示例所示。奇怪…请参考此链接谢谢。但这似乎是一个新的错误:google.api_core.exceptions.Forbidden:403 POST:Access Denied:Project ace-sight-299913:User没有bigquery.jobs.create在Project ace-sight-299913中的权限。您是否有一个正确的服务帐户文件,即“google_应用程序_凭据”?是的,我创建了一个作为前有足够的显示。奇怪…请参考此链接