You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS SageMaker Feature Group双记录标识符支持性及实现、查询方法问询

SageMaker Feature Store 支持多字段联合唯一记录吗?如何实现?

SageMaker Feature Store 支持通过构造复合记录标识符的方式实现多字段联合唯一的需求——因为原生的record_identifier_feature_name仅支持单个字段作为主键,所以需要将你的两个业务标识符拼接为一个唯一的复合字段,以此作为Feature Group的记录标识符,同时保留原字段作为普通特征。

创建带联合标识符的Feature Group

1. 构造复合标识符字段

将你的两个业务标识符(比如user_id和transaction_id)用唯一分隔符(如#)拼接成新字段(如user_transaction_id),确保每个拼接结果对应唯一的业务记录。

2. 定义特征与创建Feature Group

通过SDK(如boto3)调用CreateFeatureGroup API,将复合字段指定为RecordIdentifierFeatureName,同时包含原标识符字段和其他业务特征:

import boto3

sagemaker = boto3.client("sagemaker")

feature_group_name = "fraud-detection-joint-id-fg"

# 定义所有特征,包含复合标识符、原业务标识符及其他特征
feature_defs = [
    {"FeatureName": "user_transaction_id", "FeatureType": "String"},
    {"FeatureName": "user_id", "FeatureType": "String"},
    {"FeatureName": "transaction_id", "FeatureType": "String"},
    {"FeatureName": "transaction_amount", "FeatureType": "Integral"},
    {"FeatureName": "transaction_time", "FeatureType": "String"}
]

# 创建Feature Group
sagemaker.create_feature_group(
    FeatureGroupName=feature_group_name,
    RecordIdentifierFeatureName="user_transaction_id",
    EventTimeFeatureName="transaction_time",
    FeatureDefinitions=feature_defs,
    OnlineStoreConfig={"EnableOnlineStore": True},
    OfflineStoreConfig={
        "S3StorageConfig": {"S3Uri": "s3://your-bucket/featurestore/"},
        "EnableOfflineStore": True
    }
)

3. 写入数据

写入特征数据时,确保每条记录的user_transaction_id是user_id与transaction_id的唯一拼接值,比如user_id#transaction_id。

查询联合标识符对应的记录

在线存储查询

先拼接出复合标识符,再调用GetRecord API查询:

target_user_id = "U1001"
target_transaction_id = "T2002"
composite_id = f"{target_user_id}#{target_transaction_id}"

response = sagemaker.get_record(
    FeatureGroupName=feature_group_name,
    RecordIdentifierValueAsString=composite_id
)

离线存储查询

离线存储的数据会同步到S3,可通过Athena直接基于原两个业务标识符过滤查询:

SELECT * 
FROM "sagemaker_featurestore"."fraud-detection-joint-id-fg"
WHERE user_id = 'U1001' AND transaction_id = 'T2002'

内容的提问来源于stack exchange,提问作者FlyingPickle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 15:15:46