【发布时间】:2022-01-19 22:10:03
【问题描述】:
我打算创建一个脚本来从 Bigquery 中提取数据,但我不知道如何设置环境变量。
这是官方文档中的一个实例:
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"]))
我运行它但返回错误:
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
我关注official doc,但遇到一个问题:第二步是设置环境变量,但它只提供 Windows 和 Linux/macOS 上的实例。那么,如何在 Colab 上设置环境变量呢?
另外,我注意到实例要求我提供关键路径。在本地机器上没问题,但我认为上传我的密钥文件并在我的代码中通过它的链接在线是一个想法。
【问题讨论】:
标签: python google-bigquery environment-variables google-colaboratory