如何从 Google BigQuery 导入使用两个项目中的表的视图

发布于 2025-01-10 05:18:20 字数 985 浏览 4 评论 0原文

当视图内部存在与其他项目的表的联接时,我需要从一个项目中的视图导入。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风铃鹿 2025-01-17 05:18:21

确保在 IAM 中,您当前的项目 SA 可以访问其他项目中的表/视图。错误信息很清楚

Ensure in IAM, your current project SA has access to the tables / views in other project. The error message is very clear

Bonjour°[大白 2025-01-17 05:18:20

请选择适合您需求的选项:

  1. 拥有可以访问这两个项目的帐户

您的 credentials.json 必须有权访问这两个数据集。有关项目访问权限的详细信息,请查看此链接。另外,请检查此 stackoverflow 答案:如何使用 Python 连接 google BigQuery 中的两个不同项目

  1. 使用授权视图

可以说,您无法拥有可以访问这两个项目且具有表限制的帐户。在这种情况下,您将必须使用授权视图。根据定义:

授予视图访问数据集的权限也称为在 BigQuery 中创建授权视图。授权视图允许您与特定用户和组共享查询结果,而无需授予他们访问基础表的权限。您还可以使用视图的 SQL 查询来限制用户能够查询的列(字段)。

尽管如此,您的用户仍需要有权访问存储视图的数据集。

为了让数据分析师查询视图,他们需要被授予对包含视图的数据集的 bigquery.dataViewer 角色。

您可以遵循完整的指南将视图更新为共享视图。查看共享访问视图页面。

Please go with the option that fits your needs:

  1. Have an account that have access to both projects

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.

  1. Use Authorized View

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:

Giving a view access to a dataset is also known as creating an authorized view in BigQuery. An authorized view lets you share query results with particular users and groups without giving them access to the underlying tables. You can also use the view's SQL query to restrict the columns (fields) the users are able to query.

Still, your users will need have access to the dataset that stores the view.

For your data analysts to query the view, they need to be granted the bigquery.dataViewer role on the dataset containing the view.

There is a complete guide you can follow to update the view into a shared view. Check Share Access Views page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文