我最近建立了一个GCP帐户+ BigQuery数据库,并试图将其连接到R和Python上以进行一些数据科学工作。我已经用bigquery admin的凭据创建了一个服务帐户。以下内容通过RStudio控制台在R中对我来说正常工作:
library(bigrquery)
bigrquery::bq_auth(path = '/Users/myname/mypath/my-bigquery-cred.json')
project_id <- 'myprojectid'
dataset_name <- 'mydatasetname'
ds <- bq_dataset(project = project_id, dataset = dataset_name)
my_query <- "SELECT * FROM somedataset.sometable LIMIT 10"
my_table <- bq_dataset_query(ds, query = my_query)
my_dataframe <- bq_table_download(my_table)
当我运行代码的bq_auth
行时,收到以下消息:
The bigrquery package is requesting access to your Google account. Select a pre-authorised account or enter '0' to obtain a new token. Press Esc/Ctrl + C to abort.
1: [email protected]
Selection:
并且我选择1并按预期工作,通常,bigrquery
库在这里按预期工作。但是,当我尝试在jupyter笔记本中的Python中运行以下代码时:
from google.cloud import bigquery
client = bigquery.Client()
...我在找不到凭据时收到以下标准错误消息:
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
我收到以下命令后,我相当确定我已设置好GOOGLE_APPLICATION_CREDENTIALS
,因为我从命令行运行了以下命令:
My-MBP-5:~ myname$ export GOOGLE_APPLICATION_CREDENTIALS="/Users/myname/my_bigquery-cred.json"
My-MBP-5:~ myname$ echo $GOOGLE_APPLICATION_CREDENTIALS
/Users/myname/my_bigquery-cred.json
My-MBP-5:~ nyname$
...由于某种原因,即使R和RStudio能够找到,我的Jupyter Notebook也找不到此env变量。关于为何可能会出现这种情况,有什么建议吗?我该怎么办?也许有一种方法可以检查Jupyter Notebook是否可以以与GOOGLE_APPLICATION_CREDENTIALS
相同的方式查看echo $
?
谢谢!
[以下文章中的第4步-https://jingsblog.com/2018/11/27/connect-your-jupyter-notebook-to-the-bigquery/-可能解决了我的问题。