如何从使用两个项目表的 Google BigQuery 导入视图
How to import view from Google BigQuery that used tables from two projects
我需要从一个项目中的视图导入,而在内部视图中存在与其他项目中的表的连接。
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 文件,其中包含两个项目的凭据,但不清楚如何组合它们以授予对两个项目的访问权限。
这样的问题如何解决?
确保在 IAM 中,您当前的项目 SA 可以访问其他项目中的表/视图。报错信息很清楚
请选择适合您需要的选项:
- 拥有一个可以访问这两个项目的帐户
您的 credentials.json
必须有权访问这两个数据集。有关访问项目的详细信息,请查看此 link. Also, check this Whosebug answer: .
- 使用授权视图
假设您无法拥有一个可以访问这两个项目的帐户并且有 table 限制。在这种情况下,您将不得不使用 authorized view
。根据定义:
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.
不过,您的用户仍需要能够访问存储视图的数据集。
For your data analysts to query the view, they need to be granted the bigquery.dataViewer role on the dataset containing the view.
您可以按照完整指南将视图更新为共享视图。检查 Share Access Views 页。
我需要从一个项目中的视图导入,而在内部视图中存在与其他项目中的表的连接。
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 文件,其中包含两个项目的凭据,但不清楚如何组合它们以授予对两个项目的访问权限。
这样的问题如何解决?
确保在 IAM 中,您当前的项目 SA 可以访问其他项目中的表/视图。报错信息很清楚
请选择适合您需要的选项:
- 拥有一个可以访问这两个项目的帐户
您的 credentials.json
必须有权访问这两个数据集。有关访问项目的详细信息,请查看此 link. Also, check this Whosebug answer:
- 使用授权视图
假设您无法拥有一个可以访问这两个项目的帐户并且有 table 限制。在这种情况下,您将不得不使用 authorized view
。根据定义:
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.
不过,您的用户仍需要能够访问存储视图的数据集。
For your data analysts to query the view, they need to be granted the bigquery.dataViewer role on the dataset containing the view.
您可以按照完整指南将视图更新为共享视图。检查 Share Access Views 页。