

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

# FileMatch
<a name="dqdl-rule-types-FileMatch"></a>

 FileMatch 規則可讓您將檔案與其他檔案或檢查總和進行比較。這在幾個案例中很有用：

1.  驗證從外部來源收到的檔案：您可以使用 FileMatch，透過比較檢查總和來確保您已收到來自外部來源的正確檔案。這有助於驗證您正在擷取的資料的完整性。

1.  比較兩個不同資料夾中的資料：FileMatch 可用於比較兩個資料夾之間的檔案。

 此規則會收集一個指標：規則掃描的檔案數目。

```
{"Dataset.*.FileCount":1}
```

 **使用檢查總和驗證檔案：**

 FileMatch 接受檔案和一組檢查總和，以確保至少一個檢查總和與該檔案相符。

```
FileMatch "s3://amzn-s3-demo-bucket/file.json" in ["3ee0d8617ac041793154713e5ef8f319"] with hashAlgorithm = "MD5"
FileMatch "s3://amzn-s3-demo-bucket/file.json" in ["3ee0d8617ac041793154713e5ef8f319"] with hashAlgorithm = "SHA-1"
FileMatch "s3://amzn-s3-demo-bucket/file.json" in ["3ee0d8617ac041793154713e5ef8f319"] with hashAlgorithm = "SHA-256"
FileMatch "s3://amzn-s3-demo-bucket/file.json" in ["3ee0d8617ac041793154713e5ef8f319"]
```

 支援下列標準演算法：
+ MD5
+ SHA-1
+ SHA-256

 如果您不提供演算法，則預設值為 SHA-256。

 **使用一組檢查總和驗證資料夾中的所有檔案：**

```
FileMatch "s3://amzn-s3-demo-bucket /" in ["3ee0d8617ac041793154713e5ef8f319", "7e8617ac041793154713e5ef8f319"] with hashAlgorithm = "MD5"
FileMatch "s3://amzn-s3-demo-bucket /internal-folder/" in ["3ee0d8617ac041793154713e5ef8f319", "7e8617ac041793154713e5ef8f319"]
```

 **比較不同資料夾中的檔案** 

```
# Compare all files across two buckets
FileMatch "s3://original_bucket/" "s3://archive_bucket/"
# Compare files within specific subfolders
FileMatch "s3://original_bucket/internal-folder/" "s3://original_bucket/other-folder/"
# Compare only .json files across two folders
FileMatch "s3://original_bucket/" "s3://archive_bucket/" with uriRegex = "\.json$"
# Compare only the 5 most recent .csv files
FileMatch "s3://original_bucket/" "s3://archive_bucket/" with recentFiles = 5 with uriRegex = "\.csv$" with filterOrder = ["uriRegex","recentFiles"]
```

 FileMatch 會檢查 `original_bucket` 中的檔案內容，並確保其符合 `archive_bucket` 中的內容。如果規則不完全相符，則規則將會失敗。其也可以檢查內部資料夾或個別檔案的內容。

 FileMatch 也可以互相檢查個別檔案。

```
FileMatch "s3://amzn-s3-demo-bucket /file_old.json" "s3://amzn-s3-demo-bucket /file_new.json"
```

 **直接從資料框推斷檔案名稱** 

 您不一定必須提供檔案路徑。例如，當您在 AWS Glue Data Catalog (Amazon S3 後端） 中編寫規則時，可能很難找到目錄資料表正在使用的資料夾。 AWS Glue Data Quality 可以找到用來填入資料框架的特定資料夾或檔案。

**注意**  
 此功能只有在檔案成功讀取到 DynamicFrame 或 DataFrame 時才有效。

```
FileMatch in ["3ee0d8617ac041793154713e5ef8f319"] with hashAlgorithm = "MD5"
FileMatch in ["3ee0d8617ac041793154713e5ef8f319"] with hashAlgorithm = "SHA-1"
FileMatch in ["3ee0d8617ac041793154713e5ef8f319"] with hashAlgorithm = "SHA-256"
FileMatch in ["3ee0d8617ac041793154713e5ef8f319"]
```

 如果提供的檢查總和與計算結果不同，FileMatch 會提醒您差異。

![\[螢幕擷取畫面顯示 DQ 狀態為「規則失敗」的規則。FileMatch 說明失敗。\]](http://docs.aws.amazon.com/zh_tw/glue/latest/dg/images/data-quality-file-match.png)


 **選用的以檔案為基礎的規則標籤：**

 標籤可讓您控制規則行為。

 **recentFiles** 

 此標籤會先保留最新的檔案，以限制處理的檔案數目。

```
FileMatch "s3://bucket/" in ["3ee0d8617ac04179sam4713e5ef8f319"] with recentFiles = 1
```

 **uriRegex** 

**注意**  
 此`uriRegex`標籤可在 AWS Glue 5.0 和更新版本中使用。

 此標籤透過將 regex 模式套用至檔案路徑來篩選檔案。只會處理路徑符合模式的檔案。您也可以使用負 lookahead 來排除符合模式的檔案。

```
# Match only files with a .json extension
FileMatch "s3://bucket/" in ["3ee0d8617ac04179sam4713e5ef8f319"] with uriRegex = "\.json$"
# Exclude files ending in .tmp using a negative lookahead
FileMatch "s3://bucket/" in ["3ee0d8617ac04179sam4713e5ef8f319"] with uriRegex = "(?!.*\.tmp$).*"
```

 **filterOrder** 

**注意**  
 此`filterOrder`標籤可在 AWS Glue 5.0 和更新版本中使用。

 當您同時使用多個篩選條件標籤，例如 `recentFiles`和 `uriRegex` 時，該`filterOrder`標籤會控制套用它們的順序。預設順序為 `recentFiles` ，然後是 `uriRegex`。

```
FileMatch "s3://bucket/" in ["3ee0d8617ac04179sam4713e5ef8f319"] with recentFiles = 1 with uriRegex = "\.json$" with filterOrder = ["uriRegex","recentFiles"]
```

 **matchFileName** 

 此標籤可確保檔案沒有重複的名稱。預設行為為 false。

```
FileMatch "s3://amzn-s3-demo-bucket/file.json" in ["3ee0d8617ac04179sam4713e5ef8f319"] with matchFileName = "true"
```

 有幾個考量：

1.  在 AWS Glue ETL 中，您必須在 Amazon S3 或 AWS Glue Data Catalog 轉換後立即進行 **EvaluateDataQuality** 轉換。  
![\[螢幕擷取畫面顯示 DQ 狀態為「規則失敗」的規則。FileMatch 說明失敗。\]](http://docs.aws.amazon.com/zh_tw/glue/latest/dg/images/data-quality-file-match-transform.png)

1.  此規則無法在 AWS Glue 互動式工作階段中運作。