如何从 Google BigQuery 导入使用两个项目中的表的视图
当视图内部存在与其他项目的表的联接时,我需要从一个项目中的视图导入。
import pandas as pd
from google.cloud import bigquery
from google.oauth2 import service_account
#----------BigQuery Connect---------------
cred_1 = service_account.Credentials.from_service_account_file(filename="project1.json")
cred_2 = service_account.Credentials.from_service_account_file(filename="project2.json")
client = bigquery.Client(credentials=cred_1, project=cred.project_id)
medicare = client.dataset('dataset', project='project1')
# --------- from Bigquery-----------------------------------
statment = """select * FROM `project1.dataset.view`"""
query_job = client.query(statment )
result = query_job.result()
df = result.to_dataframe()
df.head()
由于视图中使用了另一个项目的表,因此出现错误:
Forbidden: 403 Access Denied: Table project2:dataset.table: User does not have permission to query table project2:dataset.table.
我有两个 JSON 文件,其中包含两个项目的凭据,但不清楚如何组合它们以访问两个项目。
这样的问题如何解决呢?
I need to import from the view that is in one project when inside view there are joins to tables from other project.
import pandas as pd
from google.cloud import bigquery
from google.oauth2 import service_account
#----------BigQuery Connect---------------
cred_1 = service_account.Credentials.from_service_account_file(filename="project1.json")
cred_2 = service_account.Credentials.from_service_account_file(filename="project2.json")
client = bigquery.Client(credentials=cred_1, project=cred.project_id)
medicare = client.dataset('dataset', project='project1')
# --------- from Bigquery-----------------------------------
statment = """select * FROM `project1.dataset.view`"""
query_job = client.query(statment )
result = query_job.result()
df = result.to_dataframe()
df.head()
Since there is in the view the use of tables from another project, I get an error:
Forbidden: 403 Access Denied: Table project2:dataset.table: User does not have permission to query table project2:dataset.table.
I have two JSON files with credentials to two projects, but it's not clear how to combine them to give access to two projects.
How can such a problem be solved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保在 IAM 中,您当前的项目 SA 可以访问其他项目中的表/视图。错误信息很清楚
Ensure in IAM, your current project SA has access to the tables / views in other project. The error message is very clear
请选择适合您需求的选项:
您的
credentials.json
必须有权访问这两个数据集。有关项目访问权限的详细信息,请查看此链接。另外,请检查此 stackoverflow 答案:如何使用 Python 连接 google BigQuery 中的两个不同项目。可以说,您无法拥有可以访问这两个项目且具有表限制的帐户。在这种情况下,您将必须使用
授权视图
。根据定义:尽管如此,您的用户仍需要有权访问存储视图的数据集。
您可以遵循完整的指南将视图更新为共享视图。查看共享访问视图页面。
Please go with the option that fits your needs:
Your
credentials.json
must have access to both datasets. For details about access to projects check this link. Also, check this stackoverflow answer: How can I connect two different project in google BigQuery in Python.Lets said that you are unable to have an account with access to both projects and have table restrictions. On that scenario, you will have to work with
authorized view
. As per definition:Still, your users will need have access to the dataset that stores the view.
There is a complete guide you can follow to update the view into a shared view. Check Share Access Views page.