

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

 FileFreshness 可根据您提供的条件确保您的数据文件是最新的。其使用文件的最后修改时间来确保数据文件或整个文件夹是最新的。

 此规则收集两个指标：
+ 基于您所设置规则的 FileFreshness 合规性
+ 规则扫描的文件数

```
{"Dataset.*.FileFreshness.Compliance":1,"Dataset.*.FileCount":1} 
```

 异常检测不考虑这些指标。

 **检查文件新鲜度** 

 以下规则可确保 tickets.parquet 是在过去 24 小时内创建的。

```
FileFreshness "s3://amzn-s3-demo-bucket/artifacts/file/tickets/tickets.parquet" > (now() - 24 hours)
```

 **检查文件夹新鲜度** 

 如果文件夹中的所有文件都是在过去 24 小时内创建或修改的，则以下规则通过。

```
FileFreshness "s3://bucket/" >= (now() -1 days)
FileFreshness "s3://amzn-s3-demo-bucket/artifacts/file/tickets/" >= (now() - 24 hours)
```

 **使用阈值检查文件夹或文件新鲜度** 

 如果“tickets”文件夹中有 10% 的文件是在过去 10 天内创建或修改的，则以下规则通过。

```
FileFreshness "s3://amzn-s3-demo-bucket/artifacts/file/tickets/" < (now() - 10 days) with threshold > 0.1
```

 **检查特定日期的文件或文件夹** 

 您可以检查特定日期的文件新鲜度。

```
FileFreshness "s3://amzn-s3-demo-bucket/artifacts/file/tickets/" > "2020-01-01"
FileFreshness "s3://amzn-s3-demo-bucket/artifacts/file/tickets/" between "2023-01-01" and "2024-01-01"
```

 **随时间检查文件或文件夹** 

 您可以使用 FileFreshness 来确保文件在特定时间送达。

```
FileFreshness "s3://amzn-s3-demo-bucket/artifacts/file/tickets/" between now() and (now() - 45 minutes)
FileFreshness "s3://amzn-s3-demo-bucket/artifacts/file/tickets/" between "9:30 AM" and "9:30 PM"
FileFreshness "s3://amzn-s3-demo-bucket/artifacts/file/tickets/" > (now() - 10 minutes)
FileFreshness "s3://amzn-s3-demo-bucket/artifacts/file/tickets/" > now()
FileFreshness "s3://amzn-s3-demo-bucket/artifacts/file/tickets/" between (now() - 2 hours) and (now() + 15 minutes)
FileFreshness "s3://amzn-s3-demo-bucket/artifacts/file/tickets/" between (now() - 3 days) and (now() + 15 minutes)
FileFreshness "s3://amzn-s3-demo-bucket/artifacts/file/tickets/" between "2001-02-07" and (now() + 15 minutes)
FileFreshness "s3://amzn-s3-demo-bucket/artifacts/file/tickets/" > "21:45""
FileFreshness "s3://amzn-s3-demo-bucket/artifacts/file/tickets/" > "2024-01-01"
FileFreshness "s3://amzn-s3-demo-bucket/artifacts/file/tickets/" between "02:30" and "04:30"
FileFreshness "s3://amzn-s3-demo-bucket/artifacts/file/tickets/" between "9:30 AM" and "22:15"
```

 重要注意事项：
+  FileFreshness 可以使用天、小时和分钟单位来评估文件 
+  对于时间，它支持上午/下午和 24 小时 
+  除非指定了替换，否则时间以 UTC 计算 
+  日期以 UTC 时间 00:00 计算 

 基于时间的 FileFreshness 的工作方式如下所示：

```
FileFreshness "s3://amzn-s3-demo-bucket/artifacts/file/tickets/" > "21:45"
```
+  首先，将时间“21:45”与 UTC 格式的今天日期结合，以创建日期时间字段 
+  接下来，将日期时间转换为您指定的时区 
+  最后，对规则进行评估 

 **可选的基于文件的规则标签：**

 通过标签，您可以控制规则行为。

 **recentFiles** 

 此标签通过将最新文件放在最前面，来限制处理的文件数。

```
FileFreshness "s3://amzn-s3-demo-bucket/" between (now() - 100 minutes) and (now() + 10 minutes) with recentFiles = 1
```

 **uriRegex** 

**注意**  
 `uriRegex` 标签在 AWS Glue 5.0 及更高版本中可用。

 此标签通过对文件路径应用正则表达式模式来筛选文件。只有路径与该模式匹配的文件才会被处理。您也可以使用负向先行断言来排除与某个模式匹配的文件。

```
# Match only files with a .csv extension
FileFreshness "s3://amzn-s3-demo-bucket/" > (now() - 24 hours) with uriRegex = "\.csv$"
# Match Parquet files that contain "orders_" in the path
FileFreshness "s3://amzn-s3-demo-bucket/" > (now() - 24 hours) with uriRegex = ".*orders_.*\.parquet"
# Exclude files ending in .tmp using a negative lookahead
FileFreshness "s3://amzn-s3-demo-bucket/" > (now() - 24 hours) with uriRegex = "(?!.*\.tmp$).*"
```

 **filterOrder** 

**注意**  
 `filterOrder` 标签在 AWS Glue 5.0 及更高版本中可用。

 当您同时使用多个筛选标签（例如 `recentFiles` 和 `uriRegex`）时，`filterOrder` 标签用来控制这些标签的应用顺序。默认顺序为首先应用 `recentFiles`，然后应用 `uriRegex`。

```
FileFreshness "s3://amzn-s3-demo-bucket/" > (now() - 24 hours) with recentFiles = 1 with uriRegex = "inventory_" with filterOrder = ["uriRegex","recentFiles"]
```

 上例会首先应用 `uriRegex` 筛选条件来仅选择与 “inventory\$1” 匹配的文件，然后使用 `recentFiles = 1` 从筛选后到结果集中获取最新的文件。如果未设置 `filterOrder`，则默认行为将是首先获取最新的单个文件，然后再应用正则表达式。如果最新的文件与该模式不匹配，则可能导致没有任何文件匹配。

**注意**  
 `filterOrder` 列表中的所有值必须引用同一规则中同时存在的其他筛选条件标签（`recentFiles` 或 `uriRegex`）。非筛选条件标签（例如 `timeZone` 或 `failFast`）在 `filterOrder` 中无效。

 **failFast** 

 如果设置为 `"true"`，则该规则会在遇到第一个不符合新鲜度条件的文件时立即返回失败，而不会评估所有文件并计算合规率。

```
FileFreshness "s3://amzn-s3-demo-bucket/" > (now() - 24 hours) with failFast = "true"
```

 **timeZone** 

 可接受的时区覆盖，有关支持的时区，请参阅[允许的时区](https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html)。

```
FileFreshness "s3://path/" > "21:45" with timeZone = "America/New_York"
```

```
FileFreshness "s3://path/" > "21:45" with timeZone = "America/Chicago"
```

```
FileFreshness "s3://path/" > "21:45" with timeZone = "Europe/Paris"
```

```
FileFreshness "s3://path/" > "21:45" with timeZone = "Asia/Shanghai"
```

```
FileFreshness "s3://path/" > "21:45" with timeZone = "Australia/Darwin"
```

 **直接从数据帧推断文件名** 

 您不必总是提供文件路径。例如，当您在 AWS Glue Data Catalog 中编写规则时，可能很难找到目录表使用的文件夹。AWSGlue 数据质量自动监测功能可以找到用于填充数据帧的特定文件夹或文件，并可检测其是否为最新。

**注意**  
 仅当文件成功读取到 DynamicFrame 或 DataFrame 时，此功能才起作用。

```
FileFreshness > (now() - 24 hours)
```

 此规则将查找用于填充动态帧或数据帧的文件夹路径或文件。这适用于 Amazon S3 路径或基于 Amazon S3 的 AWS Glue Data Catalog 表。有几个注意事项：

1.  在 AWS Glue ETL 中，您必须在 Amazon S3 或 AWS Glue Data Catalog 转换之后立即进行 **EvaluateDataQuality** 转换。  
![\[屏幕截图显示连接到 Amazon S3 节点的评估数据质量节点。\]](http://docs.aws.amazon.com/zh_cn/glue/latest/dg/images/data-quality-file-freshness.png)

1.  此规则在 AWS Glue 交互式会话中不起作用。

 如果您在上述两种情况下都尝试这样做，或者当 AWS Glue 找不到文件时，则 AWS Glue 会抛出以下错误：`“Unable to parse file path from DataFrame”`