

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

# 使用源筛选器
<a name="CHAP_Tasks.CustomizingTasks.Filters"></a>

您可以使用源筛选器来限制从源传输到目标的记录的数量和类型。例如，您可以指定仅将总部员工移动到目标数据库。筛选器是选择规则的一部分。还可以对数据列应用筛选器。

源筛选器必须遵循以下约束：
+ 选择规则可以不包含筛选器，也可以包含一个或多个筛选器。
+ 每个筛选器可具有一个或多个筛选条件。
+ 如果使用多个筛选器，则将合并一系列筛选器，就像在筛选器之间使用 AND 运算符一样。
+ 如果在单个筛选器中使用多个筛选条件，则将合并一系列筛选条件，就像在筛选条件之间使用 OR 运算符一样。
+ 筛选器仅在 `rule-action = 'include'` 时应用。
+ 筛选器需要一个列名和一系列筛选条件。筛选条件必须具有与一个值或两个值关联或者不与值关联的筛选运算符，具体取决于运算符。
+ 列名、表名、视图名和架构名都区分大小写。Oracle 和 Db2 应始终使用大写字母。
+ 筛选器仅支持具有确切名称的表。筛选器不支持通配符。

以下限制适用于使用源筛选器：
+ 过滤器不计算 right-to-left语言列。
+ 不要将筛选器应用于 LOB 列。
+ 仅将筛选器应用于*不可变* 列，这些列在创建后不会更新。如果将源筛选器应用于*可变*列（可在创建后更新），可能会导致不良行为。

  例如，如果筛选器在某列中排除或包含特定行，则会始终排除或包含这些指定的行，即使该行稍后发生了更改。假设在 A 列中排除或包含第 1-10 行，之后这些行发生了更改，变为了第 11-20 行。在这种情况下，即使数据已经不同，这些行仍旧排除或包含在内。

  同样，假设不在筛选器范围内的某行稍后进行了更新（或者更新并删除），此时按照筛选器的定义应当排除或包含在内。在这种情况下，它会在目标上复制。

使用源筛选条件时，还需要考虑以下其他问题：
+ 建议您使用筛选定义中包含的列和主键创建索引。

## 使用 JSON 格式创建源筛选器规则
<a name="CHAP_Tasks.CustomizingTasks.Filters.Applying"></a>

您可以使用选择规则的 JSON `filters` 参数创建源筛选器。`filters` 参数指定包含一个或多个 JSON 对象的数组。每个对象都具有用于指定源筛选类型、列名称和筛选条件的参数。这些筛选条件包含一个或多个筛选运算符和筛选值。

下表显示了用于在 `filters` 对象中指定源筛选的参数。


|  参数  |  值  | 
| --- | --- | 
|   `filter-type`   | source | 
|  `column-name`  |  要将筛选器应用于的源列的名称。该名称区分大小写。  | 
|  `filter-conditions`  | 一个或多个对象的数组，其中包含一个 filter-operator 参数和 Zero 或多个关联的值参数，具体取决于 filter-operator 值。 | 
|  `filter-operator`  |  具有以下值之一的参数： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/dms/latest/userguide/CHAP_Tasks.CustomizingTasks.Filters.html)  | 
|  `value` 或者 `start-value` 和 `end-value` 或 无值  |  0 或与 `filter-operator` 关联的多个值参数： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/dms/latest/userguide/CHAP_Tasks.CustomizingTasks.Filters.html)  | 

以下示例说明了几种常见的使用源筛选器的方法。

**Example 单一筛选器**  
以下筛选器将 `empid >= 100` 的所有员工复制到目标数据库中。  

```
 {
     "rules": [{
         "rule-type": "selection",
         "rule-id": "1",
         "rule-name": "1",
         "object-locator": {
             "schema-name": "test",
             "table-name": "employee"
         },
         "rule-action": "include",
         "filters": [{
             "filter-type": "source",
             "column-name": "empid",
             "filter-conditions": [{
                "filter-operator": "gte",
                "value": "100"
             }]
         }]
     }]
 }
```

**Example 多个筛选运算符**  
以下筛选器将多个筛选运算符应用于单个数据列。筛选器将 `(empid <= 10)` OR `(empid is between 50 and 75)` OR `(empid >= 100)` 的所有员工复制到目标数据库中。  

```
{
    "rules": [{
        "rule-type": "selection",
        "rule-id": "1",
        "rule-name": "1",
        "object-locator": {
            "schema-name": "test",
            "table-name": "employee"
        },
        "rule-action": "include",
        "filters": [{
            "filter-type": "source",
            "column-name": "empid",
            "filter-conditions": [{
                "filter-operator": "lte",
                "value": "10"
            }, {
                "filter-operator": "between",
                "start-value": "50",
                "end-value": "75"
            }, {
                "filter-operator": "gte",
                "value": "100"
            }]
        }]
    }]
}
```

**Example 多个筛选器**  
以下筛选器将多个筛选条件应用于表中的两个列。筛选器将 `(empid <= 100)` AND `(dept = tech)` 的所有员工复制到目标数据库中。  

```
{
    "rules": [{
        "rule-type": "selection",
        "rule-id": "1",
        "rule-name": "1",
        "object-locator": {
            "schema-name": "test",
            "table-name": "employee"
        },
        "rule-action": "include",
        "filters": [{
            "filter-type": "source",
            "column-name": "empid",
            "filter-conditions": [{
                "filter-operator": "lte",
                "value": "100"
            }]
        }, {
            "filter-type": "source",
            "column-name": "dept",
            "filter-conditions": [{
                "filter-operator": "eq",
                "value": "tech"
            }]
        }]
    }]
}
```

**Example 筛选 NULL 值**  
以下筛选器显示如何根据空值进行筛选。它会将所有 `dept = NULL` 的员工复制到目标数据库。  

```
{
    "rules": [{
        "rule-type": "selection",
        "rule-id": "1",
        "rule-name": "1",
        "object-locator": {
            "schema-name": "test",
            "table-name": "employee"
        },
        "rule-action": "include",
        "filters": [{
            "filter-type": "source",
            "column-name": "dept",
            "filter-conditions": [{
                "filter-operator": "null"
            }]
        }]
    }]
}
```

**Example 使用 NOT 运算符筛选**  
有些运算符可以按否定形式使用。以下筛选器将 `(empid is < 50) OR (empid is > 75)` 的所有员工复制到目标数据库中。  

```
{
    "rules": [{
        "rule-type": "selection",
        "rule-id": "1",
        "rule-name": "1",
        "object-locator": {
            "schema-name": "test",
            "table-name": "employee"
        },
        "rule-action": "include",
        "filters": [{
            "filter-type": "source",
            "column-name": "empid",
            "filter-conditions": [{
                "filter-operator": "notbetween",
                "start-value": "50",
                "end-value": "75"
            }]
        }]
    }]
}
```

**Example 使用混合筛选运算符**  
从 3.5.0 AWS DMS 版开始，您可以混合使用包含运算符和负运算符。  
以下筛选器将 `(empid != 50) AND (dept is not NULL)` 的所有员工复制到目标数据库中。  

```
{
    "rules": [{
        "rule-type": "selection",
        "rule-id": "1",
        "rule-name": "1",
        "object-locator": {
            "schema-name": "test",
            "table-name": "employee"
        },
        "rule-action": "include",
        "filters": [{
            "filter-type": "source",
            "column-name": "empid",
            "filter-conditions": [{
                "filter-operator": "noteq",
                "value": "50"
            }]
        }, {
            "filter-type": "source",
            "column-name": "dept",
            "filter-conditions": [{
                "filter-operator": "notnull"
            }]
        }]
    }]
}
```

将 `null` 与其他筛选运算符一起使用时，请注意以下几点：
+ 如果在同一个筛选器中同时使用包含、否定和 `null` 筛选条件，则不会复制具有 `NULL` 值的记录。
+ 如果在同一个筛选器中同时使用否定和 `null` 筛选条件，但没有包含筛选条件，则不会复制任何数据。
+ 如果使用否定筛选条件但未明确设置 `null` 筛选条件，则不会复制具有 `NULL` 值的记录。

## 按时间和日期筛选
<a name="CHAP_Tasks.CustomizingTasks.Filters.Dates"></a>

在选择要导入的数据时，您可以指定日期或时间作为筛选条件的一部分。 AWS DMS 使用日期格式 YYYY-MM-DD和时间格式 YYYY-MM-DD HH: MM: SS.SS.SSS 进行筛选。 AWS DMS 比较函数遵循 SQLite 惯例。有关 SQLite 数据类型和日期比较的更多信息，请参阅[文档中 SQLite 版本 3 中的 SQLite 数据类型](https://sqlite.org/datatype3.html)。

以下筛选器显示了如何基于日期进行筛选。它会将所有 `empstartdate >= January 1, 2002` 的员工复制到目标数据库。

**Example 单一日期筛选器**  

```
{
    "rules": [{
        "rule-type": "selection",
        "rule-id": "1",
        "rule-name": "1",
        "object-locator": {
            "schema-name": "test",
            "table-name": "employee"
        },
        "rule-action": "include",
        "filters": [{
            "filter-type": "source",
            "column-name": "empstartdate",
            "filter-conditions": [{
                "filter-operator": "gte",
                "value": "2002-01-01"
            }]
        }]
    }]
}
```