開始使用 Spanner Data Boost 和 BigQuery

1. 簡介

在本程式碼研究室中,您將瞭解如何使用 Spanner Data Boost,透過零 ETL 聯合查詢從 BigQuery 查詢 Spanner 資料,且不會影響 Spanner 資料庫。

f1de68f762a86cc7.png

Spanner Data Boost 是全代管的無伺服器服務,可為支援的 Spanner 工作負載提供獨立運算資源。Data Boost 採用無伺服器隨選用量模式,可讓您執行分析查詢和資料匯出作業,對已佈建 Spanner 執行個體的現有工作負載幾乎沒有影響。

搭配 BigQuery 外部連線使用時,Data Boost 可讓您輕鬆將 Spanner 資料查詢至資料分析平台,不必進行複雜的 ETL 資料移轉。

必要條件

  • 對 Google Cloud 控制台有基本瞭解
  • 指令列介面和 Google Shell 的基本技能

課程內容

  • 如何部署 Spanner 執行個體
  • 如何載入資料來建立 Spanner 資料庫
  • 如何從 BigQuery 存取 Spanner 資料 (不使用 Data Boost)
  • 如何使用 Data Boost 從 BigQuery 存取 Spanner 資料

軟硬體需求

  • Google Cloud 帳戶和 Google Cloud 專案
  • 網路瀏覽器,例如 Chrome

2. 設定和需求

自修實驗室環境設定

  1. 登入 Google Cloud 控制台,然後建立新專案或重複使用現有專案。如果沒有 Gmail 或 Google Workspace 帳戶,請先建立帳戶

fbef9caa1602edd0.png

a99b7ace416376c4.png

5e3ff691252acf41.png

  • 專案名稱是這個專案參與者的顯示名稱。這是 Google API 未使用的字元字串。你隨時可以更新。
  • 專案 ID 在所有 Google Cloud 專案中都是不重複的,而且設定後即無法變更。Cloud 控制台會自動產生專屬字串,通常您不需要在意該字串為何。在大多數程式碼研究室中,您需要參照專案 ID (通常標示為 PROJECT_ID)。如果您不喜歡產生的 ID,可以產生另一個隨機 ID。你也可以嘗試使用自己的名稱,看看是否可用。完成這個步驟後就無法變更,且專案期間會維持不變。
  • 請注意,有些 API 會使用第三個值,也就是「專案編號」。如要進一步瞭解這三種值,請參閱說明文件
  1. 接著,您需要在 Cloud 控制台中啟用帳單,才能使用 Cloud 資源/API。完成這個程式碼研究室的費用不高,甚至可能完全免費。如要關閉資源,避免在本教學課程結束後繼續產生費用,請刪除您建立的資源或專案。Google Cloud 新使用者可參加 $300 美元的免費試用計畫。

啟動 Cloud Shell

雖然可以透過筆電遠端操作 Google Cloud,但在本程式碼研究室中,您將使用 Google Cloud Shell,這是可在雲端執行的指令列環境。

Google Cloud 控制台中,點選右上工具列的 Cloud Shell 圖示:

55efc1aaa7a4d3ad.png

佈建並連線至環境的作業需要一些時間才能完成。完成後,您應該會看到如下的內容:

7ffe5cbb04455448.png

這部虛擬機器搭載各種您需要的開發工具,並提供永久的 5GB 主目錄,而且可在 Google Cloud 運作,大幅提升網路效能並強化驗證功能。您可以在瀏覽器中完成本程式碼研究室的所有作業。您不需要安裝任何軟體。

3. 建立 Spanner 執行個體和資料庫

啟用 Spanner API

在 Cloud Shell 中,確認專案 ID 已設定完畢:

gcloud config set project [YOUR-PROJECT-ID]
PROJECT_ID=$(gcloud config get-value project)

將預設區域設為 us-central1。您可以將這個區域變更為 Spanner 區域設定支援的其他區域。

gcloud config set compute/region us-central1

啟用 Spanner API:

gcloud services enable spanner.googleapis.com

建立 Spanner 執行個體

在本步驟中,我們會為程式碼研究室設定 Spanner 執行個體。如要這麼做,請開啟 Cloud Shell 並執行下列指令:

export SPANNER_INSTANCE_ID=codelab-demo
export SPANNER_REGION=regional-us-central1
gcloud spanner instances create $SPANNER_INSTANCE_ID \
--config=$SPANNER_REGION \
--description="Spanner Codelab instance" \
--nodes=1

指令輸出:

$ gcloud spanner instances create $SPANNER_INSTANCE_ID \
--config=$SPANNER_REGION \
--description="Spanner Codelab instance" \
--nodes=1
Creating instance...done.  

建立資料庫

執行個體啟動後,您就可以建立資料庫。Spanner 允許單一執行個體有多個資料庫。

您可以在資料庫中定義結構定義。您也可以控管資料庫的存取權、設定自訂加密、設定最佳化工具,以及設定保留期限。

如要建立資料庫,請再次使用 gcloud 指令列工具:

export SPANNER_DATABASE=codelab-db
gcloud spanner databases create $SPANNER_DATABASE \
 --instance=$SPANNER_INSTANCE_ID

指令輸出:

$ gcloud spanner databases create $SPANNER_DATABASE \
 --instance=$SPANNER_INSTANCE_ID
Creating database...done.

4. 載入資料

如要使用 Data Boost,資料庫中必須有資料。如要這麼做,請建立 Cloud Storage 值區,將 Avro 匯入檔案上傳至該值區,然後啟動 Dataflow 匯入工作,將 Avro 資料載入 Spanner。

啟用 API

如要這麼做,請開啟 Cloud Shell 提示 (如果先前的提示已關閉)。

請務必啟用 Compute、Cloud Storage 和 Dataflow API。

gcloud services enable compute.googleapis.com storage.googleapis.com dataflow.googleapis.com

預期的控制台輸出內容:

$ gcloud services enable compute.googleapis.com storage.googleapis.com dataflow.googleapis.com
Operation "operations/acat.*snip*" finished successfully.

在 Cloud Storage 中暫存匯入檔案

現在,請建立 bucket 來儲存 avro 檔案:

export GCS_BUCKET=spanner-codelab-import_$(date '+%Y-%m-%d_%H_%M_%S')
gcloud storage buckets create gs://$GCS_BUCKET

預期的控制台輸出內容:

$ gcloud storage buckets create gs://$GCS_BUCKET
Creating gs://spanner-codelab-import/...

接著從 GitHub 下載 tar 檔案並解壓縮。

wget https://github.com/dtest/spanner-databoost-tutorial/releases/download/v0.1/spanner-chat-db.tar.gz
tar -xzvf spanner-chat-db.tar.gz 

預期的控制台輸出內容:

$ wget https://github.com/dtest/spanner-databoost-tutorial/releases/download/v0.1/spanner-chat-db.tar.gz
*snip*
*snip*(123 MB/s) - spanner-chat-db.tar.gz' saved [46941709/46941709]
$
$ tar -xzvf spanner-chat-db.tar.gz 
spanner-chat-db/
spanner-chat-db/users.avro-00000-of-00002
spanner-chat-db/user_notifications-manifest.json
spanner-chat-db/interests-manifest.json
spanner-chat-db/users-manifest.json
spanner-chat-db/users.avro-00001-of-00002
spanner-chat-db/topics-manifest.json
spanner-chat-db/topics.avro-00000-of-00002
spanner-chat-db/topics.avro-00001-of-00002
spanner-chat-db/user_interests-manifest.json
spanner-chat-db/spanner-export.json
spanner-chat-db/interests.avro-00000-of-00001
spanner-chat-db/user_notifications.avro-00000-of-00001
spanner-chat-db/user_interests.avro-00000-of-00001

現在,請將檔案上傳至您建立的 bucket。

gcloud storage cp spanner-chat-db gs://$GCS_BUCKET --recursive

預期的控制台輸出內容:

$ gcloud storage cp spanner-chat-db gs://$GCS_BUCKET --recursive
Copying file://spanner-chat-db/users.avro-00000-of-00002 to gs://spanner-codelab-import/spanner-chat-db/users.avro-00000-of-00002
Copying file://spanner-chat-db/user_notifications-manifest.json to gs://spanner-codelab-import/spanner-chat-db/user_notifications-manifest.json
Copying file://spanner-chat-db/interests-manifest.json to gs://spanner-codelab-import/spanner-chat-db/interests-manifest.json           
Copying file://spanner-chat-db/users-manifest.json to gs://spanner-codelab-import/spanner-chat-db/users-manifest.json
Copying file://spanner-chat-db/users.avro-00001-of-00002 to gs://spanner-codelab-import/spanner-chat-db/users.avro-00001-of-00002
Copying file://spanner-chat-db/topics-manifest.json to gs://spanner-codelab-import/spanner-chat-db/topics-manifest.json
Copying file://spanner-chat-db/topics.avro-00000-of-00002 to gs://spanner-codelab-import/spanner-chat-db/topics.avro-00000-of-00002
Copying file://spanner-chat-db/topics.avro-00001-of-00002 to gs://spanner-codelab-import/spanner-chat-db/topics.avro-00001-of-00002
Copying file://spanner-chat-db/user_interests-manifest.json to gs://spanner-codelab-import/spanner-chat-db/user_interests-manifest.json
Copying file://spanner-chat-db/spanner-export.json to gs://spanner-codelab-import/spanner-chat-db/spanner-export.json
Copying file://spanner-chat-db/interests.avro-00000-of-00001 to gs://spanner-codelab-import/spanner-chat-db/interests.avro-00000-of-00001
Copying file://spanner-chat-db/user_notifications.avro-00000-of-00001 to gs://spanner-codelab-import/spanner-chat-db/user_notifications.avro-00000-of-00001
Copying file://spanner-chat-db/user_interests.avro-00000-of-00001 to gs://spanner-codelab-import/spanner-chat-db/user_interests.avro-00000-of-00001
  Completed files 13/13 | 54.6MiB/54.6MiB                                                                                               

Average throughput: 46.4MiB/s

匯入資料

將檔案存放在 Cloud Storage 後,您就可以啟動資料流匯入工作,將資料載入 Spanner。

gcloud dataflow jobs run import_chatdb \
    --gcs-location gs://dataflow-templates-us-central1/latest/GCS_Avro_to_Cloud_Spanner \
    --region us-central1 \
    --staging-location gs://$GCS_BUCKET/tmp \
    --parameters \
instanceId=$SPANNER_INSTANCE_ID,\
databaseId=$SPANNER_DATABASE,\
inputDir=gs://$GCS_BUCKET/spanner-chat-db

預期的控制台輸出內容:

$ gcloud dataflow jobs run import_chatdb \
>     --gcs-location gs://dataflow-templates-us-central1/latest/GCS_Avro_to_Cloud_Spanner \
>     --region us-central1 \
>     --staging-location gs://$GCS_BUCKET/tmp \
>     --parameters \
> instanceId=$SPANNER_INSTANCE_ID,\
> databaseId=$SPANNER_DATABASE,\
> inputDir=gs://$GCS_BUCKET/spanner-chat-db
createTime: '*snip*'
currentStateTime: '*snip*'
id: *snip*
location: us-central1
name: import_chatdb
projectId: *snip*
startTime: '*snip*'
type: JOB_TYPE_BATCH

您可以使用這項指令檢查匯入工作的狀態。

gcloud dataflow jobs list --filter="name=import_chatdb" --region us-central1

預期的控制台輸出內容:

$ gcloud dataflow jobs list --filter="name=import_chatdb"
`--region` not set; getting jobs from all available regions. Some jobs may be missing in the event of an outage. https://cloud.google.com/dataflow/docs/concepts/regional-endpoints
JOB_ID                                   NAME           TYPE   CREATION_TIME        STATE    REGION
*snip*                                  import_chatdb  Batch  2024-04-*snip*  Done  us-central1

驗證 Spanner 中的資料

現在請前往 Spanner Studio,確認資料是否已匯入。首先,展開主題表格即可查看資料欄。

cd1cf38efd2b974a.png

現在,請執行下列查詢,確保資料可用:

SELECT COUNT(*) FROM topics;

預期輸出內容:

89e5d92fbe71c022.png

5. 從 BigQuery 讀取資料

現在您已在 Spanner 中擁有資料,接下來要從 BigQuery 存取資料。為此,您將在 BigQuery 中設定與 Spanner 的外部連線

假設您具備適當權限,請按照下列步驟建立 Spanner 的外部連線。

按一下 BigQuery 控制台頂端的「新增」按鈕,然後選取「連線至外部資料來源」選項。

580a0d237f11a9c5.png

75968de398fabf7e.png

現在可以執行查詢,從 Spanner 讀取資料。在 BigQuery 控制台中執行這項查詢,並務必代入 ${PROJECT_ID} 的值:

SELECT *
FROM (
  SELECT * FROM EXTERNAL_QUERY("projects/${PROJECT_ID}/locations/us-central1/connections/codelab-demo-chat_no-databoost", "SELECT users.userUUID, SHA256(users.email) as hashed_email, COUNT(*) num_topics, m.last_posted from users HASH JOIN (select MAX(t.created) last_posted, t.userUUID FROM topics t GROUP BY 2) m USING (userUUID)HASH JOIN topics USING (userUUID) GROUP BY users.userUUID, users.email, m.last_posted") 
)
ORDER BY num_topics DESC;

輸出內容範例:

e47265487c3e39bd.png

您可以在「Job Information」(工作資訊) 分頁中查看工作相關資訊,例如執行時間和處理的資料量。

9ae40b0aa8c4891.png

接著,您會將 Data Boost 連線新增至 Spanner,並比較結果。

6. 使用 Data Boost 讀取資料

如要使用 Spanner Data Boost,您必須從 BigQuery 建立連往 Spanner 的新外部連線。在 BigQuery 控制台中按一下「新增」,然後再次選取「Connections from external data sources」。

填寫詳細資料,並使用相同的 Spanner 連線 URI。變更「連線 ID」,然後勾選「使用資料加速」方塊。

8e2205255b56a279.png

建立 Data Boost 連線後,您可以執行相同的查詢,但要使用新的連線名稱。同樣地,請在查詢中代入您的 project_id。

SELECT *
FROM (
  SELECT * FROM EXTERNAL_QUERY("projects/${PROJECT_ID}/locations/us-central1/connections/codelab-demo-chat_use-databoost", "SELECT users.userUUID, SHA256(users.email) as hashed_email, COUNT(*) num_topics, m.last_posted from users HASH JOIN (select MAX(t.created) last_posted, t.userUUID FROM topics t GROUP BY 2) m USING (userUUID)HASH JOIN topics USING (userUUID) GROUP BY users.userUUID, users.email, m.last_posted") 
)
ORDER BY num_topics DESC;

您應該會得到與先前相同的結果集。時間是否有所改變?

7. 瞭解 Data Boost

Spanner Data Boost 可讓您使用與 Spanner 執行個體資源無關的資源。這項功能主要可減少分析工作負載對營運工作負載的影響。

如果您在兩到三分鐘內多次執行查詢,且不使用 Data Boost,就會看到這項資訊。請記得替換 ${PROJECT_ID}

SELECT *
FROM (
  SELECT * FROM EXTERNAL_QUERY("projects/${PROJECT_ID}/locations/us-central1/connections/codelab-demo-chat_no-databoost", "SELECT users.userUUID, SHA256(users.email) as hashed_email, COUNT(*) num_topics, m.last_posted from users HASH JOIN (select MAX(t.created) last_posted, t.userUUID FROM topics t GROUP BY 2) m USING (userUUID)HASH JOIN topics USING (userUUID) GROUP BY users.userUUID, users.email, m.last_posted") 
)
ORDER BY num_topics DESC;

接著再稍候幾分鐘,然後執行查詢,再次使用 Data Boost。請記得替換 ${PROJECT_ID}

SELECT *
FROM (
  SELECT * FROM EXTERNAL_QUERY("projects/${PROJECT_ID}/locations/us-central1/connections/codelab-demo-chat_use-databoost", "SELECT users.userUUID, SHA256(users.email) as hashed_email, COUNT(*) num_topics, m.last_posted from users HASH JOIN (select MAX(t.created) last_posted, t.userUUID FROM topics t GROUP BY 2) m USING (userUUID)HASH JOIN topics USING (userUUID) GROUP BY users.userUUID, users.email, m.last_posted") 
)
ORDER BY num_topics DESC;

現在返回 Cloud 控制台的 Spanner Studio,然後前往「系統洞察」

c1dc67fcc7a2a71.png

您可以在這裡查看 CPU 指標。如果查詢未透過 Data Boost 執行,則會使用 CPU 執行「executesql_select_withpartitiontoken」作業。即使查詢相同,執行 Data Boost 時也不會顯示執行個體 CPU 使用率。

a86a7508b6738904.png

在許多情況下,使用 Data Boost 可提升分析查詢的效能。本教學課程中的資料集很小,且沒有其他工作負載會爭用資源。因此,本教學課程不會展示效能提升。

歡迎隨意執行查詢和工作負載,瞭解 Data Boost 的運作方式。完成後,請前往下一節清理環境。

8. 清除環境

如果您是特地為了這個程式碼研究室建立專案,只要刪除專案即可清除資源。如要保留專案並清除個別元件,請按照下列步驟操作。

移除 BigQuery 連線

如要移除這兩個連結,請按一下連結名稱旁的三個點,選取「刪除」,然後按照指示刪除連結。

c9348832bcf202a9.png

刪除 Cloud Storage bucket

gcloud storage rm --recursive gs://$GCS_BUCKET

刪除 Spanner 執行個體

如要清理資源,請前往 Cloud 控制台的 Cloud Spanner 專區,然後刪除我們在程式碼研究室中建立的「codelab-demo」執行個體。

ab7c83ebdab74c04.png

9. 恭喜

恭喜您完成本程式碼研究室。

涵蓋內容

  • 如何部署 Spanner 執行個體
  • 如何使用 Dataflow 將資料載入 Spanner
  • 如何從 BigQuery 存取 Spanner 資料
  • 如何使用 Spanner Data Boost,避免 BigQuery 的分析查詢影響 Spanner 執行個體

10. 問卷調查

輸出內容:

您會如何使用本教學課程?

僅閱讀內容 閱讀內容並完成練習