本文介紹如何使用Go SDK讀取FeatureStore線上資料來源的資料(離線特徵、即時特徵、序列特徵等)。
前提條件
已建立FeatureStore專案(Project)、特徵實體(FeatureEntity)、特徵視圖(FeatureView)和模型特徵(ModelFeature),並完成資料同步操作。具體操作,請參見配置FeatureStore專案。
已擷取阿里雲賬戶的AccessKey ID和AccessKey Secret。具體操作,請參見建立AccessKey。
建議使用本地配置環境變數的方式儲存AccessKey ID和AccessKey Secret。具體操作,請參見配置環境變數。
安裝FeatureStore Go SDK
執行如下命令安裝FeatureStore Go SDK。
go get github.com/aliyun/aliyun-pai-featurestore-go-sdk/v2
初始化FeatureStore用戶端
介面
//初始化``FeatureStoreClient``執行個體。
//regionId是執行個體所在地區
//accessKeyId是訪問Table Store服務的AccessKeyID,通過官方網站申請或通過管理員擷取。
//accessKeySecret是訪問Table Store服務的AccessKeySecret,通過官方網站申請或通過管理員擷取。
//projectName是於控制台建立的FeatureStore Project名稱
func NewFeatureStoreClient(regionId, accessKeyId, accessKeySecret, projectName string, opts ...ClientOption) (*FeatureStoreClient, error)
由於SDK是直連線上資料來源,用戶端需要在VPC環境運行。例如Hologres和GraphCompute需要在指定的VPC才能串連。
樣本
accessId := os.Getenv("AccessId")
accessKey := os.Getenv("AccessKey")
client, err := featurestore.NewFeatureStoreClient("cn-beijing", accessId, accessKey, "project_name")
擷取FeatureView的特徵資料
介面
//根據joinId的值以及選取的特徵名和特徵別名擷取FeatureView下具體特徵資料
GetOnlineFeatures(joinIds []interface{}, features []string, alias map[string]string) ([]map[string]interface{}, error)
參數說明
參數 | 說明 | |
joinIds | 需要擷取到特徵的joinId(主鍵)的值。 | |
features | 指定需要擷取的特徵的名稱。 | |
alias | 特徵別名配置。需要注意此配置對序列特徵FeatureView不生效。 |
樣本
// get project by name
project, err := client.GetProject("project_name")
if err != nil {
// t.Fatal(err)
}
// get featureview by name
user_feature_view := project.GetFeatureView("feature_view_name")
if user_feature_view == nil {
// t.Fatal("feature view not exist")
}
// get online features
features, err := user_feature_view.GetOnlineFeatures([]interface{}{"key1", "key2"}, []string{"*"}, nil)
離線/即時FeatureView特徵資料樣本結果
[ { "city":"合肥市", "follow_cnt":1, "gender":"male", "user_id":"100043186" }, { "city":"", "follow_cnt":5, "gender":"male", "user_id":"100060369" } ]
序列特徵FeatureView特徵資料樣本結果
假設序列特徵讀取配置如下
離線序列特徵欄位
事件名稱
序列長度
線上序列特徵名稱
click_seq_50_seq
click
50
click_seq_50_seq
expr_seq_100_seq
expr
100
expr_seq_100
返回樣本結果
[ { "click_seq_50_seq": "216751275;228787053;220852269;242884721", "click_seq_50_seq__event": "click;click;click;click", "click_seq_50_seq__event_time": "1699128398;1699128398;1699118623;1699118623", "click_seq_50_seq__item_id": "216751275;228787053;220852269;242884721", "click_seq_50_seq__playtime": "65.40;72.06;104.69;62.74", "click_seq_50_seq__ts": "389018;389018;398793;398793", "expr_seq_100": "207474427;216751275;228787053;247136848;270584471;299485479;220852269;242884721;245999124;265863707", "expr_seq_100__event": "expr;expr;expr;expr;expr;expr;expr;expr;expr;expr", "expr_seq_100__event_time": "1699128398;1699128398;1699128398;1699128398;1699128398;1699128398;1699118623;1699118623;1699118623;1699118623", "expr_seq_100__item_id": "207474427;216751275;228787053;247136848;270584471;299485479;220852269;242884721;245999124;265863707", "expr_seq_100__playtime": "0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00", "expr_seq_100__ts": "389018;389018;389018;389018;389018;389018;398793;398793;398793;398793", "user_id": "186569075" }, { "click_seq_50_seq": "201741544;236327912;293320498", "click_seq_50_seq__event": "click;click;click", "click_seq_50_seq__event_time": "1699178245;1699178245;1699178245", "click_seq_50_seq__item_id": "201741544;236327912;293320498", "click_seq_50_seq__playtime": "97.41;70.32;135.21", "click_seq_50_seq__ts": "339171;339171;339171", "expr_seq_100": "201741544;224940066;236327912;240253906;247562151;293320498", "expr_seq_100__event": "expr;expr;expr;expr;expr;expr", "expr_seq_100__event_time": "1699178245;1699178245;1699178245;1699178245;1699178245;1699178245", "expr_seq_100__item_id": "201741544;224940066;236327912;240253906;247562151;293320498", "expr_seq_100__playtime": "0.00;0.00;0.00;0.00;0.00;0.00", "expr_seq_100__ts": "339171;339171;339171;339171;339171;339171", "user_id": "186569870" } ]
擷取ModelFeature裡的特徵資料
介面
//根據joinId的值以及特徵實體(FeatureEntity)擷取ModelFeature裡的特徵資料
GetOnlineFeatures(joinIds map[string][]interface{}) ([]map[string]interface{}, error)
//根據joinId的值擷取ModelFeature裡某個指定的FeatureEntity對應的特徵資料
GetOnlineFeaturesWithEntity(joinIds map[string][]interface{}, featureEntityName string) ([]map[string]interface{}, error)
參數說明
參數 | 說明 | |
joinIds | joinIds的map集合。key為JoinId的名稱,values為JoinId的值。 | |
featureEntityName | 指定特定的特徵實體名稱。 |
樣本
ModelFeature可以關聯多個FeatureEntity,可以設定多個join_id,然後特徵統一返回。
樣本中有兩個join_id
、user_id
和item_id
。擷取特徵的時候需要設定相同的ID數量。
// get project by name
project, err := client.GetProject("fs_test_ots")
if err != nil {
// t.Fatal(err)
}
// get ModelFeature by name
model_feature := project.GetModelFeature("rank")
if model_feature == nil {
// t.Fatal("model feature not exist")
}
// get online features
features, err := model_feature.GetOnlineFeatures(map[string][]interface{}{"user_id": {"100000676", "100004208"}, "item_id":{"238038872", "264025480"}} )
也可以指定某個FeatureEntity,將FeatureEntity對應的特徵一起返回。
features, err := model_feature.GetOnlineFeaturesWithEntity(map[string][]interface{}{"user_id": {"100000676", "100004208"}}, "user" )
擷取ModelFeature特徵資料樣本(不包含序列特徵FeatureView)
[ { "age":26, "city":"瀋陽市", "gender":"male", "user_id":"100000676" }, { "age":23, "city":"西安市", "gender":"male", "user_id":"100004208" } ]
擷取ModelFeature特徵資料樣本(包含序列特徵FeatureView)
註冊ModelFeature時可以選擇序列特徵FeatureView裡註冊的離線序列特徵欄位,之後在FeatureStore Go SDK中便可以擷取到對應的線上序列特徵名稱。
序列特徵對應FeatureEntity一般為user,樣本中有兩個
join_id
、user_id
和item_id
。擷取特徵的時候需要設定相同的ID數量。假設序列特徵讀取配置如下
離線序列特徵欄位
事件名稱
序列長度
線上序列特徵名稱
click_seq_50_seq
click
50
click_seq_50_seq
返回樣本結果
[ { "age": 51, "author": 147848300, "category": "7", "city": "", "click_count": 0, "click_seq_50_seq": "216751275;228787053;220852269;242884721", "click_seq_50_seq__event": "click;click;click;click", "click_seq_50_seq__event_time": "1699128398;1699128398;1699118623;1699118623", "click_seq_50_seq__item_id": "216751275;228787053;220852269;242884721", "click_seq_50_seq__playtime": "65.40;72.06;104.69;62.74", "click_seq_50_seq__ts": "391447;391447;401222;401222", "duration": 48, "follow_cnt": 2, "follower_cnt": 0, "gender": "female", "item_cnt": 0, "item_id": 299485479, "praise_count": 2, "pub_time": 1697885713, "register_time": 1696582012, "tags": "0", "title": "#健身打卡", "user_id": "186569075" }, { "age": 28, "author": 119734983, "category": "18", "city": "", "click_count": 0, "click_seq_50_seq": "201741544;236327912;293320498", "click_seq_50_seq__event": "click;click;click", "click_seq_50_seq__event_time": "1699178245;1699178245;1699178245", "click_seq_50_seq__item_id": "201741544;236327912;293320498", "click_seq_50_seq__playtime": "97.41;70.32;135.21", "click_seq_50_seq__ts": "341600;341600;341600", "duration": 15, "follow_cnt": 0, "follower_cnt": 2, "gender": "male", "item_cnt": 0, "item_id": 207474427, "praise_count": 79, "pub_time": 1697731285, "register_time": 1699135393, "tags": "1", "title": "#成語故事", "user_id": "186569870" } ]