This topic describes how to use the FeatureStore SDK for Go to read data from online data stores, including offline features, real-time features, and sequence features.
Prerequisites
A FeatureStore project, feature entities, feature views, and model features are created. Data is synchronized between the online and offline stores. For more information, see Configure FeatureStore projects.
An AccessKey ID and an AccessKey secret are obtained. For more information, see Create an AccessKey pair.
We recommend that you use environment variables to store your AccessKey ID and AccessKey secret. For more information, see Step 2: Configure environment variables.
Install the FeatureStore SDK for Go
Run the following command to install the FeatureStore SDK for Go:
go get github.com/aliyun/aliyun-pai-featurestore-go-sdk/v2
Initialize the FeatureStore client
API
// Initialize the FeatureStore client.
// regionId specifies the region where the instance resides.
// accessKeyId specifies the AccessKey ID that is used to access the storage services. To obtain an AccessKey ID, visit the official website of Alibaba Cloud or contact an administrator.
// accessKeySecret specifies the AccessKey secret that is used to access the storage services. To obtain an AccessKey secret, visit the official website of Alibaba Cloud or contact an administrator.
// projectName specifies the name of the FeatureStore project that you created in the Platform for AI (PAI) console.
func NewFeatureStoreClient(regionId, accessKeyId, accessKeySecret, projectName string, opts ...ClientOption) (*FeatureStoreClient, error)
The FeatureStore client must run in a virtual private cloud (VPC) to allow the SDK for Go to directly connect to the online stores. For example, the SDK for Go can access a Hologres instance or a GraphCompute instance only over a specific VPC.
Sample code
accessId := os.Getenv("AccessId")
accessKey := os.Getenv("AccessKey")
client, err := featurestore.NewFeatureStoreClient("cn-beijing", accessId, accessKey, "project_name")
Retrieve feature data from a feature view
API
// Retrieve feature data from a feature view based on the join ID, feature name, and feature alias.
GetOnlineFeatures(joinIds []interface{}, features []string, alias map[string]string) ([]map[string]interface{}, error)
Parameters
Parameter | Description | |
joinIds | Specifies the join IDs (primary keys) of the features that you want to retrieve. | |
features | Specifies the names of the features that you want to retrieve. | |
alias | Specifies the aliases of the features that you want to retrieve. This parameter does not apply to a sequence feature view. |
Sample code
// 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)
Success response: Retrieve data from an offline or real-time feature view
[ { "city":"Hefei", "follow_cnt":1, "gender":"male", "user_id":"100043186" }, { "city":"", "follow_cnt":5, "gender":"male", "user_id":"100060369" } ]
Success response: Retrieve data from a sequence feature view
The following table describes the configuration of sequence features used in this example.
Offline sequence feature field
Event name
Sequence length
Online sequence feature name
click_seq_50_seq
click
50
click_seq_50_seq
expr_seq_100_seq
expr
100
expr_seq_100
Sample output
[ { "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" } ]
Retrieve feature data from model features
API
// Retrieve feature data from model features based on the join ID and feature entity.
GetOnlineFeatures(joinIds map[string][]interface{}) ([]map[string]interface{}, error)
// Retrieve feature data about a specific entity from model features based on the join ID.
GetOnlineFeaturesWithEntity(joinIds map[string][]interface{}, featureEntityName string) ([]map[string]interface{}, error)
Parameters
Parameter | Description | |
joinIds | A collection of key-value pairs of join IDs. The key is the name of the join ID and the value is the value of the join ID. | |
featureEntityName | Specifies the name of the feature entity whose feature data you want to retrieve. |
Sample code
Each model feature can be associated with multiple feature entities. You can specify multiple join IDs to retrieve the corresponding features.
The following sample code specifies two join IDs
: user_id
and item_id
. You must specify all join IDs that you configured.
// 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"}} )
You can also specify a feature entity to retrieve its features.
features, err := model_feature.GetOnlineFeaturesWithEntity(map[string][]interface{}{"user_id": {"100000676", "100004208"}}, "user" )
Success response: Retrieve data from model features (excluding sequence features)
[ { "age":26, "city":"Shenyang", "gender":"male", "user_id":"100000676" }, { "age":23, "city":"Xi'an", "gender":"male", "user_id":"100004208" } ]
Success response: Retrieve data from model features (including sequence features)
When you register model features, you can specify offline sequence features that you want to use for model training. The specified offline sequence features are synchronized to the online data store. Then, you can obtain the corresponding online sequence features by using the FeatureStore SDK for Go.
In general, the entity of sequence features is the user. The following sample code specifies two
join IDs
:user_id
anditem_id
. You must specify all join IDs that you configured.The following table describes the configuration of sequence features used in this example.
Offline sequence feature field
Event name
Sequence length
Online sequence feature name
click_seq_50_seq
click
50
click_seq_50_seq
Sample output
[ { "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": "#Workout tracking", "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": "#Idiom story", "user_id": "186569870" } ]