我试图用python运行一个使用BigQuery的示例,但我一直收到同样的错误。它说通过在终端中写下这个来设置路径(在pycharm中(:
set GOOGLE_APPLICATION_CREDENTIALS=[PATH]
我的路在哪里:C: \Users\Ejer\PycharmProjects\pythonProject\我的json文件.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:UsersEjeranaconda3envspythonProjectpython.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:UsersEjeranaconda3envspythonProjectlibsite-packagesgooglecloudbigqueryclient.py", line 176, in __init__
super(Client, self).__init__(
File "C:UsersEjeranaconda3envspythonProjectlibsite-packagesgooglecloudclient.py", line 249, in __init__
_ClientProjectMixin.__init__(self, project=project)
File "C:UsersEjeranaconda3envspythonProjectlibsite-packagesgooglecloudclient.py", line 201, in __init__
project = self._determine_default(project)
File "C:UsersEjeranaconda3envspythonProjectlibsite-packagesgooglecloudclient.py", line 216, in _determine_default
return _determine_default_project(project)
File "C:UsersEjeranaconda3envspythonProjectlibsite-packagesgooglecloud_helpers.py", line 186, in _determine_default_project
_, project = google.auth.default()
File "C:UsersEjeranaconda3envspythonProjectlibsite-packagesgoogleauth_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
请包含以下代码,它对我有效
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
尝试以下工作示例
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()