Compartilhar via

将自定义字段映射到事件网格架构

如果事件数据与预期的 事件网格架构不匹配,仍可使用事件网格将事件路由到订阅者。 本文介绍如何将架构映射到事件网格架构。

原始事件架构

假设你有一个以以下格式发送事件的应用程序:

[
  {
    "myEventTypeField":"Created",
    "resource":"Users/example/Messages/1000",
    "resourceData":{"someDataField1":"SomeDataFieldValue"}
  }
]

尽管该格式与所需的架构不匹配,但事件网格允许将字段映射到架构。 或者,您可以接收原始架构中的值。

使用映射字段创建自定义主题

创建自定义主题时,指定如何将字段从原始事件映射到事件网格架构。 可以使用三个值来自定义映射:

  • 输入架构值指定架构的类型。 可用选项包括 CloudEvents 架构、自定义事件架构或事件网格架构。 默认值为事件网格架构。 在架构与事件网格架构之间创建自定义映射时,请使用自定义事件架构。 当事件采用 CloudEvents 格式时,请使用 CloudEvents 架构。

  • 映射默认值属性指定事件网格架构中字段的默认值。 可以设置 subject默认值, eventtype以及 dataversion。 通常,自定义架构不包含与这三个字段之一对应的字段时,请使用此参数。 例如,可以指定数据版本始终设置为 1.0

  • 映射字段值将架构中的字段映射到事件网格架构。 在空格分隔的键/值对中指定值。 对于键名称,请使用事件网格字段的名称。 对于值,请使用字段名称。 可以对idtopiceventtimesubjecteventtypedataversion使用键名。

若要使用 Azure CLI 创建自定义主题,请使用:

az eventgrid topic create \
  -n demotopic \
  -l chinaeast2 \
  -g myResourceGroup \
  --input-schema customeventschema \
  --input-mapping-fields eventType=myEventTypeField \
  --input-mapping-default-values subject=DefaultSubject dataVersion=1.0

对于 PowerShell,请使用:

New-AzEventGridTopic `
  -ResourceGroupName myResourceGroup `
  -Name demotopic `
  -Location chinaeast2 `
  -InputSchema CustomEventSchema `
  -InputMappingField @{eventType="myEventTypeField"} `
  -InputMappingDefaultValue @{subject="DefaultSubject"; dataVersion="1.0" }

订阅事件网格主题

订阅自定义主题时,可以指定要用于接收事件的架构。 指定 CloudEvents 架构、自定义事件架构或事件网格架构。 默认值为事件网格架构。

以下示例订阅事件网格主题并使用事件网格架构。 对于Azure CLI,请使用:

topicid=$(az eventgrid topic show --name demoTopic -g myResourceGroup --query id --output tsv)

az eventgrid event-subscription create \
  --source-resource-id $topicid \
  --name eventsub1 \
  --event-delivery-schema eventgridschema \
  --endpoint <endpoint_URL>

下一个示例使用事件的输入架构:

az eventgrid event-subscription create \
  --source-resource-id $topicid \
  --name eventsub2 \
  --event-delivery-schema custominputschema \
  --endpoint <endpoint_URL>

以下示例订阅事件网格主题并使用事件网格架构。 对于 PowerShell,请使用:

$topicid = (Get-AzEventGridTopic -ResourceGroupName myResourceGroup -Name demoTopic).Id

New-AzEventGridSubscription `
  -ResourceId $topicid `
  -EventSubscriptionName eventsub1 `
  -EndpointType webhook `
  -Endpoint <endpoint-url> `
  -DeliverySchema EventGridSchema

下一个示例使用事件的输入架构:

New-AzEventGridSubscription `
  -ResourceId $topicid `
  -EventSubscriptionName eventsub2 `
  -EndpointType webhook `
  -Endpoint <endpoint-url> `
  -DeliverySchema CustomInputSchema

将事件发布到主题

现在,你已准备好将事件发送到自定义主题,并查看映射的结果。 以下脚本用于在 示例架构中发布事件:

对于Azure CLI,请使用:

endpoint=$(az eventgrid topic show --name demotopic -g myResourceGroup --query "endpoint" --output tsv)
key=$(az eventgrid topic key list --name demotopic -g myResourceGroup --query "key1" --output tsv)

event='[ { "myEventTypeField":"Created", "resource":"Users/example/Messages/1000", "resourceData":{"someDataField1":"SomeDataFieldValue"} } ]'

curl -X POST -H "aeg-sas-key: $key" -d "$event" $endpoint

对于 PowerShell,请使用:

$endpoint = (Get-AzEventGridTopic -ResourceGroupName myResourceGroup -Name demotopic).Endpoint
$keys = Get-AzEventGridTopicKey -ResourceGroupName myResourceGroup -Name demotopic

$htbody = @{
    myEventTypeField="Created"
    resource="Users/example/Messages/1000"
    resourceData= @{
        someDataField1="SomeDataFieldValue"
    }
}

$body = "["+(ConvertTo-Json $htbody)+"]"
Invoke-WebRequest -Uri $endpoint -Method POST -Body $body -Headers @{"aeg-sas-key" = $keys.Key1}

现在,查看 WebHook 终结点。 这两个订阅以不同的模式传递了事件。

第一个订阅使用了事件网格架构。 传递事件的格式为:

{
  "id": "aa5b8e2a-1235-4032-be8f-5223395b9eae",
  "eventTime": "2018-11-07T23:59:14.7997564Z",
  "eventType": "Created",
  "dataVersion": "1.0",
  "metadataVersion": "1",
  "topic": "/subscriptions/<subscription-id>/resourceGroups/myResourceGroup/providers/Microsoft.EventGrid/topics/demotopic",
  "subject": "DefaultSubject",
  "data": {
    "myEventTypeField": "Created",
    "resource": "Users/example/Messages/1000",
    "resourceData": {
      "someDataField1": "SomeDataFieldValue"
    }
  }
}

这些字段包含自定义主题中的映射。 myEventTypeField 映射到 EventType。 使用 DataVersionSubject 的默认值。 Data 对象包含原始事件架构字段。

第二个订阅使用了输入事件架构。 传递事件的格式为:

{
  "myEventTypeField": "Created",
  "resource": "Users/example/Messages/1000",
  "resourceData": {
    "someDataField1": "SomeDataFieldValue"
  }
}

请注意,原始字段已交付。

后续步骤