了解事件网格订阅的事件筛选Understand event filtering for Event Grid subscriptions

本本介绍了筛选发送到终结点的事件的不同方法。This article describes the different ways to filter which events are sent to your endpoint. 创建事件订阅时,有三个筛选选项:When creating an event subscription, you have three options for filtering:

  • 事件类型Event types
  • 主题开头内容或结尾内容Subject begins with or ends with
  • 高级字段和运算符Advanced fields and operators

事件类型筛选Event type filtering

默认情况下,事件源的所有事件类型都将发送到终结点。By default, all event types for the event source are sent to the endpoint. 可以决定仅将某些事件类型发送到终结点。You can decide to send only certain event types to your endpoint. 例如,可以收到有关资源更新的通知,但不会收到删除等其他操作的通知。For example, you can get notified of updates to your resources, but not notified for other operations like deletions. 在这种情况下,按 Microsoft.Resources.ResourceWriteSuccess 事件类型进行筛选。In that case, filter by the Microsoft.Resources.ResourceWriteSuccess event type. 提供包含事件类型的数组或指定 All 以获取事件源的所有事件类型。Provide an array with the event types, or specify All to get all event types for the event source.

按事件类型筛选的 JSON 语法是:The JSON syntax for filtering by event type is:

"filter": {
  "includedEventTypes": [
    "Microsoft.Resources.ResourceWriteFailure",
    "Microsoft.Resources.ResourceWriteSuccess"
  ]
}

主题筛选Subject filtering

对于按主题的简单筛选,请指定主题的开头值或结尾值。For simple filtering by subject, specify a starting or ending value for the subject. 例如,可以使用 .txt 指定主题的结尾,以仅获取与将文本文件上传到存储帐户相关的事件。For example, you can specify the subject ends with .txt to only get events related to uploading a text file to storage account. 或者,可以筛选以 /blobServices/default/containers/testcontainer 开头的主题以获取该容器的所有事件,但不获取存储帐户中的其他容器。Or, you can filter the subject begins with /blobServices/default/containers/testcontainer to get all events for that container but not other containers in the storage account.

将事件发布到自定义主题时,可为事件创建主题,便于订阅者们了解他们是否对该事件感兴趣。When publishing events to custom topics, create subjects for your events that make it easy for subscribers to know whether they're interested in the event. 订阅者使用主题属性来筛选和路由事件。Subscribers use the subject property to filter and route events. 考虑添加事件发生位置的路径,以便订阅者可以按该路径的段进行筛选。Consider adding the path for where the event happened, so subscribers can filter by segments of that path. 通过路径,订阅者可精确或宽泛地筛选事件。The path enables subscribers to narrowly or broadly filter events. 如果在主题中提供一个由三个段构成的路径(如 /A/B/C),订阅者可根据第一个段 /A 进行筛选,获取范围较宽泛的一组事件。If you provide a three segment path like /A/B/C in the subject, subscribers can filter by the first segment /A to get a broad set of events. 这些订阅者会获取主题为 /A/B/C/A/D/E 这样的事件。Those subscribers get events with subjects like /A/B/C or /A/D/E. 其他订阅者可通过 /A/B 进行筛选,这样可以获取范围更精确的一组事件。Other subscribers can filter by /A/B to get a narrower set of events.

按主题筛选的 JSON 语法是:The JSON syntax for filtering by subject is:

"filter": {
  "subjectBeginsWith": "/blobServices/default/containers/mycontainer/log",
  "subjectEndsWith": ".jpg"
}

高级筛选Advanced filtering

要按数据字段中的值进行筛选并指定比较运算符,请使用高级筛选选项。To filter by values in the data fields and specify the comparison operator, use the advanced filtering option. 在高级筛选中,指定:In advanced filtering, you specify the:

  • 运算符类型 - 比较的类型。operator type - The type of comparison.
  • 键 - 用于筛选的事件数据中的字段。key - The field in the event data that you're using for filtering. 它可以是数字、布尔值、字符串或数组。It can be a number, boolean, string, or an array.
  • 值 - 要与键进行比较的值。values - The value or values to compare to the key.

密钥Key

键是用于筛选的事件数据中的字段。Key is the field in the event data that you're using for filtering. 可以是下列类型之一:It can be one of the following types:

  • NumberNumber

  • 布尔Boolean

  • 字符串String

  • 数组。Array. 需要将 enableAdvancedFilteringOnArrays 属性设置为 true 才能使用此功能。You need to set the enableAdvancedFilteringOnArrays property to true to use this feature. 目前,Azure 门户不支持启用此功能。Currently, the Azure portal doesn't support enabling this feature.

    "filter":
    {
        "subjectBeginsWith": "/blobServices/default/containers/mycontainer/log",
        "subjectEndsWith": ".jpg",
        "enableAdvancedFilteringOnArrays": true
    }
    

对于 事件网格架构 中的事件,请使用以下键值:IDTopicSubjectEventTypeDataVersion 或事件数据(如 data.key1)。For events in the Event Grid schema, use the following values for the key: ID, Topic, Subject, EventType, DataVersion, or event data (like data.key1).

对于 云事件架构 中的事件,请使用以下键值:eventidsourceeventtypeeventtypeversion 或事件数据(如 data.key1)。For events in Cloud Events schema, use the following values for the key: eventid, source, eventtype, eventtypeversion, or event data (like data.key1).

对于 自定义输入架构,请使用事件数据字段(如 data.key1)。For custom input schema, use the event data fields (like data.key1). 若要访问数据节中的字段,请使用 .(点)表示法。To access fields in the data section, use the . (dot) notation. 例如,使用 data.sitenamedata.appEventTypeDetail.action 访问以下示例事件的 sitenameactionFor example, data.sitename, data.appEventTypeDetail.action to access sitename or action for the following sample event.

    "data": {
        "appEventTypeDetail": {
            "action": "Started"
        },
        "siteName": "<site-name>",
        "clientRequestId": "None",
        "correlationRequestId": "None",
        "requestId": "292f499d-04ee-4066-994d-c2df57b99198",
        "address": "None",
        "verb": "None"
    },

Values

值可以是:数字、字符串、布尔值或数组The values can be: number, string, boolean, or array

运算符Operators

可用的数字运算符为:The available operators for numbers are:

NumberInNumberIn

如果 值为指定的 筛选器 值之一,则 NumberIn 运算符的计算结果为 true。The NumberIn operator evaluates to true if the key value is one of the specified filter values. 在以下示例中,它将检查 data 节中 counter 特性的值是 5 还是 1。In the following example, it checks whether the value of the counter attribute in the data section is 5 or 1.

"advancedFilters": [{
    "operatorType": "NumberIn",
    "key": "data.counter",
    "values": [
        5,
        1
    ]
}]

如果键为数组,则会对照筛选器值的数组检查数组中的所有值。If the key is an array, all the values in the array are checked against the array of filter values. 下面是键为 [v1, v2, v3]、筛选器为 [a, b, c] 的伪代码。Here's the pseudo code with the key: [v1, v2, v3] and the filter: [a, b, c]. 其数据类型与筛选器数据类型不匹配的任何键值都会被忽略。Any key values with data types that don�t match the filter�s data type are ignored.

FOR_EACH filter IN (a, b, c)
    FOR_EACH key IN (v1, v2, v3)
        IF filter == key
            MATCH

NumberNotInNumberNotIn

如果 不是 任何指定的 筛选器 值,则 NumberNotIn 的计算结果为 true。The NumberNotIn evaluates to true if the key value is not any of the specified filter values. 在以下示例中,它将检查 data 节中 counter 特性的值是否不是 41 和 0。In the following example, it checks whether the value of the counter attribute in the data section isn't 41 and 0.

"advancedFilters": [{
    "operatorType": "NumberNotIn",
    "key": "data.counter",
    "values": [
        41,
        0
    ]
}]

如果键为数组,则会对照筛选器值的数组检查数组中的所有值。If the key is an array, all the values in the array are checked against the array of filter values. 下面是键为 [v1, v2, v3]、筛选器为 [a, b, c] 的伪代码。Here's the pseudo code with the key: [v1, v2, v3] and the filter: [a, b, c]. 其数据类型与筛选器数据类型不匹配的任何键值都会被忽略。Any key values with data types that don't match the filter's data type are ignored.

FOR_EACH filter IN (a, b, c)
    FOR_EACH key IN (v1, v2, v3)
        IF filter == key
            FAIL_MATCH

NumberLessThanNumberLessThan

如果 小于 指定的 筛选器 值,则 NumberLessThan 运算符的计算结果为 true。The NumberLessThan operator evaluates to true if the key value is less than the specified filter value. 在以下示例中,它将检查 data 节中 counter 特性的值是否小于 100。In the following example, it checks whether the value of the counter attribute in the data section is less than 100.

"advancedFilters": [{
    "operatorType": "NumberLessThan",
    "key": "data.counter",
    "value": 100
}]

如果键为数组,则会对照筛选器值检查数组中的所有值。If the key is an array, all the values in the array are checked against the filter value. 下面是键为 [v1, v2, v3] 的伪代码。Here's the pseudo code with the key: [v1, v2, v3]. 其数据类型与筛选器数据类型不匹配的任何键值都会被忽略。Any key values with data types that don't match the filter's data type are ignored.

FOR_EACH key IN (v1, v2, v3)
    IF key < filter
        MATCH

NumberGreaterThanNumberGreaterThan

如果 大于 指定的 筛选器 值,则 NumberGreaterThan 运算符的计算结果为 true。The NumberGreaterThan operator evaluates to true if the key value is greater than the specified filter value. 在以下示例中,它将检查 data 节中 counter 特性的值是否大于 20。In the following example, it checks whether the value of the counter attribute in the data section is greater than 20.

"advancedFilters": [{
    "operatorType": "NumberGreaterThan",
    "key": "data.counter",
    "value": 20
}]

如果键为数组,则会对照筛选器值检查数组中的所有值。If the key is an array, all the values in the array are checked against the filter value. 下面是键为 [v1, v2, v3] 的伪代码。Here's the pseudo code with the key: [v1, v2, v3]. 其数据类型与筛选器数据类型不匹配的任何键值都会被忽略。Any key values with data types that don't match the filter's data type are ignored.

FOR_EACH key IN (v1, v2, v3)
    IF key > filter
        MATCH

NumberLessThanOrEqualsNumberLessThanOrEquals

如果 小于或等于 指定的 筛选器 值,则 NumberLessThanOrEquals 运算符的计算结果为 true。The NumberLessThanOrEquals operator evaluates to true if the key value is less than or equal to the specified filter value. 在以下示例中,它将检查 data 节中 counter 特性的值是否小于或等于 100。In the following example, it checks whether the value of the counter attribute in the data section is less than or equal to 100.

"advancedFilters": [{
    "operatorType": "NumberLessThanOrEquals",
    "key": "data.counter",
    "value": 100
}]

如果键为数组,则会对照筛选器值检查数组中的所有值。If the key is an array, all the values in the array are checked against the filter value. 下面是键为 [v1, v2, v3] 的伪代码。Here's the pseudo code with the key: [v1, v2, v3]. 其数据类型与筛选器数据类型不匹配的任何键值都会被忽略。Any key values with data types that don't match the filter's data type are ignored.

FOR_EACH key IN (v1, v2, v3)
    IF key <= filter
        MATCH

NumberGreaterThanOrEqualsNumberGreaterThanOrEquals

如果 大于或等于 指定的 筛选器 值,则 NumberGreaterThanOrEquals 运算符的计算结果为 true。The NumberGreaterThanOrEquals operator evaluates to true if the key value is greater than or equal to the specified filter value. 在以下示例中,它将检查 data 节中 counter 特性的值是否大于或等于 30。In the following example, it checks whether the value of the counter attribute in the data section is greater than or equal to 30.

"advancedFilters": [{
    "operatorType": "NumberGreaterThanOrEquals",
    "key": "data.counter",
    "value": 30
}]

如果键为数组,则会对照筛选器值检查数组中的所有值。If the key is an array, all the values in the array are checked against the filter value. 下面是键为 [v1, v2, v3] 的伪代码。Here's the pseudo code with the key: [v1, v2, v3]. 其数据类型与筛选器数据类型不匹配的任何键值都会被忽略。Any key values with data types that don't match the filter's data type are ignored.

FOR_EACH key IN (v1, v2, v3)
    IF key >= filter
        MATCH

NumberInRangeNumberInRange

如果 值在指定的 筛选器范围 之一内,则 NumberInRange 运算符的计算结果为 true。The NumberInRange operator evaluates to true if the key value is in one of the specified filter ranges. 在以下示例中,它将检查 data 节中 key1 特性的值是否在以下两个范围之内:3.14159 - 999.95,3000 - 4000。In the following example, it checks whether the value of the key1 attribute in the data section is in one of the two ranges: 3.14159 - 999.95, 3000 - 4000.

{
    "operatorType": "NumberInRange",
    "key": "data.key1",
    "values": [[3.14159, 999.95], [3000, 4000]]
}

values 属性是一个范围数组。The values property is an array of ranges. 在以上示例中,它是两个范围的数组。In the previous example, it's an array of two ranges. 下面是具有一个要检查的范围的数组示例。Here's an example of an array with one range to check.

具有一个范围的数组:Array with one range:

{
    "operatorType": "NumberInRange",
    "key": "data.key1",
    "values": [[3000, 4000]]
}

如果键为数组,则会对照筛选器值的数组检查数组中的所有值。If the key is an array, all the values in the array are checked against the array of filter values. 下面是键为 [v1, v2, v3]、筛选器为范围数组的伪代码。Here's the pseudo code with the key: [v1, v2, v3] and the filter: an array of ranges. 在此伪代码中,ab 为数组中每个范围的最低值和最高值。In this pseudo code, a and b are low and high values of each range in the array. 其数据类型与筛选器数据类型不匹配的任何键值都会被忽略。Any key values with data types that don't match the filter's data type are ignored.

FOR_EACH (a,b) IN filter.Values
    FOR_EACH key IN (v1, v2, v3)
       IF key >= a AND key <= b
           MATCH

NumberNotInRangeNumberNotInRange

如果 不在 任何指定的 筛选器范围 内,则 NumberNotInRange 运算符的计算结果为 true。The NumberNotInRange operator evaluates to true if the key value is not in any of the specified filter ranges. 在以下示例中,它将检查 data 节中 key1 特性的值是否在以下两个范围之内:3.14159 - 999.95,3000 - 4000。In the following example, it checks whether the value of the key1 attribute in the data section is in one of the two ranges: 3.14159 - 999.95, 3000 - 4000. 如果是,则运算符返回 false。If it's, the operator returns false.

{
    "operatorType": "NumberNotInRange",
    "key": "data.key1",
    "values": [[3.14159, 999.95], [3000, 4000]]
}

values 属性是一个范围数组。The values property is an array of ranges. 在以上示例中,它是两个范围的数组。In the previous example, it's an array of two ranges. 下面是具有一个要检查的范围的数组示例。Here's an example of an array with one range to check.

具有一个范围的数组:Array with one range:

{
    "operatorType": "NumberNotInRange",
    "key": "data.key1",
    "values": [[3000, 4000]]
}

如果键为数组,则会对照筛选器值的数组检查数组中的所有值。If the key is an array, all the values in the array are checked against the array of filter values. 下面是键为 [v1, v2, v3]、筛选器为范围数组的伪代码。Here's the pseudo code with the key: [v1, v2, v3] and the filter: an array of ranges. 在此伪代码中,ab 为数组中每个范围的最低值和最高值。In this pseudo code, a and b are low and high values of each range in the array. 其数据类型与筛选器数据类型不匹配的任何键值都会被忽略。Any key values with data types that don't match the filter's data type are ignored.

FOR_EACH (a,b) IN filter.Values
    FOR_EACH key IN (v1, v2, v3)
        IF key >= a AND key <= b
            FAIL_MATCH

可用的布尔值运算符为:The available operator for booleans is:

BoolEqualsBoolEquals

如果 值为指定的布尔值 筛选器,则 BoolEquals 运算符的计算结果为 true。The BoolEquals operator evaluates to true if the key value is the specified boolean value filter. 在以下示例中,它将检查 data 节中 isEnabled 特性的值是否为 trueIn the following example, it checks whether the value of the isEnabled attribute in the data section is true.

"advancedFilters": [{
    "operatorType": "BoolEquals",
    "key": "data.isEnabled",
    "value": true
}]

如果键为数组,则会对照筛选器布尔值检查数组中的所有值。If the key is an array, all the values in the array are checked against the filter boolean value. 下面是键为 [v1, v2, v3] 的伪代码。Here's the pseudo code with the key: [v1, v2, v3]. 其数据类型与筛选器数据类型不匹配的任何键值都会被忽略。Any key values with data types that don't match the filter's data type are ignored.

FOR_EACH key IN (v1, v2, v3)
    IF filter == key
        MATCH

可用的字符串运算符为:The available operators for strings are:

StringContainsStringContains

如果 包含 任何指定的 筛选器 值(作为子字符串),则 StringContains 的计算结果为 true。The StringContains evaluates to true if the key value contains any of the specified filter values (as substrings). 在以下示例中,它将检查 data 节中 key1 特性的值是否包含指定的子字符串之一:microsoftazureIn the following example, it checks whether the value of the key1 attribute in the data section contains one of the specified substrings: microsoft or azure. 例如,azure data factory 中包含 azureFor example, azure data factory has azure in it.

"advancedFilters": [{
    "operatorType": "StringContains",
    "key": "data.key1",
    "values": [
        "microsoft", 
        "azure"
    ]
}]

如果键为数组,则会对照筛选器值的数组检查数组中的所有值。If the key is an array, all the values in the array are checked against the array of filter values. 下面是键为 [v1, v2, v3]、筛选器为 [a,b,c] 的伪代码。Here's the pseudo code with the key: [v1, v2, v3] and the filter: [a,b,c]. 其数据类型与筛选器数据类型不匹配的任何键值都会被忽略。Any key values with data types that don't match the filter's data type are ignored.

FOR_EACH filter IN (a, b, c)
    FOR_EACH key IN (v1, v2, v3)
        IF key CONTAINS filter
            MATCH

StringNotContainsStringNotContains

如果 不包含 指定的 筛选器 值作为子字符串,则 StringNotContains 运算符的计算结果为 true。The StringNotContains operator evaluates to true if the key does not contain the specified filter values as substrings. 如果键包含一个指定值作为子字符串,则运算符的计算结果为 false。If the key contains one of the specified values as a substring, the operator evaluates to false. 在以下示例中,仅当 data 节中 key1 特性的值不包含 contosofabrikam 作为子字符串时,运算符才返回 true。In the following example, the operator returns true only if the value of the key1 attribute in the data section doesn't have contoso and fabrikam as substrings.

"advancedFilters": [{
    "operatorType": "StringNotContains",
    "key": "data.key1",
    "values": [
        "contoso", 
        "fabrikam"
    ]
}]

如果键为数组,则会对照筛选器值的数组检查数组中的所有值。If the key is an array, all the values in the array are checked against the array of filter values. 下面是键为 [v1, v2, v3]、筛选器为 [a,b,c] 的伪代码。Here's the pseudo code with the key: [v1, v2, v3] and the filter: [a,b,c]. 其数据类型与筛选器数据类型不匹配的任何键值都会被忽略。Any key values with data types that don't match the filter's data type are ignored.

FOR_EACH filter IN (a, b, c)
    FOR_EACH key IN (v1, v2, v3)
        IF key CONTAINS filter
            FAIL_MATCH

有关此运算符的当前限制,请参阅限制部分。See Limitations section for current limitation of this operator.

StringBeginsWithStringBeginsWith

如果 值以任何指定的 筛选器开头,则 StringBeginsWith 运算符的计算结果为 true。The StringBeginsWith operator evaluates to true if the key value begins with any of the specified filter values. 在以下示例中,它将检查 data 节中 key1 特性的值是否以 eventgrid 开头。In the following example, it checks whether the value of the key1 attribute in the data section begins with event or grid. 例如,event hubsevent 开头。For example, event hubs begins with event.

"advancedFilters": [{
    "operatorType": "StringBeginsWith",
    "key": "data.key1",
    "values": [
        "event", 
        "message"
    ]
}]

如果键为数组,则会对照筛选器值的数组检查数组中的所有值。If the key is an array, all the values in the array are checked against the array of filter values. 下面是键为 [v1, v2, v3]、筛选器为 [a,b,c] 的伪代码。Here's the pseudo code with the key: [v1, v2, v3] and the filter: [a,b,c]. 其数据类型与筛选器数据类型不匹配的任何键值都会被忽略。Any key values with data types that don't match the filter's data type are ignored.

FOR_EACH filter IN (a, b, c)
    FOR_EACH key IN (v1, v2, v3)
        IF key BEGINS_WITH filter
            MATCH

StringNotBeginsWithStringNotBeginsWith

如果 以任何指定的 筛选器 值开头,则 StringNotBeginsWith 运算符的计算结果为 true。The StringNotBeginsWith operator evaluates to true if the key value does not begin with any of the specified filter values. 在以下示例中,它将检查 data 节中 key1 特性的值是否不以 eventmessage 开头。In the following example, it checks whether the value of the key1 attribute in the data section doesn't begin with event or message.

"advancedFilters": [{
    "operatorType": "StringNotBeginsWith",
    "key": "data.key1",
    "values": [
        "event", 
        "message"
    ]
}]

如果键为数组,则会对照筛选器值的数组检查数组中的所有值。If the key is an array, all the values in the array are checked against the array of filter values. 下面是键为 [v1, v2, v3]、筛选器为 [a,b,c] 的伪代码。Here's the pseudo code with the key: [v1, v2, v3] and the filter: [a,b,c]. 其数据类型与筛选器数据类型不匹配的任何键值都会被忽略。Any key values with data types that don't match the filter's data type are ignored.

FOR_EACH filter IN (a, b, c)
    FOR_EACH key IN (v1, v2, v3)
        IF key BEGINS_WITH filter
            FAIL_MATCH

StringEndsWithStringEndsWith

如果 值以指定的 筛选器 值之一 结尾,则 StringEndsWith 运算符的计算结果为 true。The StringEndsWith operator evaluates to true if the key value ends with one of the specified filter values. 在以下示例中,它将检查 data 节中 key1 特性的值是否以 jpgjpegpng 结尾。In the following example, it checks whether the value of the key1 attribute in the data section ends with jpg or jpeg or png. 例如,eventgrid.pngpng 结尾。For example, eventgrid.png ends with png.

"advancedFilters": [{
    "operatorType": "StringEndsWith",
    "key": "data.key1",
    "values": [
        "jpg", 
        "jpeg", 
        "png"
    ]
}]

如果键为数组,则会对照筛选器值的数组检查数组中的所有值。If the key is an array, all the values in the array are checked against the array of filter values. 下面是键为 [v1, v2, v3]、筛选器为 [a,b,c] 的伪代码。Here's the pseudo code with the key: [v1, v2, v3] and the filter: [a,b,c]. 其数据类型与筛选器数据类型不匹配的任何键值都会被忽略。Any key values with data types that don't match the filter's data type are ignored.

FOR_EACH filter IN (a, b, c)
    FOR_EACH key IN (v1, v2, v3)
        IF key ENDS_WITH filter
            MATCH

StringNotEndsWithStringNotEndsWith

如果 以任何指定的 筛选器 值结尾,则 StringNotEndsWith 运算符的计算结果为 true。The StringNotEndsWith operator evaluates to true if the key value does not end with any of the specified filter values. 在以下示例中,它将检查 data 节中 key1 特性的值是否不以 jpgjpegpng 结尾。In the following example, it checks whether the value of the key1 attribute in the data section doesn't end with jpg or jpeg or png.

"advancedFilters": [{
    "operatorType": "StringNotEndsWith",
    "key": "data.key1",
    "values": [
        "jpg", 
        "jpeg", 
        "png"
    ]
}]

如果键为数组,则会对照筛选器值的数组检查数组中的所有值。If the key is an array, all the values in the array are checked against the array of filter values. 下面是键为 [v1, v2, v3]、筛选器为 [a,b,c] 的伪代码。Here's the pseudo code with the key: [v1, v2, v3] and the filter: [a,b,c]. 其数据类型与筛选器数据类型不匹配的任何键值都会被忽略。Any key values with data types that don't match the filter's data type are ignored.

FOR_EACH filter IN (a, b, c)
    FOR_EACH key IN (v1, v2, v3)
        IF key ENDS_WITH filter
            FAIL_MATCH

StringInStringIn

StringIn 运算符检查 值是否与指定的 筛选器 值之一 完全匹配The StringIn operator checks whether the key value exactly matches one of the specified filter values. 在以下示例中,它将检查 data 节中 key1 特性的值是否为 exactstringmatchesIn the following example, it checks whether the value of the key1 attribute in the data section is exact or string or matches.

"advancedFilters": [{
    "operatorType": "StringIn",
    "key": "data.key1",
    "values": [
        "contoso", 
        "fabrikam", 
        "factory"
    ]
}]

如果键为数组,则会对照筛选器值的数组检查数组中的所有值。If the key is an array, all the values in the array are checked against the array of filter values. 下面是键为 [v1, v2, v3]、筛选器为 [a,b,c] 的伪代码。Here's the pseudo code with the key: [v1, v2, v3] and the filter: [a,b,c]. 其数据类型与筛选器数据类型不匹配的任何键值都会被忽略。Any key values with data types that don't match the filter's data type are ignored.

FOR_EACH filter IN (a, b, c)
    FOR_EACH key IN (v1, v2, v3)
        IF filter == key
            MATCH

StringNotInStringNotIn

StringNotIn 运算符检查 值是否与任何指定的 筛选器不匹配The StringNotIn operator checks whether the key value does not match any of the specified filter values. 在以下示例中,它将检查 data 节中 key1 特性的值是否不是 awsbridgeIn the following example, it checks whether the value of the key1 attribute in the data section isn't aws and bridge.

"advancedFilters": [{
    "operatorType": "StringNotIn",
    "key": "data.key1",
    "values": [
        "aws", 
        "bridge"
    ]
}]

如果键为数组,则会对照筛选器值的数组检查数组中的所有值。If the key is an array, all the values in the array are checked against the array of filter values. 下面是键为 [v1, v2, v3]、筛选器为 [a,b,c] 的伪代码。Here's the pseudo code with the key: [v1, v2, v3] and the filter: [a,b,c]. 其数据类型与筛选器数据类型不匹配的任何键值都会被忽略。Any key values with data types that don't match the filter's data type are ignored.

FOR_EACH filter IN (a, b, c)
    FOR_EACH key IN (v1, v2, v3)
        IF filter == key
            FAIL_MATCH

所有字符串比较都不区分大小写。All string comparisons aren't case-sensitive.

备注

如果事件 JSON 不包含高级筛选键,则对于以下运算符,筛选器的计算结果为“不匹配”:NumberGreaterThan、NumberGreaterThanOrEquals、NumberLessThan、NumberLessThanOrEquals、NumberIn、BoolEquals、StringContains、StringNotContains、StringBeginsWith、StringNotBeginsWith、StringEndsWith、StringNotEndsWith、StringIn。If the event JSON doesn't contain the advanced filter key, filter is evaulated as not matched for the following operators: NumberGreaterThan, NumberGreaterThanOrEquals, NumberLessThan, NumberLessThanOrEquals, NumberIn, BoolEquals, StringContains, StringNotContains, StringBeginsWith, StringNotBeginsWith, StringEndsWith, StringNotEndsWith, StringIn.

对于以下运算符,筛选器的计算结果为“匹配”:NumberNotIn、StringNotIn。The filter is evaulated as matched for the following operators:NumberNotIn, StringNotIn.

IsNullOrUndefinedIsNullOrUndefined

如果键的值为 NULL 或未定义,则 IsNullOrUndefined 运算符的计算结果为 true。The IsNullOrUndefined operator evaluates to true if the key's value is NULL or undefined.

{
    "operatorType": "IsNullOrUndefined",
    "key": "data.key1"
}

在以下示例中,缺少 key1,因此运算符的计算结果为 true。In the following example, key1 is missing, so the operator would evaluate to true.

{ 
    "data": 
    { 
        "key2": 5 
    } 
}

在以下示例中,key1 设置为 null,因此运算符的计算结果为 true。In the following example, key1 is set to null, so the operator would evaluate to true.

{
    "data": 
    { 
        "key1": null
    }
}

如果 key1 在这些示例中具有其他任何值,则运算符的计算结果将为 false。if key1 has any other value in these examples, the operator would evaluate to false.

IsNotNullIsNotNull

如果键的值不为 NULL 或未定义,则 IsNotNull 运算符的计算结果为 true。The IsNotNull operator evaluates to true if the key's value isn't NULL or undefined.

{
    "operatorType": "IsNotNull",
    "key": "data.key1"
}

OR 和 ANDOR and AND

如果指定具有多个值的单个筛选器,则将执行 OR 操作,因此键字段的值必须是这些值之一。If you specify a single filter with multiple values, an OR operation is performed, so the value of the key field must be one of these values. 以下是示例:Here is an example:

"advancedFilters": [
    {
        "operatorType": "StringContains",
        "key": "Subject",
        "values": [
            "/providers/microsoft.devtestlab/",
            "/providers/Microsoft.Compute/virtualMachines/"
        ]
    }
]

如果指定多个不同的筛选器,则将执行 AND 操作,因此必须满足每个筛选器条件。If you specify multiple different filters, an AND operation is done, so each filter condition must be met. 下面是一个示例:Here's an example:

"advancedFilters": [
    {
        "operatorType": "StringContains",
        "key": "Subject",
        "values": [
            "/providers/microsoft.devtestlab/"
        ]
    },
    {
        "operatorType": "StringContains",
        "key": "Subject",
        "values": [
            "/providers/Microsoft.Compute/virtualMachines/"
        ]
    }
]

CloudEventsCloudEvents

对于 CloudEvents 架构 中的事件,请使用以下键值:eventidsourceeventtypeeventtypeversion 或事件数据(如 data.key1)。For events in the CloudEvents schema, use the following values for the key: eventid, source, eventtype, eventtypeversion, or event data (like data.key1).

还可以使用 CloudEvents 1.0 中的扩展上下文特性You can also use extension context attributes in CloudEvents 1.0. 在以下示例中,comexampleextension1comexampleothervalue 是扩展上下文特性。In the following example, comexampleextension1 and comexampleothervalue are extension context attributes.

{
    "specversion" : "1.0",
    "type" : "com.example.someevent",
    "source" : "/mycontext",
    "id" : "C234-1234-1234",
    "time" : "2018-04-05T17:31:00Z",
    "subject": null,
    "comexampleextension1" : "value",
    "comexampleothervalue" : 5,
    "datacontenttype" : "application/json",
    "data" : {
        "appinfoA" : "abc",
        "appinfoB" : 123,
        "appinfoC" : true
    }
}

下面是在筛选器中使用扩展上下文特性的示例。Here's an example of using an extension context attribute in a filter.

"advancedFilters": [{
    "operatorType": "StringBeginsWith",
    "key": "comexampleothervalue",
    "values": [
        "5", 
        "1"
    ]
}]

限制Limitations

高级筛选具有以下限制:Advanced filtering has the following limitations:

  • 每个事件网格订阅的所有筛选器都有 5 个高级筛选器和 25 个筛选器值5 advanced filters and 25 filter values across all the filters per event grid subscription
  • 每个字符串值有 512 个字符512 characters per string value
  • “in”和“not in”运算符有 5 个值 Five values for in and not in operators
  • StringNotContains 运算符当前在门户中不可用。The StringNotContains operator is currently not available in the portal.
  • 具有 .(点)字符的键。Keys with . (dot) character in them. 例如: http://schemas.microsoft.com/claims/authnclassreferencejohn.doe@contoso.comFor example: http://schemas.microsoft.com/claims/authnclassreference or john.doe@contoso.com. 目前不支持键中使用转义字符。Currently, there's no support for escape characters in keys.

可以在多个筛选器中使用相同的键。The same key can be used in more than one filter.

后续步骤Next steps