[英]How can I connect to two different projects in Google BigQuery in Python?
I'm trying to connect to two different BigQuery projects in Python, but I'm not sure how to do it.我正在尝试使用 Python 连接到两个不同的 BigQuery 项目,但我不知道该怎么做。 I have two JSON keys, both of which work when run separately, but I want to run them together so I can do a join from two different projects.
我有两个 JSON 密钥,它们在单独运行时都可以工作,但我想将它们一起运行,这样我就可以从两个不同的项目中进行连接。
Here is my code:这是我的代码:
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")
This code is accepted.此代码已被接受。
Here is where there is a problem:这是有问题的地方:
sales = client.query(sql).to_dataframe()
I know this will bring in one project, but how do I bring in the second if my sql query has a join?我知道这会引入一个项目,但是如果我的 sql 查询有连接,我该如何引入第二个项目?
Thanks in advance!提前致谢!
write a SQL query with joins from both tables from both the projects.使用来自两个项目的两个表的联接编写 SQL 查询。 Execute it as a single query.
将其作为单个查询执行。 You need not to create different big query clients instead
您无需创建不同的大查询客户端
sameple_query = f"""select * from {{project1}}.{{dataset1}}.{{table1}} left join select * from {{project2}}.{{dataset2}}.{{table2}}"""
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()
Or或者
# 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.