追蹤 BigQuery 資料表的資料歷程
本文說明如何追蹤 BigQuery 資料表中的資料沿革。資料歷程是追蹤資料來源、轉換方式和隨時間移動位置的過程。瞭解資料歷程對於確保法規遵循、排解資料問題及執行根本原因分析至關重要。
本快速入門導覽課程說明如何開始使用 BigQuery 資料表的資料沿襲功能:
從公開的
new_york_taxi_trips資料集複製兩個資料表。將兩個資料表中的計程車趟次總數合併到新資料表。
查看這三項作業的沿襲關係視覺化圖表。
事前準備
設定專案:
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Dataplex, BigQuery, and Data Lineage APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Dataplex, BigQuery, and Data Lineage APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
必要的角色
如要取得查看歷程資料視覺化圖表所需的權限,請要求管理員授予您下列 IAM 角色:
-
Dataplex Catalog 檢視者 (
roles/dataplex.catalogViewer) Dataplex Universal Catalog 資源專案 -
資料沿襲檢視者 (
roles/datalineage.viewer) 在您使用 BigQuery 的專案中 -
您在 BigQuery 專案中使用的 BigQuery 資料檢視者 (
roles/bigquery.dataViewer)
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
將公開資料集新增至專案
前往 Google Cloud 控制台的「BigQuery」頁面。
點選左側窗格中的 「Explorer」。

如果沒有看到左側窗格,請按一下「展開左側窗格」圖示 開啟窗格。
在「Explorer」窗格中,點選「新增資料」。
在「新增資料」窗格中,選取「公開資料集」。
在「市集」窗格中搜尋
NYC TLC Trips,然後點按「NYC TLC Trips」結果。點選「查看資料集」。
這會將公開資料集的專案新增為參照,您可以在「Explorer」窗格中查看。詳細資料窗格會顯示「資料集資訊」,包括「資料集 ID」、「資料位置」和「上次修改」日期等資訊。
在專案中建立資料集
點選左側窗格中的 「Explorer」。

在「Explorer」窗格中,選取要建立資料集的專案。
按一下「動作」,然後點選「建立資料集」。
在「Create dataset」(建立資料集) 頁面的「Dataset ID」(資料集 ID) 欄位中,輸入:
data_lineage_demo。其他欄位則保留預設值。點選「建立資料集」。
在「Explorer」窗格中,點選「Datasets」,然後點選新加入的
data_lineage_demo。
詳細資料窗格會顯示資料集的資訊。
將兩個可公開存取的資料表複製到資料集
開啟查詢編輯器:在詳細資料窗格中,點選「
data_lineage_demo」分頁旁的 「SQL 查詢」。這個步驟會建立名為「Untitled」的分頁。在查詢編輯器中輸入下列查詢,複製第一個表格。將
PROJECT_ID替換為專案的 ID。CREATE TABLE `PROJECT_ID.data_lineage_demo.nyc_green_trips_2021` COPY `bigquery-public-data.new_york_taxi_trips.tlc_green_trips_2021`按一下「執行」,這個步驟會建立第一個資料表,稱為
nyc_green_trips_2021。在「Query results」(查詢結果) 窗格中,點選「Go to table」(前往資料表)。這個步驟會顯示第一個資料表的內容。
在查詢編輯器中,將先前的查詢換成下列查詢,即可複製第二個資料表。將
PROJECT_ID替換為專案的 ID。CREATE TABLE `PROJECT_ID.data_lineage_demo.nyc_green_trips_2022` COPY `bigquery-public-data.new_york_taxi_trips.tlc_green_trips_2022`按一下「執行」,這個步驟會建立第二個資料表,名為
nyc_green_trips_2022。在「Query results」(查詢結果) 窗格中,點選「Go to table」(前往資料表)。這個步驟會顯示第二個表格的內容。
將資料匯總至新資料表
在查詢編輯器中輸入下列查詢。將
PROJECT_ID替換為專案 ID。CREATE TABLE `PROJECT_ID.data_lineage_demo.total_green_trips_22_21` AS SELECT vendor_id, COUNT(*) AS number_of_trips FROM ( SELECT vendor_id FROM `PROJECT_ID.data_lineage_demo.nyc_green_trips_2022` UNION ALL SELECT vendor_id FROM `PROJECT_ID.data_lineage_demo.nyc_green_trips_2021` ) GROUP BY vendor_id按一下「執行」,這個步驟會建立名為
total_green_trips_22_21的合併資料表。在「Query results」(查詢結果) 窗格中,點選「Go to table」(前往資料表)。這個步驟會顯示合併後的資料表。
在 Dataplex Universal Catalog 中查看歷程圖
在 Google Cloud 控制台,前往 Dataplex Universal Catalog 的「Search」(搜尋) 頁面。
如果搜尋平台設為「Data Catalog」,請在「選擇搜尋平台」選單中選取「Dataplex Universal Catalog」。
在「搜尋」方塊中輸入
total_green_trips_22_21,然後按一下「搜尋」。在結果清單中,按一下
total_green_trips_22_21。這個步驟會顯示 BigQuery 資料表的「詳細資料」分頁。按一下「歷程」分頁標籤。
在沿襲圖中,每個矩形節點都代表一個資料表,可能是原始、複製或合併的資料表。您可以採取以下做法:
如要顯示或隱藏表格來源,請按一下「+」(展開) 或「-」(收合)。
如要顯示表格資訊,請按一下節點。這個步驟會顯示節點「詳細資料」窗格。
如要顯示程序資訊,請按一下
。這個步驟會顯示「詳細資料」窗格,當中會顯示將來源資料表轉換為目標資料表的工作。
清除所用資源
為了避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。
刪除專案
- 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。
- 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
- 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。
刪除資料集
前往 Google Cloud 控制台的「BigQuery」頁面。
點選左側窗格中的 「Explorer」。

在「Explorer」窗格中,搜尋您建立的
data_lineage_demo資料集。按一下資料集,然後點選「刪除」。
確認刪除動作。
後續步驟
- 進一步瞭解資料歷程。
- 瞭解如何執行 BigQuery 查詢。
- 瞭解如何使用資料歷程。
- 瞭解 Dataplex Universal Catalog 定價。