

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 用于亚马逊事件模式的比较运算符 EventBridge
<a name="eb-create-pattern-operators"></a>

以下是中所有可用的比较运算符的摘要 EventBridge。

比较运算符仅适用于叶节点，`$or` 和 `anything-but` 除外。


| **Comparison**（比较） | **示例** | **Rule syntax**（规则语法） | **事件总线支持** | **管道支持** | 
| --- | --- | --- | --- | --- | 
|  And  |  位置为“纽约”，日期为“星期一”  |  `"Location": [ "New York" ], "Day": ["Monday"]`  |  支持  |  是  | 
| [Anything-but](#eb-filtering-anything-but) | 状态是除了“initializing”之外的任何值。 | `"state": [ { "anything-but": "initializing" } ]` |  支持  |  是  | 
| [Anything-but (begins with)](#eb-filtering-anything-but-prefix) | 区域不在美国。 |  `"Region": [ { "anything-but": {"prefix": "us-" } } ]`  |  是  |  否  | 
| [Anything-but (ends with)](#eb-filtering-anything-but-suffix) | FileName 不以.png 扩展名结尾。 |  `"FileName": [ { "anything-but": { "suffix": ".png" } } ]`  |  是  |  否  | 
| [Anything-but (ignore case)](#eb-filtering-anything-but-ignore-case) | 状态是除了“initializing”或任何其他大小写变体（例如“INITIALIZING”）之外的任何值。 | `"state": : [{ "anything-but": { "equals-ignore-case": "initializing" }}]}` |  是  |  否  | 
| [Anything-but using a wildcard](#eb-filtering-anything-but-wildcard) | FileName 不是包含以下内容的文件路径`/lib/`。 |  `"FilePath" : [{ "anything-but": { "wildcard": "*/lib/*" }}]`  |  是  |  否  | 
|  [开头](#eb-filtering-prefix-matching)  |  区域位于美国。  |  `"Region": [ {"prefix": "us-" } ]`  |  支持  |  是  | 
| Begins with (ignore case) | 服务名称以字母“eventb”开头（不区分大小写）。 | `{"service" : [{ "prefix": { "equals-ignore-case": "eventb" }}]}` |  支持  |  是  | 
|  [空](eb-event-patterns-null-values.md)  |  LastName 为空。  |  `"LastName": [""]`  |  支持  |  是  | 
|  Equals  |  名字为“Alice”  |  `"Name": [ "Alice" ]`  |  支持  |  是  | 
|  [等于（忽略大小写）](#eb-filtering-equals-ignore-case-matching)  |  名字为“Alice”  |  `"Name": [ { "equals-ignore-case": "alice" } ]`  |  支持  |  是  | 
|  [结束](#eb-filtering-suffix-matching)  |  FileName 以.png 扩展名结尾  |  `"FileName": [ { "suffix": ".png" } ]`  |  支持  |  是  | 
| Ends with (ignore case) | 服务名称以字母“tbridge”或任何其他大小写变体（例如“TBRIDGE”）结尾。 | `{"service" : [{ "suffix": { "equals-ignore-case": "tBridge" }}]}` |  支持  |  是  | 
|  [存在](#eb-filtering-exists-matching)  |  ProductName 存在  |  `"ProductName": [ { "exists": true } ]`  |  支持  |  是  | 
|  [不存在](#eb-filtering-exists-matching)  |  ProductName 不存在  |  `"ProductName": [ { "exists": false } ]`  |  支持  |  是  | 
|  [非](#eb-filtering-anything-but)  |  天气是除“下雨”以外的任何天气  |  `"Weather": [ { "anything-but": [ "Raining" ] } ]`  |  支持  |  是  | 
|  [Null](eb-event-patterns-null-values.md)  |  用户 ID 为空  |  `"UserID": [ null ]`  |  支持  |  是  | 
|  [数值（等于）](#filtering-numeric-matching)  |  价格为 100  |  `"Price": [ { "numeric": [ "=", 100 ] } ]`  |  支持  |  是  | 
|  [数值（范围）](#filtering-numeric-matching)  |  价格大于 10，且小于等于 20  |  `"Price": [ { "numeric": [ ">", 10, "<=", 20 ] } ]`  |  支持  |  是  | 
|  Or  |  PaymentType 是 “贷方” 或 “借记卡”  |  `"PaymentType": [ "Credit", "Debit"]`  |  支持  |  是  | 
|  [或（多个字段）](#eb-filtering-complex-example-or)  |  位置为“纽约”，或日期为“星期一”。  |  `"$or": [ { "Location": [ "New York" ] }, { "Day": [ "Monday" ] } ]`  |  支持  |  是  | 
|  [通配符](#eb-filtering-wildcard-matching)  |  位于“dir”文件夹中的任何扩展名为 .png 的文件  |  `"FileName": [ { "wildcard": "dir/*.png" } ] `  |  是  |  否  | 

## 前缀匹配
<a name="eb-filtering-prefix-matching"></a>

您可以根据事件源中值的前缀匹配事件。您可以对字符串值使用 prefix 匹配。

例如，以下事件模式将匹配 `"time"` 字段以 `"2017-10-02"` 开头的任何事件，例如 `"time": "2017-10-02T18:43:48Z"`。

```
{
  "time": [ { "prefix": "2017-10-02" } ]
}
```

### Prefix 匹配（忽略大小写）
<a name="eb-filtering-prefix-matching-ignore-case"></a>

您也可以将 `equals-ignore-case` 与 `prefix.` 搭配使用，在匹配前缀值时，不考虑值开头字符的大小写。

例如，以下事件模式将匹配其中 `service` 字段以 `EventB`、`EVENTB`、`eventb` 或这些字符的任何其他大写开头的任何事件。

```
{
  "detail": {"service" : [{ "prefix": { "equals-ignore-case": "EventB" }}]}
}
```

## 后缀匹配
<a name="eb-filtering-suffix-matching"></a>

您可以根据事件源中值的后缀匹配事件。您可以对字符串值使用后缀匹配。

例如，以下事件模式将匹配 `"FileName"` 字段以 `.png` 文件扩展名结尾的任何事件。

```
{
  "FileName": [ { "suffix": ".png" } ]
}
```

### Suffix 匹配（忽略大小写）
<a name="eb-filtering-suffix-matching-ignore-case"></a>

您也可以将 `equals-ignore-case` 与 `suffix.` 搭配使用，在匹配后缀值时，不考虑值开头字符的大小写。

例如，以下事件模式将匹配其中 `FileName` 字段以 `.png`、`.PNG` 或这些字符的任何其他大写结尾的任何事件。

```
{
  "detail": {"FileName" : [{ "suffix": { "equals-ignore-case": ".png" }}]}
}
```

## Anything-but 匹配
<a name="eb-filtering-anything-but"></a>

*Anything-but* 匹配会匹配除规则中指定的内容之外的任何内容。

您可以将 Anything-but 匹配与字符串和数值一起使用，包括仅包含字符串或仅包含数值的列表。

以下事件模式显示了 Anything-but 匹配与字符串和数字。

```
{
  "detail": {
    "state": [ { "anything-but": "initializing" } ]
  }
}

{
  "detail": {
    "x-limit": [ { "anything-but": 123 } ]
  }
}
```

以下事件模式显示了 Anything-but 匹配与一组字符串。

```
{
  "detail": {
    "state": [ { "anything-but": [ "stopped", "overloaded" ] } ]
  }
}
```

以下事件模式显示了 Anything-but 匹配与一组数字。

```
{
  "detail": {
    "x-limit": [ { "anything-but": [ 100, 200, 300 ] } ]
  }
}
```

### Anything-but 匹配（忽略大小写）
<a name="eb-filtering-anything-but-ignore-case"></a>

您也可以将 `equals-ignore-case` 与 `anything-but` 搭配使用，匹配字符串值，不考虑字符大小写。

以下事件模式将匹配不包含字符串“initializing”、“INITIALIZING”、“Initializing”或这些字符的任何其他大写字母的 `state` 字段。

```
{
  "detail": {"state" : [{ "anything-but": { "equals-ignore-case": "initializing" }}]}
}
```

您也可以将 `equals-ignore-case` 与 `anything-but` 搭配使用匹配值列表：

```
{
  "detail": {"state" : [{ "anything-but": { "equals-ignore-case": ["initializing", "stopped"] }}]}
}
```

### 对前缀使用 anything-but 匹配
<a name="eb-filtering-anything-but-prefix"></a>

您可以将 `prefix` 与 `anything-but` 搭配使用来匹配不以指定值开头的字符串值。这包括单个的值或值的列表。

以下事件模式显示了与 `"state"` 字段中没有 `"init"` 前缀的任何事件相匹配的 anything-but 匹配。

```
{
  "detail": {
    "state": [ { "anything-but": { "prefix": "init" } } ]
  }
}
```

以下事件模式显示了与一个前缀值列表搭配使用的 anything-but 匹配。此事件模式将匹配 `"state"` 字段中没有前缀 `"init"` 或 `"stop"` 的任何事件。

```
{
"detail": {
  "state" : [{ "anything-but": { "prefix": ["init", "stop"] } } ] }
  }
}
```

### 对后缀使用 anything-but 匹配
<a name="eb-filtering-anything-but-suffix"></a>

您可以将 `suffix` 与 `anything-but` 搭配使用来匹配不以指定值结尾的字符串值。这包括单个的值或值的列表。

以下事件模式将匹配 `FileName` 字段中不以 `.txt` 结尾的任何值。

```
{
  "detail": {
    "FileName": [ { "anything-but": { "suffix": ".txt" } } ]
  }
}
```

以下事件模式显示了与一个后缀值列表搭配使用的 anything-but 匹配。此事件模式将匹配 `FileName` 字段中不以 `.txt` 或 `.rtf` 结尾的任何值。

```
{
  "detail": {
    "FileName": [ { "anything-but": { "suffix": [".txt", ".rtf"] } } ]
  }
}
```

### Anything-but 匹配与通配符搭配使用
<a name="eb-filtering-anything-but-wildcard"></a>

您可以在为 anything-but 匹配指定的值中使用通配符（\$1）。这包括单个的值或值的列表。

以下事件模式将匹配 `FileName` 字段中不包含 `/lib/` 的任何值。

```
{
"detail": {
  "FilePath" : [{ "anything-but": { "wildcard": "*/lib/*" }}]
  }
}
```

以下事件模式显示了与一个含通配符的值列表搭配使用的 anything-but 匹配。此事件模式将匹配 `FileName` 字段中不含 `/lib/` 或 `/bin/` 的任何值。

```
{
"detail": {
  "FilePath" : [{ "anything-but": { "wildcard": ["*/lib/*", "*/bin/*"] }}]
  }
}
```

有关更多信息，请参阅 [使用通配符进行匹配](#eb-filtering-wildcard-matching)。

## 数值匹配
<a name="filtering-numeric-matching"></a>

数值匹配适用于 JSON 数字值。仅限于 -5.0e9 和 \$15.0e9（含）之间的值，精度为 15 位（小数点右侧为六位）。

以下代码显示的事件模式的数值匹配，仅匹配所有字段均为真的事件。

```
{
  "detail": {
    "c-count": [ { "numeric": [ ">", 0, "<=", 5 ] } ],
    "d-count": [ { "numeric": [ "<", 10 ] } ],
    "x-limit": [ { "numeric": [ "=", 3.018e2 ] } ]
  }
}
```

## IP 地址匹配
<a name="eb-filtering-ip-matching"></a>

您可以对 IPv4 和地址使用 IP IPv6 地址匹配。以下事件模式显示的 IP 地址匹配，匹配以 10.0.0 开头并以 0 到 255 之间的数字结尾的 IP 地址。

```
{
  "detail": {
    "sourceIPAddress": [ { "cidr": "10.0.0.0/24" } ]
  }
}
```

## Exists 匹配
<a name="eb-filtering-exists-matching"></a>

*Exists 匹配* 用于确定事件的 JSON 中存在或不存在某个字段。

Exists 匹配仅适用于叶节点。它对于中间节点不起作用。

以下事件模式与任何具有 `detail.state` 字段的事件相匹配。

```
{
  "detail": {
    "state": [ { "exists": true  } ]
  }
}
```

上一事件模式与以下事件匹配。

```
{
  "version": "0",
  "id": "7bf73129-1428-4cd3-a780-95db273d1602",
  "detail-type": "EC2 Instance State-change Notification",
  "source": "aws.ec2",
  "account": "123456789012",
  "time": "2015-11-11T21:29:54Z",
  "region": "us-east-1",
  "resources": ["arn:aws:ec2:us-east-1:123456789012:instance/i-abcd1111"],
  "detail": {
    "instance-id": "i-abcd1111",
    "state": "pending"
  }
}
```

上一事件模式与以下事件不匹配，因为它没有 `detail.state` 字段。

```
{
  "detail-type": [ "EC2 Instance State-change Notification" ],
  "resources": [ "arn:aws:ec2:us-east-1:123456789012:instance/i-02ebd4584a2ebd341" ],
  "detail": {
    "c-count" : {
       "c1" : 100
    }
  }
}
```

## Equals-ignore-case 匹配
<a name="eb-filtering-equals-ignore-case-matching"></a>

无论大小写如何，*E quals-ignore-case* 匹配都适用于字符串值。

以下事件模式匹配 `detail-type` 字段与指定字符串相匹配（无论大小写如何）的任何事件。

```
{
  "detail-type": [ { "equals-ignore-case": "ec2 instance state-change notification" } ]
}
```

上一事件模式与以下事件匹配。

```
{
  "detail-type": [ "EC2 Instance State-change Notification" ],
  "resources": [ "arn:aws:ec2:us-east-1:123456789012:instance/i-02ebd4584a2ebd341" ],
  "detail": {
    "c-count" : {
       "c1" : 100
    }
  }
}
```

## 使用通配符进行匹配
<a name="eb-filtering-wildcard-matching"></a>

您可以使用通配符 (\$1) 匹配事件模式中的字符串值。

**注意**  
目前，只有事件总线规则支持通配符。

在事件模式中使用通配符的注意事项：
+ 可以在给定的字符串值中指定任意数量的通配符；但是，不支持连续通配符。
+ EventBridge 支持使用反斜杠字符 (\$1) 来指定通配符过滤器中的文字 \$1 和\$1 字符：
  + 字符串 `\*` 代表文字 \$1 字符
  + 字符串 `\\` 代表文字 \$1 字符

  不支持使用反斜杠对其他字符进行转义。

### 通配符和事件模式的复杂性
<a name="eb-filtering-wildcard-matching-complexity"></a>

规则使用通配符的复杂程度是有限的。如果规则过于复杂，则`InvalidEventPatternException`在尝试创建规则时 EventBridge 返回。如果您的规则生成此类错误，请考虑使用以下指南来降低事件模式的复杂性：
+ **减少使用的通配符数量**

  仅在确实需要匹配多个可能值的位置使用通配符。例如，考虑以下事件模式，您想匹配同一区域中的事件总线：

  ```
  {
  "EventBusArn": [ { "wildcard": "*:*:*:*:*:event-bus/*" } ]
  }
  ```

  在上述情况下，ARN 的许多部分将直接取决于您的事件总线所在的区域。因此，如果您使用 `us-east-1` 区域，则以下示例可能是不太复杂，但仍可与所需值匹配的模式：

  ```
  {
  "EventBusArn": [ { "wildcard": "arn:aws:events:us-east-1:*:event-bus/*" } ]
  }
  ```
+ **减少通配符后出现的重复字符序列**

  使用通配符后多次出现相同的字符序列，会增加处理事件模式的复杂性。请修改您的事件模式，最大限度地减少重复序列。例如，考虑以下示例，该示例与任何用户的文件名为 `doc.txt` 的文件相匹配：

  ```
  {
  "FileName": [ { "wildcard": "/Users/*/dir/dir/dir/dir/dir/doc.txt" } ]
  }
  ```

  如果您知道 `doc.txt` 文件只会出现在指定路径中，则可以通过以下方式减少重复的字符序列：

  ```
  {
  "FileName": [ { "wildcard": "/Users/*/doc.txt" } ]
  }
  ```

## 具有多个匹配的复杂示例
<a name="eb-filtering-complex-example"></a>

您可以将多匹配条件组合成更复杂的事件模式。例如，以下事件模式组合了 `anything-but` 和 `numeric`。

```
{
  "time": [ { "prefix": "2017-10-02" } ],
  "detail": {
    "state": [ { "anything-but": "initializing" } ],
    "c-count": [ { "numeric": [ ">", 0, "<=", 5 ] } ],
    "d-count": [ { "numeric": [ "<", 10 ] } ],
    "x-limit": [ { "anything-but": [ 100, 200, 300 ] } ]
  }
}
```

**注意**  
在构建事件模式时，如果您多次包含一个密钥，则最后一次引用将用于评估事件。例如，对于以下模式：  

```
{
  "detail": {
    "location": [ { "prefix": "us-" } ],
    "location": [ { "anything-but": "us-east" } ]
  }
}
```
只有在评估 `location` 时才会考虑 `{ "anything-but": "us-east" }`。

## 具有 `$or` 匹配的复杂示例
<a name="eb-filtering-complex-example-or"></a>

您还可以创建复杂的事件模式，检查多个字段中是否有*任何* 字段值匹配。`$or` 用于创建事件模式，匹配多个字段中有任何值匹配的情况。

请注意，在 `$or` 构造中，您可以在各个字段的模式匹配中包含其他筛选器类型，例如[数值匹配](#filtering-numeric-matching)和[数组](eb-event-patterns-arrays.md)。

满足以下任意条件时，匹配以下事件模式：
+ `c-count` 字段大于 0 或小于等于 5。
+ `d-count` 字段小于 10。
+ `x-limit` 字段等于 3.018e2。

```
{
  "detail": {
    "$or": [
      { "c-count": [ { "numeric": [ ">", 0, "<=", 5 ] } ] },
      { "d-count": [ { "numeric": [ "<", 10 ] } ] },
      { "x-limit": [ { "numeric": [ "=", 3.018e2 ] } ] }
    ]
  }
}
```

**注意**  
APIs `InvalidEventPatternException`如果使用`$or`结果超过 1000 个规则组合 `PutRule` `CreateArchive``UpdateArchive`，则接受事件模式（例如、、和`TestEventPattern`）将抛出。  
要确定事件模式中规则组合的数量，请将事件模式中每个 `$or` 数组的参数总数相乘。例如，上面的模式包含一个 `$or` 数组，带有三个参数，因此规则组合的总数也是三个。如果您再添加一个包含两个参数的 `$or` 数组，则规则组合总数将为六个。