

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

 FileMatch ルールを使用すると、ファイルを他のファイルやチェックサムと比較できます。これは、いくつかのシナリオで役立ちます: 

1.  外部ソースから受信したファイルの検証: FileMatch を使用して、チェックサムと比較することで、外部ソースから正しいファイルを受け取ったことを確認できます。これにより、取り込むんでいるデータの整合性を検証できます。

1.  2 つの異なるフォルダのデータの比較: FileMatch を使用して、2 つのフォルダ間でファイルを比較できます。

 このルールは、ルールでスキャンされたファイルの数という 1 つのメトリクスを収集します。

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

 **チェックサムを使用してファイルを検証します:** 

 FileMatch はファイルとセットのチェックサムを受け入れ、少なくとも 1 つのチェックサムがファイルと一致することを確認します。

```
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 がバックアップした) でルールを作成する場合、カタログテーブルが使用しているフォルダを見つけるのが難しい場合があります。 AWSGlue 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 ステータスが Rule failed のルールを示しています。FileMatch が障害について説明します。\]](http://docs.aws.amazon.com/ja_jp/glue/latest/dg/images/data-quality-file-match.png)


 **オプションのファイルベースのルールタグ:** 

 タグを使用すると、ルールの動作を制御できます。

 **recentFiles** 

 このタグは、最新のファイルを最初に保持することで、処理されるファイルの数を制限します。

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

 **uriRegex** 

**注記**  
 `uriRegex` タグは、AWS Glue 5.0 以降で有効です。

 このタグは、ファイルパスに正規表現パターンを適用してファイルをフィルタリングします。パスがパターンに一致するファイルのみが処理されます。負の先読みを使用して、パターンに一致するファイルを除外することもできます。

```
# 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 ステータスが Rule failed のルールを示しています。FileMatch が障害について説明します。\]](http://docs.aws.amazon.com/ja_jp/glue/latest/dg/images/data-quality-file-match-transform.png)

1.  このルールは AWS Glue インタラクティブセッションでは機能しません。