FeatureStore Go SDK
Overview
FeatureStore Go SDK is the Go client SDK for Platform for AI (PAI) FeatureStore. It provides high-performance feature data reading and writing.
Scenarios
Quickly retrieve feature data in online inference scenarios
Write and update feature data in real time
Read behavior data and concatenate sequence features
Retrieve model features in batches
Prerequisites
Before you use FeatureStore Go SDK, make sure that you have completed the following preparations:
* You have created a FeatureStore project (Project), feature entities (FeatureEntity), feature views (FeatureView), and model features (ModelFeature), and completed data synchronization. For instructions, see Configure a FeatureStore project.
* You have obtained the AccessKey ID and AccessKey secret of your Alibaba Cloud account. For instructions, see Create an AccessKey.
(Recommended) Store the AccessKey ID and AccessKey secret in local environment variables. For instructions, see Configure environment variables.
FeatureStore Go SDK supports zero-trust calls. For details, see Manage access credentials.
QuickStart
The FeatureStore Go SDK is available at: https://github.com/aliyun/aliyun-pai-featurestore-go-sdk
Run the following command to install the SDK:
go get github.com/aliyun/aliyun-pai-featurestore-go-sdk/v2
import (
"os"
featurestore "github.com/aliyun/aliyun-pai-featurestore-go-sdk/v2/featurestore"
)
accessId := os.Getenv("AccessId")
accessKey := os.Getenv("AccessKey")
regionId := "cn-hangzhou"
projectName := "fs_test_ots"
client, err := featurestore.NewFeatureStoreClient(regionId, accessId, accessKey, projectName)
If you read data from or write data to FeatureDB, you must configure the FeatureDB username and password:
username := os.Getenv("FEATUREDB_USERNAME")
password := os.Getenv("FEATUREDB_PASSWORD")
client, err := featurestore.NewFeatureStoreClient(
regionId, accessId, accessKey, projectName,
featurestore.WithFeatureDBLogin(username, password),
)
Parameters
Parameter | Type | Required | Description | Example |
regionId | String | Yes | The region ID. | cn-hangzhou |
accessId | String | No | The AccessKey ID of your Alibaba Cloud account. | Obtained from environment variables |
accessKey | String | No | The AccessKey secret of your Alibaba Cloud account. | Obtained from environment variables |
projectName | String | Yes | The name of the FeatureStore project. | my_project |
username | String | Required for FeatureDB | The FeatureDB username. Configure this parameter only when the online store is FeatureDB. | featuredb_user |
password | String | Required for FeatureDB | The FeatureDB password. Configure this parameter only when the online store is FeatureDB. | featuredb_password |
By default, the SDK connects directly to the online store (such as FeatureDB), and the client must run in a VPC to connect. For local debugging, you can use WithTestMode() to enable public network access. Do not enable it in the production environment.
// Use the public network address for local debugging
client, err := featurestore.NewFeatureStoreClient(
regionId, accessId, accessKey, projectName,
featurestore.WithTestMode(),
featurestore.WithFeatureDBLogin(username, password),
)
Optional configuration items
Configuration item | Purpose | Example |
WithFeatureDBLogin() | FeatureDB credentials | featurestore.WithFeatureDBLogin("admin", "pwd") |
WithTestMode() | Local debugging over the public network address | featurestore.WithTestMode() |
Read operations
Retrieve feature data from a FeatureView
Retrieves online feature data from a FeatureView. Supports reading offline features, real-time features, and sequence features.
API description
GetOnlineFeatures(joinIds []interface{}, features []string, alias map[string]string) ([]map[string]interface{}, error)
Parameters
Parameter | Type | Required | Description |
joinIds | []interface{} | Yes | The list of join_id values, which is used to query feature data. |
features | []string | No | The list of feature fields to return. |
alias | map[string]string | No | The alias mapping of feature fields. Aliases are displayed in the returned data. |
Examples
project, err := client.GetProject("fs_test_ots")
if err != nil {
log.Fatal(err)
}
userFeatureView := project.GetFeatureView("user_fea")
if userFeatureView == nil {
log.Fatal("feature view not exist")
}
features, err := userFeatureView.GetOnlineFeatures(
[]interface{}{"100043186", "100060369"},
[]string{"*"},
nil,
)
Sample result of offline or real-time FeatureView feature data
[ { "city":"Hefei", "follow_cnt":1, "gender":"male", "user_id":"100043186" }, { "city":"", "follow_cnt":5, "gender":"male", "user_id":"100060369" } ]Sample result of sequence feature FeatureView feature data
Assume that the sequence feature data reading configuration is as follows:
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 response:
[ { "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 the features associated with a ModelFeature
Retrieves the feature data of all FeatureEntities associated with a Model, and supports parent-child Entity hierarchies.
API description
// Get the features of all root entities and their child entities
GetOnlineFeatures(joinIds map[string][]interface{}) ([]map[string]interface{}, error)
// Get the features of a specified Entity and its child entities
GetOnlineFeaturesWithEntity(joinIds map[string][]interface{}, featureEntityName string) ([]map[string]interface{}, error)
Parameters
Parameter | Type | Required | Description |
joinIds | map[string][]interface{} | Yes | The join_id mapping. The key is the join_id name, and the value is the list of values. |
featureEntityName | string | No | Specifies the FeatureEntity whose feature data you want to retrieve. |
Entity hierarchy
A ModelFeature can be associated with multiple FeatureEntities that have nested parent-child relationships:
* Parent Entity: contains the primary feature fields and the fields associated with child Entities
* Child Entity: referenced by the parent Entity, and contains its own independent feature fields
When you retrieve features, pass in only the join_id of the root entity. The SDK automatically retrieves the features of the child entities based on the feature values of the parent entities.
Scenario examples
Scenario 1: Retrieve all features associated with a Model
Call GetOnlineFeatures with the join_id of all root entities. The SDK automatically retrieves all features associated with the Model, including those of child entities.
model := project.GetModelFeature("model_fv1")
if model == nil {
t.Fatal("model feature not exist")
}
// Pass in all root entities' join_id
joinIds := map[string][]interface{}{
"user_id": {"U001", "U002", "U003"},
"item_id": {"1001", "1002", "1003"},
}
// Also retrieves the features of all root entities and their child entities
features, err := model.GetOnlineFeatures(joinIds)
Sample response:
{
"item_id": "1001",
"item_title": "Smart Watch",
"item_price": 299.00,
"author_id": "A001",
"author_name": "John Doe",
"author_fans_count": 10000,
"category_id": "C001",
"category_name": "Electronics",
"category_level": 1,
"user_id": "U001",
"user_age": 28,
"user_gender": "male"
}
Scenario 2: Retrieve the features of a specified Entity (including child entities)
Call GetOnlineFeaturesWithEntity and specify the Entity name to retrieve the features of that Entity and its child entities. In this example, the item Entity is associated with two child entities, author and category, and all features of the item Entity are returned.
joinIds := map[string][]interface{}{
"item_id": {"1001", "1002", "1003"},
}
// Retrieve the features of the item Entity
// item Entity is associated with the child entities author and category, which are returned together
features, err := model.GetOnlineFeaturesWithEntity(joinIds, "item")
Sample response:
{
"item_id": "1001",
"item_title": "Smart Watch",
"item_price": 299.00,
"author_id": "A001",
"author_name": "John Doe",
"author_fans_count": 10000,
"category_id": "C001",
"category_name": "Electronics",
"category_level": 1
}
Scenario 3: Retrieve the features of a specified Entity (without child entities)
Call GetOnlineFeaturesWithEntity and specify the entity name to retrieve the features of that entity. In this example, the author entity has no child entities, so only the feature fields of the author entity itself are returned.
joinIds := map[string][]interface{}{
"author_id": {"A001", "A002", "A003"},
}
// Retrieve the features of the author Entity
// author Entity has no child entities, so only the author Entity features are returned
features, err := model.GetOnlineFeaturesWithEntity(joinIds, "author")
Sample response:
{
"author_id": "A001",
"author_name": "John Doe",
"author_fans_count": 10000
}
Example of a ModelFeature that contains sequence features
When a ModelFeature is associated with a sequence feature FeatureView, the returned data contains sequence feature fields:
The FeatureEntity that corresponds to sequence features is usually user. This example has two join_id fields: user_id and item_id. When you retrieve features, you must specify the same number of IDs.
Assume that the sequence feature data reading configuration is as follows:
Offline sequence feature field
Event name
Sequence length
Online sequence feature name
click_seq_50_seq
click
50
click_seq_50_seq
// get ModelFeature by name model := project.GetModelFeature("model_with_seq") if model == nil { t.Fatal("model feature not exist") } joinIds := map[string][]interface{}{ "user_id": {"186569075", "186569870"}, "item_id": {"299485479", "207474427"}, } // get online features features, err := model.GetOnlineFeatures(joinIds)Sample response:
[ { "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": "#Fitness Check-in", "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 Stories", "user_id": "186569870" } ]
Write operations
Writes feature data to the FeatureDB data source in real time, and supports writing real-time features and sequence features.
Scenarios
Update user features in real time (such as online feature updates)
Write user behavior sequence data (events such as clicks and plays)
Update some fields of real-time feature data (other fields are not affected)
Currently, the write API supports only real-time feature views and behavior sequence feature views whose online store is FeatureDB. You must configure the FeatureDB username and password.
Real-time feature data writing
Full-row update
project, err := client.GetProject("test_pro1")
if err != nil {
log.Fatal(err)
}
featureView := project.GetFeatureView("user_feature_view")
if featureView == nil {
t.Fatal("feature view not exist")
}
// Build the data to write
writeData := make([]map[string]interface{}, 0)
record := map[string]interface{}{
"user_id": "user_001",
"name": "John Doe",
"age": 28,
"city": "Beijing",
}
writeData := append(writeData, record)
// Write the data
featureView.WriteFeatures(writeData)
// Force a flush (ensures that all data is written)
featureView.WriteFlush()
Method | Parameter | Required | Description |
WriteFeatures | []map[string]interface{} | Yes | Writes data in batches. |
WriteFlush | None | - | Ensures that all data is written. No more data can be written after this method is called. |
Partial field update
By default, entire rows are replaced. To update only some fields, you must specify InsertMode:
import "github.com/aliyun/aliyun-pai-featurestore-go-sdk/v2/constants"
record = map[string]interface{}{
"user_id": "user_001",
"name": "Jane Doe",
}
writeData := append(writeData, record)
featureView.WriteFeaturesWithInsertMode(writeData, constants.Partial_Field_Write)
// Force a flush (ensures that all data is written)
featureView.WriteFlush()
Sequence feature data writing
Sequence feature data writing records user behavior events (such as clicks, plays, and purchases) in real time, and supports building behavior sequence features for online inference scenarios.
Example
sequenceFeatureView := project.GetFeatureView("sequence")
if sequenceFeatureView == nil {
t.Fatal("sequence featureview not found")
}
// Build the sequence data
sequenceWriteData := make([]map[string]interface{}, 0)
for i := 0; i < 10; i++ {
data := make(map[string]interface{})
data["request_id"] = int64(901850344 + i)
data["user_id"] = fmt.Sprintf("%d", 1000+i+1)
data["page"] = "home"
data["net_type"] = "wifi"
data["day_h"] = 17
data["week_day"] = 6
data["event_unix_time"] = time.Now().Unix()
data["item_id"] = fmt.Sprintf("22346678%d", i)
data["event"] = "click"
data["playtime"] = 54.7296554003366 + float64(i)
sequenceWriteData = append(sequenceWriteData, data)
}
err = sequenceFeatureView.WriteFeatures(sequenceWriteData)
if err != nil {
t.Fatal(err)
}
sequenceFeatureView.WriteFlush()