

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 向特征添加可搜索的元数据
<a name="feature-store-add-metadata"></a>

在 Amazon F SageMaker eature Store 中，您可以搜索所有功能。要使特征更容易被发现，您可以向其添加元数据。可以添加以下类型的元数据：
+ 描述 - 特征的可搜索描述。
+ 参数 - 可搜索的键值对。

描述最多可包含 255 个字符。对于参数，您必须在搜索中指定键值对。最多可以添加 25 个参数。

要更新功能的元数据，可以使用管理控制台或 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateFeatureMetadata.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateFeatureMetadata.html) 操作。

## 如何在功能中添加可搜索的元数据
<a name="feature-store-add-metadata-how-to"></a>

您可以使用控制台或 Amazon Feature Store API 向您的 SageMaker 功能添加可搜索的元数据。通过管理控制台使用特征存放区的说明取决于您是否已启用 [亚马逊 SageMaker Studio](studio-updated.md) 或 [亚马逊 SageMaker Studio 经典版](studio.md) 作为默认体验。

### 如果 Studio 是您的默认体验（管理控制台），则为功能添加可搜索的元数据
<a name="feature-store-add-metadata-how-to-with-studio-updated"></a>

1. 按照 [启动亚马逊 SageMaker Studio](studio-updated-launch.md) 中的说明打开 Studio 管理控制台。

1. 在左侧导航窗格中选择**数据**，展开下拉列表。

1. 从下拉列表中，选择 **Feature Store**。

1. （可选）要查看您的功能，请选择**我的帐户**。要查看共享功能，请选择**跨账户**。

1. 要查看特征组，请在**功能目录**选项卡下选择**我的账户**。

1. 在**功能目录**选项卡下，选择**跨账户**，查看他人让您可以发现的特征组。在**创建者**下，可以查看特征组的资源所有者账户 ID。

1. 可以在**搜索**下拉列表中搜索特征。
   + （可选）要筛选搜索，请选择**搜索**下拉列表旁边的筛选图标。可以使用筛选器在搜索结果中指定参数或日期范围。如果要搜索一个参数，请同时指定其键和值。为了更轻松地查找功能，您可以指定时间范围或取消选择不想查询的列。
   + 对于共享资源，只有拥有资源所有者账户授予的适当访问权限，才能编辑特征组元数据或功能定义。仅拥有可发现性权限并不能编辑元数据或功能定义。有关授予访问权限的更多信息，请参阅 [启用跨账户访问](feature-store-cross-account-access.md)。

1. 选择您的特征。

1. 请选择 **Edit metadata**（编辑元数据）。

1. 在**描述**字段中，添加或更新描述。

1. 在**参数**字段的**参数**下，为参数指定键值对。

1. （可选）选择**添加新参数**以添加其他参数。

1. 选择**保存更改**。

1. 选择**确认**。

### 使用 Python SDK 为您的功能添加可搜索的元数据 (Boto3)
<a name="feature-store-add-metadata-how-to-with-sdk"></a>

本节中的代码使用 适用于 Python (Boto3) 的 AWS SDK 中的 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateFeatureMetadata.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateFeatureMetadata.html) 操作为您的功能添加可搜索的元数据，以满足不同场景的需求。有关提交查询的其他语言的信息，请参[阅 *Amazon SageMaker API 参考*中的另请参阅](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateFeatureMetadata.html#API_Search_SeeAlso)。

有关更多特征存放区示例和资源，请参阅 [Amazon SageMaker 功能商店资源](feature-store-resources.md)。

------
#### [ Add a list of parameters to a feature ]

要向特征添加参数列表，请为以下字段指定值：
+ `FeatureGroupName`
+ `Feature`
+ `Parameters`

以下示例代码使用 适用于 Python (Boto3) 的 AWS SDK 添加两个参数。

```
sagemaker_client.update_feature_metadata(
    FeatureGroupName={{"feature_group_name"}},
    FeatureName={{"feature-name"}},
    ParameterAdditions=[
        {"Key": {{"example-key-0"}}, "Value": {{"example-value-0"}}},
        {"Key": {{"example-key-1"}}, "Value": {{"example-value-1"}}},
    ]
)
```

------
#### [ Add a description to a feature ]

要向特征添加描述，请为以下字段指定值：
+ `FeatureGroupName`
+ `Feature`
+ `Description`

```
sagemaker_client.update_feature_metadata(
    FeatureGroupName={{"feature-group-name"}},
    FeatureName={{"feature-name"}},
    Description={{"description"}}
)
```

------
#### [ Remove parameters for a feature ]

要删除特征的所有参数，请执行以下操作。

为以下字段指定值：
+ `FeatureGroupName`
+ `Feature`

为要在 `ParameterRemovals` 下删除的参数指定键。

```
sagemaker_client.update_feature_metadata(
    FeatureGroupName={{"feature_group_name"}},
    FeatureName={{"feature-name"}},
        ParameterRemovals=[
        {"Key": {{"example-key-0"}}},
        {"Key": {{"example-key-1"}}},
    ]
)
```

------
#### [ Remove the description for a feature ]

要删除某项特征的描述，请执行以下操作。

为以下字段指定值：
+ `FeatureGroupName`
+ `Feature`

为 `Description` 指定一个空字符串。

```
sagemaker_client.update_feature_metadata(
    FeatureGroupName={{"feature-group-name"}},
    FeatureName={{"feature-name"}},
    Description=""
)
```

------

#### 代码示例
<a name="feature-store-add-metadata-python-sdk-example"></a>

更新某项特征的元数据后，您可以使用 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeFeatureMetadata.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeFeatureMetadata.html) 操作来查看所做的更新。

以下代码是一个使用 适用于 Python (Boto3) 的 AWS SDK的示例工作流。该示例代码执行以下操作：

1. 设置你的 SageMaker AI 环境。

1. 创建特征组。

1. 向该组添加特征。

1. 向特征添加元数据。

有关更多特征存放区示例和资源，请参阅 [Amazon SageMaker 功能商店资源](feature-store-resources.md)。

##### 步骤 1：设置
<a name="feature-store-add-metadata-step-1"></a>

要开始使用功能商店，请创建 SageMaker AI、boto3 和功能商店会话。然后设置要用于特征的 S3 存储桶。这是您的离线存储。以下代码使用 SageMaker AI 默认存储桶并为其添加自定义前缀。

**注意**  
您使用的角色必须附加以下托管策略：`AmazonS3FullAccess` 和 `AmazonSageMakerFeatureStoreAccess`。

```
# SageMaker Python SDK version 2.x is required
%pip install 'sagemaker>=2.0.0'
import sagemaker
import sys
```

```
import boto3
import pandas as pd
import numpy as np
import io
from sagemaker.session import Session
from sagemaker import get_execution_role
from botocore.exceptions import ClientError


prefix = 'sagemaker-featurestore-introduction'
role = get_execution_role()

sagemaker_session = sagemaker.Session()
region = sagemaker_session.boto_region_name
s3_bucket_name = sagemaker_session.default_bucket()
sagemaker_client = boto_session.client(service_name='sagemaker', region_name=region)
```

##### 步骤 2：创建特征组并添加特征
<a name="feature-store-add-metadata-step-2"></a>

以下代码是一个使用特征定义创建特征组的示例。

```
feature_group_name = "test-for-feature-metadata"
feature_definitions = [
    {"FeatureName": "feature-1", "FeatureType": "String"},
    {"FeatureName": "feature-2", "FeatureType": "String"},
    {"FeatureName": "feature-3", "FeatureType": "String"},
    {"FeatureName": "feature-4", "FeatureType": "String"},
    {"FeatureName": "feature-5", "FeatureType": "String"}
]
try:
    sagemaker_client.create_feature_group(
        FeatureGroupName=feature_group_name,
        RecordIdentifierFeatureName="feature-1",
        EventTimeFeatureName="feature-2",
        FeatureDefinitions=feature_definitions,
        OnlineStoreConfig={"EnableOnlineStore": True}
    )
except ClientError as e:
    if e.response["Error"]["Code"] == "ResourceInUse":
        pass
    else:
        raise e
```

##### 步骤 3：添加元数据
<a name="feature-store-add-metadata-step-3"></a>

添加元数据之前，请使用 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeFeatureGroup.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeFeatureGroup.html) 操作确保特征组的状态为 `Created`。

```
sagemaker_client.describe_feature_group(
        FeatureGroupName=feature_group_name
    )
```

为该特征添加描述。

```
sagemaker_client.update_feature_metadata(
    FeatureGroupName=feature_group_name,
    FeatureName="feature-1",
    Description="new description"
)
```

您可以使用 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeFeatureMetadata.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeFeatureMetadata.html) 操作查看是否成功更新了特征组的描述。

```
    sagemaker_client.describe_feature_metadata(
    FeatureGroupName=feature_group_name,
    FeatureName="feature-1"
)
```

也可以使用它向特征组添加参数。

```
sagemaker_client.update_feature_metadata(
    FeatureGroupName=feature_group_name,
    FeatureName="feature-1",
    ParameterAdditions=[
        {"Key": "team", "Value": "featurestore"},
        {"Key": "org", "Value": "sagemaker"},
    ]
)
```

您可以再次使用 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeFeatureMetadata.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeFeatureMetadata.html) 操作来查看是否已成功添加参数。

```
    sagemaker_client.describe_feature_metadata(
    FeatureGroupName=feature_group_name,
    FeatureName="feature-1"
)
```