將事件發布至 BigQuery 資料表
本快速入門導覽課程說明如何在專案中建立 Eventarc Advanced 匯流排和註冊項目,藉此發布及接收事件訊息。 Google Cloud
匯流排可集中管理系統中的訊息流程,並做為路由器。接收來自訊息來源或提供者發布的事件訊息,並根據註冊條件評估訊息。
註冊項目可用於識別特定匯流排的訂閱項目,並定義訊息的比對條件,以便將訊息轉送至一或多個目的地。
在本快速入門導覽課程中,您將:
建立 BigQuery 資料表。
建立 Eventarc Advanced 匯流排。
建立 Eventarc Advanced 註冊。
將事件訊息發布至匯流排。
查看 BigQuery 資料表中的事件資料。
您可以使用 gcloud CLI 和 bq 指令列工具完成本快速入門導覽課程。
事前準備
貴機構定義的安全性限制,可能會導致您無法完成下列步驟。如需疑難排解資訊,請參閱「在受限的 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.
-
Install the Google Cloud CLI.
-
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
-
如要初始化 gcloud CLI,請執行下列指令:
<x0A> <x0A>gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the BigQuery and Eventarc APIs:
gcloud services enable bigquery.googleapis.com
eventarc.googleapis.com eventarcpublishing.googleapis.com -
Install the Google Cloud CLI.
-
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
-
如要初始化 gcloud CLI,請執行下列指令:
<x0A> <x0A>gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the BigQuery and Eventarc APIs:
gcloud services enable bigquery.googleapis.com
eventarc.googleapis.com eventarcpublishing.googleapis.com - 更新
gcloud
元件:gcloud components update
- 登入帳戶:
gcloud auth login
- 設定本快速入門導覽課程中使用的設定變數:
REGION=REGION
將
REGION
替換為公車的支援位置,例如us-central1
。 -
如果您是專案建立者,系統會授予基本「擁有者」角色 (
roles/owner
)。根據預設,這個身分與存取權管理 (IAM) 角色包含完整存取大多數資源所需的權限,因此您可以略過這個步驟。 Google Cloud如果您不是專案建立者,必須在專案中將必要權限授予適當的主體。舉例來說,主體可以是 Google 帳戶 (適用於使用者),也可以是服務帳戶 (適用於應用程式和運算工作負載)。
所需權限
如要取得完成本快速入門所需的權限,請要求管理員為您授予專案的下列 IAM 角色:
-
BigQuery 資料編輯者 (
roles/bigquery.dataEditor
) -
Eventarc 開發人員 (
roles/eventarc.developer
) -
Eventarc 訊息匯流排管理員 (
roles/eventarc.messageBusAdmin
) -
記錄檢視存取者 (
roles/logging.viewAccessor
) -
專案 IAM 管理員 (
roles/resourcemanager.projectIamAdmin
) -
服務帳戶管理員 (
roles/iam.serviceAccountAdmin
) -
服務帳戶使用者 (
roles/iam.serviceAccountUser
) -
服務使用情形管理員 (
roles/serviceusage.serviceUsageAdmin
)
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。
-
BigQuery 資料編輯者 (
- 如要授予 Eventarc Advanced 更新 BigQuery 資料表屬性所需的權限,請管理員在 Google Cloud 專案中,將 BigQuery 資料編輯者 (
roles/bigquery.dataEditor
) IAM 角色指派給服務帳戶:- 建立服務帳戶。為進行測試,您會將這個服務帳戶附加至 Eventarc Advanced 管道,代表管道的身分。
將gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
SERVICE_ACCOUNT_NAME
替換為服務帳戶的名稱。 - 將
roles/bigquery.dataEditor
身分與存取權管理角色授予服務帳戶:gcloud projects add-iam-policy-binding PROJECT_ID \ --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" \ --role=roles/bigquery.dataEditor
- 建立服務帳戶。為進行測試,您會將這個服務帳戶附加至 Eventarc Advanced 管道,代表管道的身分。
如要建立新的資料集,請使用 bq mk 指令搭配
--dataset
旗標。bq --location=$REGION mk --dataset DATASET_ID
將
DATASET_ID
替換為 BigQuery 資料集的專屬名稱,例如my_dataset
。在終端機中,建立名為
my-schema.json
的新檔案。將下列結構定義複製並貼到新檔案中,然後儲存檔案。
[ { "name": "name", "type": "STRING", "mode": "REQUIRED" }, { "name": "age", "type": "INTEGER", "mode": "NULLABLE" } ]
如要建立資料表,請使用 bq mk 指令搭配
--table
標記。bq mk --table PROJECT_ID:DATASET_ID.TABLE_ID my-schema.json
將
TABLE_ID
換成 BigQuery 資料表的專屬名稱,例如my-table
。使用
gcloud eventarc pipelines create
指令建立管道:gcloud eventarc pipelines create PIPELINE_NAME \ --destinations=http_endpoint_uri='https://bigquery.googleapis.com/bigquery/v2/projects/PROJECT_ID/datasets/DATASET_ID/tables/TABLE_ID/insertAll',http_endpoint_message_binding_template='{"headers": headers.merge({"content-type":"application/json"}), "body": {"rows":[{"json":message.data}]}}',oauth_token_authentication_service_account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com \ --input-payload-format-json= \ --location=$REGION
將
PIPELINE_NAME
替換為管道的 ID 或完整名稱,例如my-pipeline
。注意事項:
使用
gcloud eventarc enrollments create
指令建立註冊:gcloud eventarc enrollments create ENROLLMENT_NAME \ --cel-match=MATCH_EXPRESSION \ --destination-pipeline=PIPELINE_NAME \ --message-bus=BUS_NAME \ --message-bus-project=PROJECT_ID \ --location=$REGION
更改下列內容:
刪除 Eventarc Advanced 資源:
建立 BigQuery 資料表
建立 BigQuery 資料表做為事件目的地。系統也支援其他事件目的地,例如 Pub/Sub 主題、工作流程或其他 HTTP 端點。詳情請參閱「事件供應商和目的地」。
建立 BigQuery 資料表前,請先建立「資料集」(做為資料表的頂層容器) 和資料表「結構定義」。
建立 Eventarc Advanced 匯流排
匯流排會接收來自訊息來源或提供者發布的事件訊息,並做為訊息路由器。
詳情請參閱建立用於傳送訊息的匯流排。
使用 gcloud eventarc message-buses create
指令,在專案中建立 Eventarc Advanced 匯流排:
gcloud eventarc message-buses create BUS_NAME \ --location=$REGION
將 BUS_NAME
替換為匯流排的 ID 或完整名稱,例如 my-bus
。
建立 Eventarc Advanced 註冊
註冊項目會決定要將哪些訊息轉送至目的地,並指定用於為事件訊息設定目的地的管道。在本例中,目標目的地是 BigQuery API 端點。
詳情請參閱「建立註冊項目以接收事件」。
使用 gcloud CLI 時,您會先建立管道,然後建立註冊:
將事件訊息發布至匯流排
如要直接將訊息發布至匯流排,可以使用 gcloud eventarc message-buses publish
指令,或向 Eventarc Publishing REST API 傳送要求。詳情請參閱「直接發布事件」。
訊息必須採用 CloudEvents 格式,這是以通用方式描述事件資料的規格。data
元素是事件的酬載,最終必須與 BigQuery 資料表的結構定義相符。這個欄位可填入任何格式正確的 JSON。如要進一步瞭解 CloudEvents 內容屬性,請參閱「事件格式」。
以下範例說明如何直接將事件發布至 Eventarc Advanced 匯流排:
範例 1
您可以使用 gcloud CLI、--event-data
和其他事件屬性旗標,將事件發布至匯流排:
gcloud eventarc message-buses publish BUS_NAME \
--event-data='{"name": "my-name", "age": "20"}' \
--event-id=hello-world-id-1234 \
--event-source=hello-world-source \
--event-type=hello-world-type \
--event-attributes="datacontenttype=application/json" \
--location=$REGION
範例 2
您可以使用 gcloud CLI 和 --json-message
旗標,以 JSON 訊息的形式將事件發布至匯流排:
gcloud eventarc message-buses publish BUS_NAME \
--location=$REGION \
--json-message='{"id": "hello-world-id-1234", "type":
"hello-world-type", "source":
"hello-world-source", "specversion": "1.0", "data":
{"name": "my-name", "age": "20"}}'
發布活動後,您應該會收到「活動發布成功」訊息。
查看 BigQuery 資料表中的事件資料
將事件發布至 Eventarc Advanced 匯流排後,您可以使用 bq query 指令,確認 BigQuery 資料表是否新增了資料列。
bq query \ --use_legacy_sql=false \ 'SELECT * FROM `PROJECT_ID.DATASET_ID.TABLE_ID` LIMIT 10;'
您已成功建立 Eventarc Advanced 匯流排和註冊項目、將事件訊息發布至匯流排,並透過查詢 BigQuery 資料表驗證預期結果。
清除所用資源
完成本快速入門導覽課程所述工作後,您可以刪除已建立的資源,避免系統繼續計費:
或者,您也可以刪除 Google Cloud 專案,以免產生費用。 刪除 Google Cloud 專案後,系統就會停止對專案使用的所有資源收取費用。
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID