简体   繁体   English

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

[英]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}}"""

1. use a service account with permissions on both projects to create the credentials object and big query client creation. 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()

Note: result() will wait for the query to complete and then return the dataframe.注意:result() 将等待查询完成,然后返回数据帧。

Or或者

2. If your user has access to both projects, you can use the same query with bigquery client from any of the project. 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.

相关问题 我可以在python中同时连接到两个不同的websockets服务器吗? - Can I connect to two different websockets servers simultaneously in python? 使用python如何同时连接到两个不同的api,以便可以比较数据? - Using python how do I connect to two different api s at the same time so I can compare data? 使用Python通过Cloudfunctions将Dialogflow与Google BigQuery连接 - Connect Dialogflow with Google BigQuery via Cloudfunctions with Python 使用 Google Cloud Functions 在两个 BigQuery 项目之间传输数据 - Data Transfer between two BigQuery Projects using Google Cloud Functions 如何连接图形的两个部分? 在蟒蛇 - How can I connect two portion of my graph? in python 如何根据 python 中的多个标准连接两个数据帧? - How can I connect two dataframes based on mutliple criteria in python? 我可以通过两个不同的 Django 项目传递数据吗? - Can I pass data through two different django projects? 如何用 python 插座连接两台电脑? - How can i connect two computers with python socket? 我如何比较python中两行不同的两列 - How can i compare two columns in two different rows in python 如何在 Databricks/Spark 中读取 Google BigQuery 查看数据 - How can I read Google BigQuery View data in Databricks/Spark
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM