使用選用的 Cloud Run 函式觸發程序,將變更串流傳入 Pub/Sub

本教學課程說明如何使用「Bigtable 變更串流至 Pub/Sub」範本,包括如何設定主題和範本。您可以選擇以自選的程式設計語言建立 Cloud Run 函式,並透過事件串流觸發該函式。

本教學課程適用於熟悉 Bigtable、程式碼編寫和事件串流服務的技術人員。

目標

本教學課程說明如何執行下列操作:

  • 建立已啟用變更串流的 Bigtable 資料表。
  • 使用 Bigtable 變更串流結構定義建立 Pub/Sub 主題。
  • 使用範本在 Dataflow 上,將 Bigtable 變更串流部署至 Pub/Sub 管道。
  • 直接在 Pub/Sub 中或 Cloud Run 函式的記錄中查看事件串流。

費用

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

您可以使用 Pricing Calculator,根據預測用量估算費用。

初次使用 Google Cloud 的使用者可能符合免費試用期資格。

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

事前準備

    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.

    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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

    Verify that billing is enabled for your Google Cloud project.

    Enable the Dataflow, Cloud Bigtable API, Cloud Bigtable Admin API, Pub/Sub, Cloud Run functions, and Cloud Storage APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

    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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

    Verify that billing is enabled for your Google Cloud project.

    Enable the Dataflow, Cloud Bigtable API, Cloud Bigtable Admin API, Pub/Sub, Cloud Run functions, and Cloud Storage APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

    In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  1. 更新並安裝 cbt CLI 。
    gcloud components update
    gcloud components install cbt
  2. 建立 Pub/Sub 主題

    1. 前往 Google Cloud 控制台的 Pub/Sub「主題」頁面。

      前往「主題」頁面

    2. 按一下「建立主題」

    3. 將 ID 設定為 bigtable-change-stream-topic

    4. 選取「使用結構定義」

    5. 在「選取 Pub/Sub 結構定義」下拉式選單中,按一下「建立新結構定義」。系統會開啟新分頁,供您定義結構定義。

      1. 將結構定義 ID 設為 bigtable-change-stream-schema
      2. 將結構定義類型設為「Avro」Avro
      3. 貼上下列結構定義。如要進一步瞭解架構,請參閱範本說明文件頁面
        {
            "name" : "ChangelogEntryMessage",
            "type" : "record",
            "namespace" : "com.google.cloud.teleport.bigtable",
            "fields" : [
              { "name" : "rowKey", "type" : "bytes"},
              {
                "name" : "modType",
                "type" : {
                  "name": "ModType",
                  "type": "enum",
                  "symbols": ["SET_CELL", "DELETE_FAMILY", "DELETE_CELLS", "UNKNOWN"]}
              },
              { "name": "isGC", "type": "boolean" },
              { "name": "tieBreaker", "type": "int"},
              { "name": "columnFamily", "type": "string"},
              { "name": "commitTimestamp", "type" : "long"},
              { "name" : "sourceInstance", "type" : "string"},
              { "name" : "sourceCluster", "type" : "string"},
              { "name" : "sourceTable", "type" : "string"},
              { "name": "column", "type" : ["null", "bytes"]},
              { "name": "timestamp", "type" : ["null", "long"]},
              { "name": "timestampFrom", "type" : ["null", "long"]},
              { "name": "timestampTo", "type" : ["null", "long"]},
              { "name" : "value", "type" : ["null", "bytes"]}
          ]
        }
      
      1. 按一下「建立」,建立結構定義。
    6. 關閉「建立結構定義」分頁,重新整理結構定義清單,然後選取新定義的結構定義。

    7. 按一下「建立」即可建立主題。

    選用:建立 Cloud Run 函式

    您可能想使用 Cloud Run 函式處理 Pub/Sub 串流。

    1. bigtable-change-stream-topic 主題的「詳細資料」頁面中,按一下「觸發 Cloud Function」
    2. 在「Function name」(函式名稱) 欄位中,輸入名稱 bt-ps-tutorial-function
    3. 在「原始碼」部分,按一下「執行階段」下拉式選單,然後選取您要的執行階段和程式設計語言。系統會產生 hello world,在變更串流傳入時列印出來。詳情請參閱說明文件,進一步瞭解如何編寫 Cloud Run 函式
    4. 其他欄位皆使用預設值。
    5. 按一下「Deploy function」(部署函式)

    建立啟用變更串流的資料表

    1. 前往 Google Cloud 控制台的 Bigtable「Instances」(執行個體) 頁面。

      前往「Instances」(執行個體)

    2. 按一下您在本教學課程中使用的執行個體 ID。

      如果沒有可用的執行個體,請在您附近的區域建立執行個體,並使用預設設定。

    3. 按一下左側導覽窗格中的「表格」

    4. 按一下「建立資料表」

    5. 將資料表命名為 change-streams-pubsub-tutorial

    6. 新增名為 cf 的資料欄系列。

    7. 選取「啟用變更串流」

    8. 點選「建立」

    初始化資料管道,擷取變更串流

    1. 在 Bigtable「Tables」(資料表) 頁面中,找到您的資料表 change-streams-pubsub-tutorial
    2. 在「變更串流」欄中,按一下「連線」
    3. 在對話方塊中選取「Pub/Sub」
    4. 按一下「建立 Dataflow 工作」
    5. 在 Dataflow 的「Create job」(建立工作) 頁面中,將輸出 Pub/Sub 主題名稱設為:bigtable-change-stream-topic
    6. 將 Bigtable 應用程式設定檔 ID 設為 default
    7. 按一下「Run Job」(執行工作)
    8. 請先等待工作狀態為「啟動中」或「執行中」,再繼續操作。 工作排入佇列後,大約需要 5 分鐘。

    將一些資料寫入 Bigtable

    1. 在 Cloud Shell 中,將幾列資料寫入 Bigtable,以便變更記錄將一些資料寫入 Pub/Sub 串流。只要在建立工作後寫入資料,就會顯示變更。您不必等待工作狀態變成 running

      cbt -instance=BIGTABLE_INSTANCE_ID -project=YOUR_PROJECT_ID \
          set change-streams-pubsub-tutorial user123 cf:col1=abc
      cbt -instance=BIGTABLE_INSTANCE_ID -project=YOUR_PROJECT_ID \
          set change-streams-pubsub-tutorial user546 cf:col1=def
      cbt -instance=BIGTABLE_INSTANCE_ID -project=YOUR_PROJECT_ID \
          set change-streams-pubsub-tutorial user789 cf:col1=ghi
      

    在 Pub/Sub 中查看變更記錄

    1. 前往 Google Cloud 控制台的 Pub/Sub「Subscriptions」(訂閱項目) 頁面。

      前往「訂閱項目」頁面

    2. 按一下系統為主題自動建立的訂閱項目 bigtable-change-stream-topic。應命名為 bigtable-change-stream-topic-sub

    3. 前往「訊息」分頁。

    4. 按一下「Pull」(提取)

    5. 瀏覽訊息清單,並查看您寫入的資料。

      變更 Pub/Sub 中的記錄檔訊息

    選用:查看 Cloud Run functions 記錄中的變更

    如果您建立的是 Cloud Run functions 函式,可以在記錄檔中查看變更。

    1. 前往 Google Cloud 控制台的「Cloud Run functions」

      前往「Cloud Run functions」(Cloud Run 函式)

    2. 按一下函式 bt-ps-tutorial-function

    3. 前往「記錄」分頁。

    4. 請確認「嚴重程度」至少設為「資訊」,這樣您才能查看記錄。

    5. 探索記錄檔,並查看您寫入的資料。

    輸出看起來類似以下內容:

    Pub/Sub message: {"rowKey":"user789","modType":"SET_CELL","isGC":false,"tieBreaker":0,"columnFamily":"cf","commitTimestamp":1695653833064548,"sourceInstance":"YOUR-INSTANCE","sourceCluster":"YOUR-INSTANCE-c1","sourceTable":"change-streams-pubsub-tutorial","column":{"bytes":"col1"},"timestamp":{"long":1695653832278000},"timestampFrom":null,"timestampTo":null,"value":{"bytes":"ghi"}}
    

    清除所用資源

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

    刪除 Bigtable 資料表

    1. 前往 Google Cloud 控制台的 Bigtable「Instances」(執行個體) 頁面。

      前往「Instances」(執行個體)

    2. 按一下您在本教學課程中使用的執行個體 ID。

    3. 按一下左側導覽窗格中的「表格」

    4. 找到 change-streams-pubsub-tutorial 資料表。

    5. 按一下 [編輯]

    6. 清除「啟用變更串流」

    7. 按一下 [儲存]

    8. 開啟表格的溢位選單。

    9. 按一下「刪除」,然後輸入資料表名稱來確認。

    停止變更串流管道

    1. 前往 Google Cloud 控制台的 Dataflow「Jobs」(工作) 頁面。

      前往「Jobs」(工作) 頁面

    2. 從工作清單中選取串流工作。

    3. 按一下導覽區中的「停止」

    4. 在「停止工作」對話方塊中取消管道,然後按一下「停止工作」

    刪除 Pub/Sub 主題和訂閱項目

    1. 前往 Google Cloud 控制台的 Pub/Sub「主題」頁面。

      前往「主題」頁面

    2. 選取 bigtable-change-stream-topic 主題。

    3. 按一下「Delete」(刪除) 加以確認。

    4. 按一下側欄中的「訂閱項目」

    5. 選取 bigtable-change-stream-topic-sub 訂閱方案。

    6. 按一下「Delete」(刪除) 加以確認。

    刪除 Cloud Run 函式

    1. 前往 Google Cloud 控制台的「Cloud Run functions」

      前往「Cloud Run functions」(Cloud Run 函式)

    2. 選取 bt-ps-tutorial-function 函式。

    3. 按一下「Delete」(刪除) 加以確認。

    後續步驟