建立授權檢視表


在本教學課程中,您會在 BigQuery 中建立授權的檢視表,供資料分析師使用。已授權檢視表可讓您與特定使用者和群組分享查詢結果,而不用為他們提供基礎來源資料的存取權。檢視表會獲得來源資料的存取權,而非使用者或群組。您也可以使用檢視表的 SQL 查詢,將資料欄和欄位從查詢結果中排除。

使用已授權檢視表的另一種方法,是在來源資料上設定資料欄層級存取權控管,然後讓使用者存取可查詢存取權控管資料的檢視表。如要進一步瞭解資料欄層級存取權控管,請參閱「資料欄層級存取權控管簡介」。

如果您有多個已授權的檢視表可存取相同的來源資料集,您可以授權包含這些檢視表的資料集,而非授權個別檢視表。

目標

  • 建立資料集,用於儲存來源資料。
  • 執行查詢,將資料載入來源資料集中的目的地資料表。
  • 建立包含授權檢視表的資料集。
  • 透過 SQL 查詢建立授權檢視表,限制資料分析師在查詢結果中看到的資料欄。
  • 授予資料分析師執行查詢工作權限。
  • 授予資料分析師對包含已授權檢視表的資料集存取權。
  • 授予已授權檢視表來源資料集的存取權。

費用

在本文件中,您會使用 Google Cloud的下列計費元件:

您可以使用 Pricing Calculator 根據預測用量產生預估費用。 新 Google Cloud 使用者可能符合申請免費試用的資格。

完成本文件所述工作後,您可以刪除已建立的資源,避免繼續計費。詳情請參閱「清除所用資源」。

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  4. Make sure that billing is enabled for your Google Cloud project.

  5. Enable the BigQuery API.

    Enable the API

  6. 請確認您具備必要權限,才能執行本文所述的任務。
  7. 必要的角色

    如果您建立新專案,就會成為專案擁有者,並且會獲得完成本教學課程所需的所有 IAM 權限。

    如果您使用的是現有專案,則需要下列角色。

    Make sure that you have the following role or roles on the project:

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往「身分與存取權管理」頁面
    2. 選取專案。
    3. 按一下 「授予存取權」
    4. 在「New principals」(新增主體) 欄位中輸入使用者 ID。 通常是 Google 帳戶的電子郵件地址。

    5. 在「請選擇角色」清單中,選取角色。
    6. 如要授予其他角色,請按一下 「Add another role」(新增其他角色),然後新增其他角色。
    7. 按一下 [Save]
    8. 如要進一步瞭解 BigQuery 中的角色,請參閱「預先定義的 IAM 角色」。

      所需權限

      如要建立本教學課程中使用的資源,您必須具備下列權限。BigQuery Studio 管理員預先定義的角色會授予所有這些權限。

      • bigquery.datasets.create 建立來源資料集和內含授權檢視表的資料集。
      • bigquery.tables.create 建立儲存來源資料的資料表,並建立授權檢視畫面。
      • bigquery.jobs.create 執行查詢工作,將資料載入來源資料表。
      • bigquery.datasets.getIamPolicybigquery.datasets.get 取得來源資料集和包含已授權檢視表的資料集的 IAM 權限。
      • bigquery.datasets.setIamPolicybigquery.datasets.update 更新來源資料集和包含已授權檢視表的資料集的 IAM 權限。

      如要進一步瞭解 BigQuery 中的 IAM 權限,請參閱「BigQuery 權限」。

建立資料集來儲存來源資料

您一開始可以建立儲存來源資料的資料集。

如要建立來源資料集,請選擇下列任一選項:

主控台

  1. 前往「BigQuery」頁面

    前往 BigQuery

  2. 在「Explorer」窗格中,找到要建立資料集的專案,然後依序點選 「查看動作」>「建立資料集」

  3. 在「Create dataset」頁面上執行下列操作:

    1. 在「Dataset ID」(資料集 ID) 中輸入 github_source_data

    2. 確認「位置類型」已選取「多區域」

    3. 針對「多區域」,請選擇「美國」或「歐盟」。您在本教學課程中建立的所有資源,都應位於相同的多區域位置。

    4. 點選「建立資料集」

SQL

使用 CREATE SCHEMA DDL 陳述式

  1. 前往 Google Cloud 控制台的「BigQuery」頁面。

    前往 BigQuery

  2. 在查詢編輯器中輸入以下陳述式:

    CREATE SCHEMA github_source_data;

  3. 按一下 「Run」

如要進一步瞭解如何執行查詢,請參閱「執行互動式查詢」一文。

Java

在嘗試這個範例之前,請先按照 BigQuery 快速入門:使用用戶端程式庫中的 Java 設定說明進行操作。詳情請參閱 BigQuery Java API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

// Create a source dataset to store your table.
Dataset sourceDataset = bigquery.create(DatasetInfo.of(sourceDatasetId));

Python

在嘗試這個範例之前,請先按照 BigQuery 快速入門:使用用戶端程式庫中的 Python 設定說明進行操作。詳情請參閱 BigQuery Python API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

from google.cloud import bigquery
from google.cloud.bigquery.enums import EntityTypes

client = bigquery.Client()
source_dataset_id = "github_source_data"
source_dataset_id_full = "{}.{}".format(client.project, source_dataset_id)


source_dataset = bigquery.Dataset(source_dataset_id_full)
# Specify the geographic location where the dataset should reside.
source_dataset.location = "US"
source_dataset = client.create_dataset(source_dataset)  # API request

建立資料表並載入來源資料

建立來源資料集後,您可以將 SQL 查詢的結果儲存至目的地資料表,藉此在資料集中填入資料表。查詢會從 GitHub 公開資料集擷取資料。

主控台

  1. 前往「BigQuery」頁面

    前往 BigQuery

  2. 在查詢編輯器中輸入以下查詢:

    SELECT
      commit,
      author,
      committer,
      repo_name
    FROM
      `bigquery-public-data.github_repos.commits`
    LIMIT
      1000;
    
  3. 按一下 [More] (更多) 並選取 [Query settings] (查詢設定)

  4. 在「Destination」(目的地) 部分,選取「Set a destination table for query results」(為查詢結果設定目標資料表)

  5. 在「Dataset」中輸入 PROJECT_ID.github_source_data

    PROJECT_ID 替換為您的專案 ID。

  6. 在「Table ID」(資料表 ID) 中輸入 github_contributors

  7. 按一下 [儲存]

  8. 按一下「執行」

  9. 查詢完成後,請在「Explorer」窗格中展開 github_source_data,然後按一下 github_contributors

  10. 如要確認資料是否已寫入資料表,請點選「Preview」(預覽) 分頁標籤。

Java

在嘗試這個範例之前,請先按照 BigQuery 快速入門:使用用戶端程式庫中的 Java 設定說明進行操作。詳情請參閱 BigQuery Java API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

// Populate a source table
String tableQuery =
    "SELECT commit, author, committer, repo_name"
        + " FROM `bigquery-public-data.github_repos.commits`"
        + " LIMIT 1000";
QueryJobConfiguration queryConfig =
    QueryJobConfiguration.newBuilder(tableQuery)
        .setDestinationTable(TableId.of(sourceDatasetId, sourceTableId))
        .build();
bigquery.query(queryConfig);

Python

在嘗試這個範例之前,請先按照 BigQuery 快速入門:使用用戶端程式庫中的 Python 設定說明進行操作。詳情請參閱 BigQuery Python API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

source_table_id = "github_contributors"
job_config = bigquery.QueryJobConfig()
job_config.destination = source_dataset.table(source_table_id)
sql = """
    SELECT commit, author, committer, repo_name
    FROM `bigquery-public-data.github_repos.commits`
    LIMIT 1000
"""
client.query_and_wait(
    sql,
    # Location must match that of the dataset(s) referenced in the query
    # and of the destination table.
    location="US",
    job_config=job_config,
)  # API request - starts the query and waits for query to finish

建立資料集來儲存已授權的檢視表

建立來源資料集後,您會建立新的獨立資料集,用於儲存與資料分析師分享的已授權檢視表。在後續步驟中,您將授予已授權檢視表存取來源資料集中資料的權限。資料分析師就能存取已授權的檢視畫面,但無法直接存取來源資料。

授權的檢視表應建立在與來源資料不同的資料集中。這樣一來,資料擁有者就能授予使用者已授權檢視權限,而不必同時授予基礎資料的存取權。來源資料資料集和授權檢視資料集必須位於相同的地區位置

如要建立儲存檢視表的資料集,請選擇下列任一選項:

主控台

  1. 前往「BigQuery」頁面

    前往 BigQuery

  2. 在「Explorer」面板中,選取要建立資料集的專案。

  3. 展開 「View actions」 選項,然後按一下「Create dataset」

  4. 在「Create dataset」頁面上執行下列操作:

    1. 在「Dataset ID」(資料集 ID) 中輸入 shared_views

    2. 確認「位置類型」已選取「多區域」

    3. 針對「多區域」,請選擇「美國」或「歐盟」。您在本教學課程中建立的所有資源,都應位於相同的多區域位置。

    4. 點選「建立資料集」

SQL

使用 CREATE SCHEMA DDL 陳述式

  1. 前往 Google Cloud 控制台的「BigQuery」頁面。

    前往 BigQuery

  2. 在查詢編輯器中輸入以下陳述式:

    CREATE SCHEMA shared_views;

  3. 按一下 「Run」

如要進一步瞭解如何執行查詢,請參閱「執行互動式查詢」一文。

Java

在嘗試這個範例之前,請先按照 BigQuery 快速入門:使用用戶端程式庫中的 Java 設定說明進行操作。詳情請參閱 BigQuery Java API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

// Create a separate dataset to store your view
Dataset sharedDataset = bigquery.create(DatasetInfo.of(sharedDatasetId));

Python

在嘗試這個範例之前,請先按照 BigQuery 快速入門:使用用戶端程式庫中的 Python 設定說明進行操作。詳情請參閱 BigQuery Python API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

shared_dataset_id = "shared_views"
shared_dataset_id_full = "{}.{}".format(client.project, shared_dataset_id)


shared_dataset = bigquery.Dataset(shared_dataset_id_full)
shared_dataset.location = "US"
shared_dataset = client.create_dataset(shared_dataset)  # API request

在新資料集中建立授權檢視表

您可在新資料集中建立您想要授權的視圖。這是您與資料分析師分享的視圖。這個檢視表是使用 SQL 查詢建立,該查詢可排除您不希望資料分析師看到的資料欄。

github_contributors 來源資料表包含兩個 RECORD 類型的欄位:authorcommitter。在本教學課程中,您的授權檢視畫面會排除所有作者資料 (除了作者名稱),並排除所有提交者資料 (除了提交者名稱)。

如要在新資料集中建立檢視表,請選擇下列任一選項:

主控台

  1. 前往「BigQuery」頁面

    前往 BigQuery

  2. 在查詢編輯器中輸入以下查詢。

    SELECT
    commit,
    author.name AS author,
    committer.name AS committer,
    repo_name
    FROM
    `PROJECT_ID.github_source_data.github_contributors`;

    PROJECT_ID 替換為您的專案 ID。

  3. 依序點選「儲存」「儲存視圖」

  4. 在「Save View」對話方塊中,執行下列操作:

    1. 在「Project」(專案) 部分,確認已選取專案。

    2. 在「Dataset」中輸入 shared_views

    3. 在「Table」(資料表) 中輸入 github_analyst_view

    4. 按一下 [儲存]

SQL

使用 CREATE VIEW DDL 陳述式

  1. 前往 Google Cloud 控制台的「BigQuery」頁面。

    前往 BigQuery

  2. 在查詢編輯器中輸入以下陳述式:

    CREATE VIEW shared_views.github_analyst_view
    AS (
      SELECT
        commit,
        author.name AS author,
        committer.name AS committer,
        repo_name
      FROM
        `PROJECT_ID.github_source_data.github_contributors`
    );

    PROJECT_ID 替換為您的專案 ID。

  3. 按一下 「Run」

如要進一步瞭解如何執行查詢,請參閱「執行互動式查詢」一文。

Java

在嘗試這個範例之前,請先按照 BigQuery 快速入門:使用用戶端程式庫中的 Java 設定說明進行操作。詳情請參閱 BigQuery Java API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

// Create the view in the new dataset
String viewQuery =
    String.format(
        "SELECT commit, author.name as author, committer.name as committer, repo_name FROM %s.%s.%s",
        projectId, sourceDatasetId, sourceTableId);

ViewDefinition viewDefinition = ViewDefinition.of(viewQuery);

Table view =
    bigquery.create(TableInfo.of(TableId.of(sharedDatasetId, sharedViewId), viewDefinition));

Python

在嘗試這個範例之前,請先按照 BigQuery 快速入門:使用用戶端程式庫中的 Python 設定說明進行操作。詳情請參閱 BigQuery Python API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

shared_view_id = "github_analyst_view"
view = bigquery.Table(shared_dataset.table(shared_view_id))
sql_template = """
    SELECT
        commit, author.name as author,
        committer.name as committer, repo_name
    FROM
        `{}.{}.{}`
"""
view.view_query = sql_template.format(
    client.project, source_dataset_id, source_table_id
)
view = client.create_table(view)  # API request

授予資料分析師執行查詢工作權限

您的資料分析師需要 bigquery.jobs.create 權限才能查詢檢視表,以便執行查詢工作。bigquery.studioUser 角色包含 bigquery.jobs.create 權限。bigquery.studioUser 角色不會授予使用者查看或查詢已授權檢視畫面的權限。在後續步驟中,您會授予資料分析師檢視表的存取權。

如要將資料分析師群組指派給專案層級的 bigquery.studioUser 角色,請執行下列操作:

  1. 前往 Google Cloud 控制台的「IAM」頁面。

    前往身分與存取權管理頁面

  2. 確認已在專案選取器中選取專案。

  3. 按一下 「授予存取權」

  4. 在「Grant access to」對話方塊中,執行下列操作:

    1. 在「新增主體」欄位中,輸入包含資料分析師的群組。例如:data_analysts@example.com

    2. 在「Select a role」(請選擇角色) 欄位中,搜尋並選取「BigQuery Studio User」(BigQuery Studio 使用者) 角色。

    3. 按一下 [儲存]

授權資料分析師查詢已授權的檢視表

資料分析師需要在資料集層級或檢視表層級獲得 bigquery.dataViewer 角色,才能查詢檢視表。在資料集層級授予這個角色,即可讓分析師存取資料集中的所有資料表和檢視畫面。由於本教學課程中建立的資料集包含單一已授權檢視表,因此您是在資料集層級授予存取權。如果您有需要授予存取權的已授權檢視表集合,建議改用已授權的資料集

您先前授予資料分析師的 bigquery.studioUser 角色,會為他們提供建立查詢工作的必要權限。不過,他們必須同時具備已授權檢視表或內含該檢視表的資料集的 bigquery.dataViewer 存取權,才能成功查詢檢視表。

如要授予資料分析師對包含已授權檢視表的資料集的 bigquery.dataViewer 存取權,請按照下列步驟操作:

主控台

  1. 前往「BigQuery」頁面

    前往 BigQuery

  2. 在「Explorer」窗格中,選取 shared_views 資料集。

  3. 依序按一下「分享」圖示 >「權限」

  4. 在「Share permissions」窗格中,按一下「Add principal」

  5. 在「New principals」(新增主體) 部分,輸入包含資料分析師的群組,例如 data_analysts@example.com

  6. 按一下「Select a role」(選取角色),然後依序選取「BigQuery」>「BigQuery Data Viewer」

  7. 按一下 [儲存]

  8. 按一下 [關閉]

Java

在嘗試這個範例之前,請先按照 BigQuery 快速入門:使用用戶端程式庫中的 Java 設定說明進行操作。詳情請參閱 BigQuery Java API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

// Assign access controls to the dataset containing the view
List<Acl> viewAcl = new ArrayList<>(sharedDataset.getAcl());
viewAcl.add(Acl.of(new Acl.Group("example-analyst-group@google.com"), Acl.Role.READER));
sharedDataset.toBuilder().setAcl(viewAcl).build().update();

Python

在嘗試這個範例之前,請先按照 BigQuery 快速入門:使用用戶端程式庫中的 Python 設定說明進行操作。詳情請參閱 BigQuery Python API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

# analyst_group_email = 'data_analysts@example.com'
access_entries = shared_dataset.access_entries
access_entries.append(
    bigquery.AccessEntry("READER", EntityTypes.GROUP_BY_EMAIL, analyst_group_email)
)
shared_dataset.access_entries = access_entries
shared_dataset = client.update_dataset(
    shared_dataset, ["access_entries"]
)  # API request

授權視圖存取來源資料集

為內含授權檢視表的資料集建立存取權控制後,您就可以授予授權檢視表對來源資料集的存取權。這項授權會授予檢視表對來源資料的存取權,但不會授予資料分析師群組存取權。

如要授予已授權的檢視表存取來源資料的權限,請選擇下列任一選項:

主控台

  1. 前往「BigQuery」頁面

    前往 BigQuery

  2. 在「Explorer」窗格中,選取 github_source_data 資料集。

  3. 依序按一下「分享」>「授權觀看」

  4. 在「Authorized views」(已授權檢視表) 窗格中,輸入「Authorized view」(已授權檢視表) 的 PROJECT_ID.shared_views.github_analyst_view

    PROJECT_ID 替換為您的專案 ID。

  5. 按一下「新增授權」

Java

在嘗試這個範例之前,請先按照 BigQuery 快速入門:使用用戶端程式庫中的 Java 設定說明進行操作。詳情請參閱 BigQuery Java API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

// Authorize the view to access the source dataset
List<Acl> srcAcl = new ArrayList<>(sourceDataset.getAcl());
srcAcl.add(Acl.of(new Acl.View(view.getTableId())));
sourceDataset.toBuilder().setAcl(srcAcl).build().update();

Python

在嘗試這個範例之前,請先按照 BigQuery 快速入門:使用用戶端程式庫中的 Python 設定說明進行操作。詳情請參閱 BigQuery Python API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

access_entries = source_dataset.access_entries
access_entries.append(
    bigquery.AccessEntry(None, EntityTypes.VIEW, view.reference.to_api_repr())
)
source_dataset.access_entries = access_entries
source_dataset = client.update_dataset(
    source_dataset, ["access_entries"]
)  # API request

驗證設定

設定完成後,資料分析師群組的成員 (例如 data_analysts) 可以透過查詢檢視表來驗證設定。

如要驗證設定,資料分析師應執行下列查詢:

  1. 前往「BigQuery」頁面

    前往 BigQuery

  2. 在查詢編輯器中輸入以下陳述式:

    SELECT
      *
    FROM
      `PROJECT_ID.shared_views.github_analyst_view`;

    PROJECT_ID 替換為您的專案 ID。

  3. 按一下 「Run」

查詢結果會如下所示:結果中只會顯示作者名稱和提交者名稱。

查詢授權檢視表後的查詢結果

如要進一步瞭解如何執行查詢,請參閱「執行互動式查詢」一文。

完整原始碼

以下是供您參考的完整教學課程原始碼。

Java

在嘗試這個範例之前,請先按照 BigQuery 快速入門:使用用戶端程式庫中的 Java 設定說明進行操作。詳情請參閱 BigQuery Java API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

// Create a source dataset to store your table.
Dataset sourceDataset = bigquery.create(DatasetInfo.of(sourceDatasetId));

// Populate a source table
String tableQuery =
    "SELECT commit, author, committer, repo_name"
        + " FROM `bigquery-public-data.github_repos.commits`"
        + " LIMIT 1000";
QueryJobConfiguration queryConfig =
    QueryJobConfiguration.newBuilder(tableQuery)
        .setDestinationTable(TableId.of(sourceDatasetId, sourceTableId))
        .build();
bigquery.query(queryConfig);

// Create a separate dataset to store your view
Dataset sharedDataset = bigquery.create(DatasetInfo.of(sharedDatasetId));

// Create the view in the new dataset
String viewQuery =
    String.format(
        "SELECT commit, author.name as author, committer.name as committer, repo_name FROM %s.%s.%s",
        projectId, sourceDatasetId, sourceTableId);

ViewDefinition viewDefinition = ViewDefinition.of(viewQuery);

Table view =
    bigquery.create(TableInfo.of(TableId.of(sharedDatasetId, sharedViewId), viewDefinition));

// Assign access controls to the dataset containing the view
List<Acl> viewAcl = new ArrayList<>(sharedDataset.getAcl());
viewAcl.add(Acl.of(new Acl.Group("example-analyst-group@google.com"), Acl.Role.READER));
sharedDataset.toBuilder().setAcl(viewAcl).build().update();

// Authorize the view to access the source dataset
List<Acl> srcAcl = new ArrayList<>(sourceDataset.getAcl());
srcAcl.add(Acl.of(new Acl.View(view.getTableId())));
sourceDataset.toBuilder().setAcl(srcAcl).build().update();

Python

在嘗試這個範例之前,請先按照 BigQuery 快速入門:使用用戶端程式庫中的 Python 設定說明進行操作。詳情請參閱 BigQuery Python API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

# Create a source dataset
from google.cloud import bigquery
from google.cloud.bigquery.enums import EntityTypes

client = bigquery.Client()
source_dataset_id = "github_source_data"
source_dataset_id_full = "{}.{}".format(client.project, source_dataset_id)


source_dataset = bigquery.Dataset(source_dataset_id_full)
# Specify the geographic location where the dataset should reside.
source_dataset.location = "US"
source_dataset = client.create_dataset(source_dataset)  # API request

# Populate a source table
source_table_id = "github_contributors"
job_config = bigquery.QueryJobConfig()
job_config.destination = source_dataset.table(source_table_id)
sql = """
    SELECT commit, author, committer, repo_name
    FROM `bigquery-public-data.github_repos.commits`
    LIMIT 1000
"""
client.query_and_wait(
    sql,
    # Location must match that of the dataset(s) referenced in the query
    # and of the destination table.
    location="US",
    job_config=job_config,
)  # API request - starts the query and waits for query to finish

# Create a separate dataset to store your view
shared_dataset_id = "shared_views"
shared_dataset_id_full = "{}.{}".format(client.project, shared_dataset_id)


shared_dataset = bigquery.Dataset(shared_dataset_id_full)
shared_dataset.location = "US"
shared_dataset = client.create_dataset(shared_dataset)  # API request

# Create the view in the new dataset
shared_view_id = "github_analyst_view"
view = bigquery.Table(shared_dataset.table(shared_view_id))
sql_template = """
    SELECT
        commit, author.name as author,
        committer.name as committer, repo_name
    FROM
        `{}.{}.{}`
"""
view.view_query = sql_template.format(
    client.project, source_dataset_id, source_table_id
)
view = client.create_table(view)  # API request

# Assign access controls to the dataset containing the view
# analyst_group_email = 'data_analysts@example.com'
access_entries = shared_dataset.access_entries
access_entries.append(
    bigquery.AccessEntry("READER", EntityTypes.GROUP_BY_EMAIL, analyst_group_email)
)
shared_dataset.access_entries = access_entries
shared_dataset = client.update_dataset(
    shared_dataset, ["access_entries"]
)  # API request

# Authorize the view to access the source dataset
access_entries = source_dataset.access_entries
access_entries.append(
    bigquery.AccessEntry(None, EntityTypes.VIEW, view.reference.to_api_repr())
)
source_dataset.access_entries = access_entries
source_dataset = client.update_dataset(
    source_dataset, ["access_entries"]
)  # API request

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。

刪除專案

控制台

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

gcloud

    Delete a Google Cloud project:

    gcloud projects delete PROJECT_ID

刪除個別資源

如要移除本教學課程中使用的個別資源,請執行下列操作:

  1. 刪除授權檢視畫面

  2. 刪除包含已授權檢視表的資料集

  3. 在來源資料集中刪除資料表

  4. 刪除來源資料集

由於您已建立本教學課程中使用的資源,因此無須額外權限即可刪除這些資源。

後續步驟