繁体   English   中英

如何在 Python 中连接到 Google BigQuery 中的两个不同项目?

[英]How can I connect to two different projects in Google BigQuery in Python?

我正在尝试使用 Python 连接到两个不同的 BigQuery 项目,但我不知道该怎么做。 我有两个 JSON 密钥,它们在单独运行时都可以工作,但我想将它们一起运行,这样我就可以从两个不同的项目中进行连接。

这是我的代码:

credentials = service_account.Credentials.from_service_account_file('C:\\Users\\zuser\\Desktop\\JSON_Keys\\firstjsonkey.json')
credentials_2ndproject = service_account.Credentials.from_service_account_file('C:\\Users\\zuser\\Desktop\\JSON_Keys\\2ndjsonkey.json')

client = bigquery.Client(credentials=credentials, project="project1")
client_2ndproject = bigquery.Client(credentials=credentials_2ndproject, project="project2")

此代码已被接受。

这是有问题的地方:

sales = client.query(sql).to_dataframe()

我知道这会引入一个项目,但是如果我的 sql 查询有连接,我该如何引入第二个项目?

提前致谢!

使用来自两个项目的两个表的联接编写 SQL 查询。 将其作为单个查询执行。 您无需创建不同的大查询客户端

sameple_query = f"""select * from {{project1}}.{{dataset1}}.{{table1}} left join select * from {{project2}}.{{dataset2}}.{{table2}}"""

1. 使用对两个项目都具有权限的服务帐户来创建凭据对象和大查询客户端创建。

credentials = service_account.Credentials.from_service_account_file('service_account_key_for_both_projects_bigquery_access.json')
bqclient = bigquery.Client(credentials=credentials)
bqclient.query(sample_query).result().to_dataframe()

注意:result() 将等待查询完成,然后返回数据帧。

或者

2. 如果您的用户有权访问这两个项目,您可以使用来自任何项目的 bigquery 客户端的相同查询。

# getting the credentials and project details for gcp project
    credentials, your_project_id = google.auth.default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
# creating a big query clients
    bqclient = bigquery.Client(
        credentials=credentials,
        project=your_project_id
    )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM