

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

# 在步驟之間傳遞資料
<a name="build-and-manage-propertyfile"></a>

使用 Amazon SageMaker Pipelines 建置管道時，您可能需要將資料從某個步驟傳遞到下一個步驟。例如，您可能想要使用訓練步驟產生的模型成品，做為模型評估或部署步驟的輸入。您可以使用此功能來建立相互依賴的管道步驟，並建置 ML 工作流程。

當您需要從管道步驟的輸出擷取資訊時，您可以使用 `JsonGet`。`JsonGet` 可協助您從 Amazon S3 或屬性檔案中擷取資訊。下列各節說明您可以用來搭配 `JsonGet` 擷取步驟輸出的方法。

## 使用 Amazon S3 在步驟之間傳遞資料
<a name="build-and-manage-propertyfile-s3"></a>

您可以在 `JsonGet` 中使用 `ConditionStep`，直接從 Amazon S3 擷取 JSON 輸出。Amazon S3 URI 可以是包含基本字串、管道執行變數或管道參數的 `Std:Join` 函式。以下範例展示如何在 `ConditionStep` 中使用 `JsonGet`。

```
# Example json file in s3 bucket generated by a processing_step
{
   "Output": [5, 10]
}

cond_lte = ConditionLessThanOrEqualTo(
    left=JsonGet(
        step_name="{{<step-name>}}",
        s3_uri="{{<s3-path-to-json>}}",
        json_path="Output[1]"
    ),
    right=6.0
)
```

如果您在條件步驟中使用 `JsonGet` 搭配 Amazon S3 路徑，則必須在條件步驟與產生 JSON 輸出的步驟之間明確新增相依性。在下列範例中，條件步驟是透過處理步驟上的相依性建立的：

```
cond_step = ConditionStep(
        name="{{<step-name>}}",
        conditions=[cond_lte],
        if_steps=[fail_step],
        else_steps=[register_model_step],
        depends_on=[processing_step],
)
```

## 使用屬性檔案在步驟之間傳遞資料
<a name="build-and-manage-propertyfile-property"></a>

使用屬性檔案來存放處理步驟輸出中的資訊。這在分析處理步驟的結果以決定如何執行條件步驟時特別有用。`JsonGet` 函式會處理屬性檔案，並可讓您使用 JsonPath 表示法來查詢屬性 JSON 檔案。如需有關 JsonPath 符號的詳細資訊，請參閱 [JSONPath 回購](https://github.com/json-path/JsonPath)。

若要存放屬性檔案以供日後使用，您必須先建立使用下列格式的 `PropertyFile` 執行個體。`path` 參數是儲存屬性檔案的 JSON 檔案的名稱。所有 `output_name` 都必須符合您在處理步驟中定義的 `ProcessingOutput` 之 `output_name`。這可讓屬性檔案在步驟中擷取 `ProcessingOutput`。

```
from sagemaker.workflow.properties import PropertyFile

{{<property_file_instance>}} = PropertyFile(
    name="{{<property_file_name>}}",
    output_name="{{<processingoutput_output_name>}}",
    path="{{<path_to_json_file>}}"
)
```

建立 `ProcessingStep` 執行個體時，請新增 `property_files` 參數以列出 Amazon SageMaker Pipelines 服務必須為其編製索引的所有參數檔案。這將儲存屬性檔案以供日後使用。

```
property_files=[{{<property_file_instance>}}]
```

若要在條件步驟中使用屬性檔案，請將 `property_file` 新增至您傳遞至條件步驟的條件 (如下列範例所示)，以使用 `json_path` 參數查詢所需屬性的 JSON 檔案。

```
cond_lte = ConditionLessThanOrEqualTo(
    left=JsonGet(
        step_name=step_eval.name,
        property_file={{<property_file_instance>}},
        json_path="mse"
    ),
    right=6.0
)
```

如需更深入的範例，請參閱 [Amazon SageMaker Python SDK](https://sagemaker.readthedocs.io/en/stable) 中的 [Property File](https://sagemaker.readthedocs.io/en/stable/amazon_sagemaker_model_building_pipeline.html#property-file)。**