全部产品
Search
文档中心

人工智能平台 PAI:FeatureStore Go SDK

更新时间:May 23, 2024

本文介绍如何使用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是访问表格存储服务的AccessKeyID,通过官方网站申请或通过管理员获取。
//accessKeySecret是访问表格存储服务的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

指定需要获取的特征的名称。[]string{"*"}代表获取FeatureView下的所有特征。

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_iduser_iditem_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_iduser_iditem_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"
        }
      ]