管道會將匯流排連結至目標目的地,並將事件訊息轉送至該目的地。您可以設定管道,以便預期事件資料採用特定格式,或者在事件傳送至目的地之前,將事件資料從一個支援的格式轉換為另一個格式。舉例來說,您可能需要將事件導向至只接受 Avro 資料的端點。
支援的格式
系統支援下列格式轉換:
- Avro 轉換為 JSON
- Avro 轉換為 Protobuf
- JSON 轉換為 Avro
- JSON 轉 Protobuf
- Protobuf 轉換為 Avro
- 從 Protobuf 轉換為 JSON
注意事項:
轉換事件格式時,只會轉換事件酬載,而非整個事件訊息。
如果管道指定了內送資料格式,則所有事件都必須符合該格式。任何與預期格式不符的事件都會視為持續性錯誤。
如果管道未指定入站資料格式,就無法設定出站格式。
為特定目的地轉換事件格式之前,系統會先套用任何已設定的資料轉換。
除非您指定訊息繫結,否則事件一律會採用 CloudEvents 格式,並使用二進位內容模式的 HTTP 要求傳送。
系統會動態偵測 JSON 結構定義。對於 Protobuf 結構定義,您只能定義一個頂層類型,系統不支援參照其他類型的匯入陳述式。沒有
syntax
識別碼的結構定義預設為proto2
。請注意,有結構定義大小限制。
設定管道以格式化事件
您可以在 Google Cloud 控制台或使用 gcloud CLI 中,設定管道以特定格式接收事件資料,或將事件資料從一種格式轉換為另一種格式。
控制台
gcloud
開啟終端機。
您可以建立管道,也可以使用
gcloud beta eventarc pipelines update
指令更新管道:請注意,更新管道可能需要超過 10 分鐘的時間。
gcloud beta eventarc pipelines update PIPELINE_NAME \ --location=REGION \ --INPUT_PAYLOAD_FLAG \ --destinations=OUTPUT_PAYLOAD_KEY
更改下列內容:
PIPELINE_NAME
:管道 ID 或完全修飾名稱REGION
:支援的 Eventarc Advanced 位置或者,您也可以設定 gcloud CLI 位置屬性:
gcloud config set eventarc/location REGION
INPUT_PAYLOAD_FLAG
:輸入資料格式標記,可為下列其中一個:--input-payload-format-avro-schema-definition
--input-payload-format-json
--input-payload-format-protobuf-schema-definition
請注意,如果為管道指定輸入資料格式,則所有事件都必須符合該格式。任何與預期格式不符的事件都會視為持續性錯誤。
OUTPUT_PAYLOAD_KEY
:輸出資料格式鍵,可為下列其中一個值:output_payload_format_avro_schema_definition
output_payload_format_json
output_payload_format_protobuf_schema_definition
請注意,如果您設定輸出資料格式鍵,也必須指定輸入資料格式標記。
範例:
以下範例使用
--input-payload-format-protobuf-schema-definition
標記,指定管道應預期事件採用 Protobuf 資料格式,並使用特定結構定義:gcloud beta eventarc pipelines update my-pipeline \ --input-payload-format-protobuf-schema-definition \ ' syntax = "proto3"; message schema { string name = 1; string severity = 2; } '
以下範例使用
output_payload_format_avro_schema_definition
鍵和--input-payload-format-avro-schema-definition
旗標,建立預期事件以 Avro 格式輸出的管道,並以相同格式輸出事件:gcloud beta eventarc pipelines create my-pipeline \ --location=us-central1 \ --destinations=http_endpoint_uri='https://example-endpoint.com',output_payload_format_avro_schema_definition='{"type": "record", "name": "my_record", "fields": [{"name": "my_field", "type": "string"}]}' \ --input-payload-format-avro-schema-definition='{"type": "record", "name": "my_record", "fields": [{"name": "my_field", "type": "string"}]}'
以下範例使用
output_payload_format_protobuf_schema_definition
鍵和--input-payload-format-avro-schema-definition
標記,更新管道並使用結構定義將事件資料從 Avro 轉換為 Protobuf:gcloud beta eventarc pipelines update my-pipeline \ --location=us-central1 \ --destinations=output_payload_format_protobuf_schema_definition='message MessageProto {string prop1 = 1; string prop2 = 2;}' \ --input-payload-format-avro-schema-definition= \ ' { "type": "record", "name": "MessageProto", "fields": [ { "name" : "prop1", "type": "string" }, { "name" : "prop2", "type": "string" }, ] } '