![](/img/trans.png)
[英]Can I connect to two different websockets servers simultaneously in python?
[英]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}}"""
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()
或者
# 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.