格式化收到的事件

管道會將匯流排連結至目標目的地,並將事件訊息轉送至該目的地。您可以設定管道,以便預期事件資料採用特定格式,或者在事件傳送至目的地之前,將事件資料從一個支援的格式轉換為另一個格式。舉例來說,您可能需要將事件導向至只接受 Avro 資料的端點。

支援的格式

系統支援下列格式轉換:

  • Avro 轉換為 JSON
  • Avro 轉換為 Protobuf
  • JSON 轉換為 Avro
  • JSON 轉 Protobuf
  • Protobuf 轉換為 Avro
  • 從 Protobuf 轉換為 JSON

注意事項:

  • 轉換事件格式時,只會轉換事件酬載,而非整個事件訊息。

  • 如果管道指定了內送資料格式,則所有事件都必須符合該格式。任何與預期格式不符的事件都會視為持續性錯誤

  • 如果管道指定入站資料格式,就無法設定出站格式。

  • 為特定目的地轉換事件格式之前,系統會先套用任何已設定的資料轉換

  • 除非您指定訊息繫結,否則事件一律會採用 CloudEvents 格式,並使用二進位內容模式的 HTTP 要求傳送。

  • 系統會動態偵測 JSON 結構定義。對於 Protobuf 結構定義,您只能定義一個頂層類型,系統不支援參照其他類型的匯入陳述式。沒有 syntax 識別碼的結構定義預設為 proto2。請注意,有結構定義大小限制

設定管道以格式化事件

您可以在 Google Cloud 控制台或使用 gcloud CLI 中,設定管道以特定格式接收事件資料,或將事件資料從一種格式轉換為另一種格式。

控制台

  1. 在 Google Cloud 控制台中,依序前往「Eventarc」>「管道」頁面。

    前往 Pipelines

  2. 您可以建立管道,如果要更新管道,請按一下管道名稱。

    請注意,更新管道可能需要超過 10 分鐘的時間。

  3. 在「管道詳細資料」頁面中,按一下 「編輯」

  4. 在「Event mediation」窗格中,執行下列操作:

    1. 勾選「套用轉換」核取方塊
    2. 在「Inbound format」清單中,選取適用的格式。

      請注意,如果管道指定了內送資料格式,則所有事件都必須符合該格式。任何與預期格式不符的事件都會視為持續性錯誤

    3. 如果是 Avro 或 Protobuf 格式,則必須指定傳入結構定義。(您可以選擇上傳入站結構定義,而非直接指定)。

    4. CEL 運算式欄位中,使用 CEL 編寫轉換運算式。

    5. 按一下「繼續」

  5. 在「Destination」窗格中執行下列操作:

    1. 如適用,請在「Outbound format」清單中選取格式。

      請注意,如果管道指定入站資料格式,就無法設定出站格式。

    2. 選用步驟:套用訊息繫結。詳情請參閱訊息繫結

  6. 按一下 [儲存]

gcloud

  1. 開啟終端機。

  2. 您可以建立管道,也可以使用 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" },
          ]
        }
        '