

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

# 以排程和事件為基礎執行特徵處理器管道
<a name="feature-store-feature-processor-schedule-pipeline"></a>

Amazon SageMaker Feature Store Feature Processing 管道執行可以設定為根據預先設定的排程或由於另一個 AWS 服務事件以自動和非同步方式啟動。例如，您可以將特徵處理管道排定在每月的第一天執行，或將兩條管道串連在一起，待來源管道執行完成後自動執行目標管道。

**Topics**
+ [基於排程執行](#feature-store-feature-processor-schedule-pipeline-schedule-based)
+ [事件型執行](#feature-store-feature-processor-schedule-pipeline-event-based)

## 基於排程執行
<a name="feature-store-feature-processor-schedule-pipeline-schedule-based"></a>

特徵處理器 SDK 提供 [https://sagemaker.readthedocs.io/en/stable/api/prep_data/feature_store.html#sagemaker.feature_store.feature_processor.schedule](https://sagemaker.readthedocs.io/en/stable/api/prep_data/feature_store.html#sagemaker.feature_store.feature_processor.schedule) API，可透過 Amazon EventBridge 排程器整合定期執行特徵處理器管道。您可以使用 [https://docs.aws.amazon.com/scheduler/latest/APIReference/API_CreateSchedule.html#scheduler-CreateSchedule-request-ScheduleExpression](https://docs.aws.amazon.com/scheduler/latest/APIReference/API_CreateSchedule.html#scheduler-CreateSchedule-request-ScheduleExpression) 參數 (與 Amazon EventBridge 支援的運算式相同)，使用 `at`、`rate`、或 `cron` 運算式來指定排程。排程 API 在語意上是更新或新增作業，如果排程存在，則更新排程；如果排程不存在，則建立排程。如需有關 EventBridge 運算式及範例的更多資訊，請參閱 EventBridge 排程器使用者指南中的 [EventBridge 排程器上的排程類型](https://docs.aws.amazon.com/scheduler/latest/UserGuide/schedule-types.html)。

以下範例使用了特徵處理器 API [https://sagemaker.readthedocs.io/en/stable/api/prep_data/feature_store.html#sagemaker.feature_store.feature_processor.schedule](https://sagemaker.readthedocs.io/en/stable/api/prep_data/feature_store.html#sagemaker.feature_store.feature_processor.schedule)，使用 `at`、`rate` 和 `cron` 運算式。

```
from sagemaker.feature_store.feature_processor import schedule
pipeline_name='feature-processor-pipeline'

event_bridge_schedule_arn = schedule(
    pipeline_name=pipeline_name, 
    schedule_expression="at(2020-11-30T00:00:00)"
)

event_bridge_schedule_arn = schedule(
    pipeline_name=pipeline_name, 
    schedule_expression="rate(24 hours)"
)

event_bridge_schedule_arn = schedule(
    pipeline_name=pipeline_name, 
    schedule_expression="cron(0 0-23/1 ? * * 2023-2024)"
)
```

`schedule` API 中日期和時間輸入的預設時區為 UTC。如需有關 EventBridge 排程器排程運算式的更多相關資訊，請參閱 EventBridge 排程器 API 參考文件中的 [https://docs.aws.amazon.com/scheduler/latest/APIReference/API_CreateSchedule.html#scheduler-CreateSchedule-request-ScheduleExpression](https://docs.aws.amazon.com/scheduler/latest/APIReference/API_CreateSchedule.html#scheduler-CreateSchedule-request-ScheduleExpression)。

排程的特徵處理器管道執行為您的轉換函式提供排程執行時間，可用作等冪性標記，或基於日期範圍之輸入的固定參考點。若要停用 (即暫停) 或重新啟用排程，請在 [https://sagemaker.readthedocs.io/en/stable/api/prep_data/feature_store.html#sagemaker.feature_store.feature_processor.schedule](https://sagemaker.readthedocs.io/en/stable/api/prep_data/feature_store.html#sagemaker.feature_store.feature_processor.schedule) API 的 `state` 參數中分別使用 `‘DISABLED’` 或 `‘ENABLED’`。

如需有關特徵處理器的資訊，請參閱[特徵處理器 SDK 資料來源](feature-store-feature-processor-data-sources-sdk.md)。

## 事件型執行
<a name="feature-store-feature-processor-schedule-pipeline-event-based"></a>

特徵處理管道可以設定為當 AWS 事件發生時自動執行。特徵處理 SDK 提供一個 [https://sagemaker.readthedocs.io/en/stable/api/prep_data/feature_store.html#sagemaker.feature_store.feature_processor.put_trigger](https://sagemaker.readthedocs.io/en/stable/api/prep_data/feature_store.html#sagemaker.feature_store.feature_processor.put_trigger) 函式，用於接受來源事件和目標管道的清單。來源事件必須是指定了管道與[執行狀態](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribePipelineExecution.html#sagemaker-DescribePipelineExecution-response-PipelineExecutionStatus)事件之 [https://sagemaker.readthedocs.io/en/stable/api/prep_data/feature_store.html#sagemaker.feature_store.feature_processor.FeatureProcessorPipelineEvent](https://sagemaker.readthedocs.io/en/stable/api/prep_data/feature_store.html#sagemaker.feature_store.feature_processor.FeatureProcessorPipelineEvent) 的執行個體。

`put_trigger` 函數會設定 Amazon EventBridge 規則和目標來路由事件，並可讓您指定 EventBridge 事件模式來回應任何 AWS 事件。如需有關這些概念的資訊，請參閱 Amazon EventBridge [規則](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-rules.html)、[目標](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-targets.html)和[事件模式](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns.html)。

可以啟用或停用觸發程式。EventBridge 將使用 `put_trigger` API 的 `role_arn` 參數中提供的角色來啟動目標管道執行。如果在 Amazon SageMaker Studio Classic 或筆記本環境中使用 SDK，則依預設會使用執行角色。如需有關如何取得執行角色的資訊，請參閱[取得您的執行角色](sagemaker-roles.md#sagemaker-roles-get-execution-role)。

下面的範例會設定以下內容：
+ 使用 `to_pipeline` API 的 SageMaker AI 管道，該管道接受您的目標管道名稱 (`target-pipeline`) 和轉換函式 (`transform`)。如需有關特徵處理器和轉換函式的資訊，請參閱[特徵處理器 SDK 資料來源](feature-store-feature-processor-data-sources-sdk.md)。
+ 使用 `put_trigger` API 的觸發程式，該觸發程式接受事件的 `FeatureProcessorPipelineEvent` 和您的目標管道名稱 (`target-pipeline`)。

  `FeatureProcessorPipelineEvent` 定義來源管道 (`source-pipeline`) 狀態變成 `Succeeded` 時的觸發程式。如需有關特徵處理器管道事件函式的資訊，請參閱 Feature Store 閱讀文件中的 [https://sagemaker.readthedocs.io/en/stable/api/prep_data/feature_store.html#sagemaker.feature_store.feature_processor.FeatureProcessorPipelineEvent](https://sagemaker.readthedocs.io/en/stable/api/prep_data/feature_store.html#sagemaker.feature_store.feature_processor.FeatureProcessorPipelineEvent)。

```
from sagemaker.feature_store.feature_processor import put_trigger, to_pipeline, FeatureProcessorPipelineEvent

to_pipeline(pipeline_name="target-pipeline", step=transform)

put_trigger(
    source_pipeline_events=[
        FeatureProcessorPipelineEvent(
            pipeline_name="source-pipeline",
            status=["Succeeded"]
        )
    ],
    target_pipeline="target-pipeline"
)
```

有關使用事件型觸發程式為特徵處理器管道建立持續執行和自動重試的範例，請參閱[使用事件型觸發程式連續執行和自動重試](feature-store-feature-processor-examples.md#feature-store-feature-processor-examples-continuous-execution-automatic-retries)。

如需使用事件型觸發程式建立連續*串流*和自動重試的範例，請參閱[串流自訂資料來源範例](feature-store-feature-processor-data-sources-custom-examples.md#feature-store-feature-processor-data-sources-custom-examples-streaming)。