

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用來源篩選條件
<a name="CHAP_Tasks.CustomizingTasks.Filters"></a>

您可以使用來源篩選條件來限制從來源傳輸到目標的記錄數量和類型。例如，您可以指定只將位置是總部的員工移動到目標資料庫。篩選條件是選取規則的一部分。您可以在資料的資料行上套用篩選條件。

來源篩選條件必須遵循這些限制條件：
+ 選取規則可以沒有篩選條件，或擁有一或多個篩選條件。
+ 每個篩選可以擁有一或多個篩選條件。
+ 若使用超過一個篩選條件，則會合併篩選條件清單，就好像在篩選條件間使用 AND 運算子。
+ 若在單一篩選中使用超過一個篩選條件，則會合併篩選條件清單，就好像在篩選條件間使用 OR 運算子。
+ 篩選條件只會在 `rule-action = 'include'` 時套用。
+ 篩選需要資料行名稱和篩選條件清單。篩選條件必須具有與一個值、兩個值或無值相關聯的篩選運算子，具體取決於運算子。
+ 資料行名稱、資料表名稱、檢視名稱和結構描述名稱都區分大小寫。Oracle 和 Db2 應一律使用大寫。
+ 篩選器僅支援具有精確名稱的資料表。篩選器不支援萬用字元。

下列限制適用於使用來源篩選條件：
+ 篩選條件不會計算從右到左語言的欄。
+ 請勿將篩選條件套用至 LOB 欄。
+ 僅將篩選條件套用至*不可變*欄，在建立後不會更新。如果來源篩選條件套用於*可變欄*時，在建立後更新並可能會導致不良行為。

  例如，在欄中排除或包含特定資料列的篩選條件一律排除或包含指定的資料列，即使這些資料列之後有所變更。假設您在 A 欄中排除或包含資料列 1-10，然後它們稍後會變更為資料列 11-20。在這種情況下，即使資料不再相同，也會繼續排除或包含這些資料。

  同樣地，假設篩選條件範圍外的資料列稍後更新了 (或更新後刪除)，則應該依據篩選條件的定義予以排除或包含。在這種情況下，它將複寫至目標。

使用來源篩選條件時，適用下列其他考量事項：
+ 我們建議您使用篩選定義和主索引鍵中包含的資料欄來建立索引。

## 以 JSON 建立來源篩選條件規則
<a name="CHAP_Tasks.CustomizingTasks.Filters.Applying"></a>

您可以使用選擇規則的 JSON `filters` 參數建立來源篩選條件。`filters` 參數指定一或多個 JSON 物件所組成的陣列。每個物件都有參數指定來源篩選類型、欄名稱和篩選條件。這些篩選條件包括一或多個篩選運算子和篩選值。

下表顯示在 `filters` 物件中用於指定來源篩選的參數。


|  參數  |  Value  | 
| --- | --- | 
|   `filter-type`   | source | 
|  `column-name`  |  此參數具有您希望套用篩選條件的來源資料欄名稱。名稱區分大小寫。  | 
|  `filter-conditions`  | 一或多個物件組成的陣列，其中包含 filter-operator 參數和零，或是更多相關聯的值參數，具體取決於 filter-operator 值。 | 
|  `filter-operator`  |  具有下列其中一個值的參數： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/dms/latest/userguide/CHAP_Tasks.CustomizingTasks.Filters.html)  | 
|  `value` 或 `start-value` 和 `end-value` 或 沒有值  |  零個或多個值參數關聯至 `filter-operator`： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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)` 或 `(empid is between 50 and 75)` 或 `(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)` 和 `(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.SSS 進行篩選。 AWS DMS 比較函數遵循 SQLite 慣例。如需 SQLite 資料類型和日期比較的詳細資訊，請參閱 SQLite 文件中的 [Datatypes In SQLite Version 3](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"
            }]
        }]
    }]
}
```