全部產品
Search
文件中心

Platform For AI:FeatureStore Java SDK

更新時間:Aug 01, 2024

本文介紹如何使用Java SDK讀取FeatureStore線上資料來源的資料(離線特徵、即時特徵、序列特徵等)。

前提條件

  • 已建立FeatureStore專案(Project)、特徵實體(FeatureEntity)、特徵視圖(FeatureView)和模型特徵(ModelFeature),並完成資料同步操作。具體操作,請參見配置FeatureStore專案

  • 已擷取阿里雲賬戶的AccessKey ID和AccessKey Secret。具體操作,請參見建立AccessKey

    建議使用本地配置環境變數的方式儲存AccessKey ID和AccessKey Secret。具體操作,請參見配置環境變數

安裝FeatureStore Java SDK

下載並安裝FeatureStore Java SDK

初始化FeatureStore用戶端

介面

初始化配置類Configuration。

Configuration configuration = new Configuration(regionId,accessKeyId,accessKeySecret,projectName);

參數說明

參數

樣本

說明

regionId

cn-hangzhou

所屬地區。

accessKeyId

System.getenv("ACCESS_KEY_ID")

通過環境變數擷取AccessKey ID。

accessKeySecret

System.getenv("ACCESS_KEY_SECRET")

通過環境變數擷取AccessKey Secret。

projectName

holo_pro

FeatureStore專案名稱。

說明

由於SDK是直連線上資料來源,用戶端需要在VPC環境運行。例如Hologres和GraphCompute需要在指定的VPC才能串連。

樣本

public class Constants {
    public static String accessId = "";
    public static String accessKey = "";
    public static String host = "paifeaturestore.cn-hangzhou.aliyuncs.com";

    static {
        accessId = System.getenv("ACCESS_KEY_ID");//System.getenv("")擷取環境變數的
        accessKey = System.getenv("ACCESS_KEY_SECRET");
    }
}
//註冊配置類
Configuration configuration = new Configuration("cn-hangzhou",Constants.accessId,
                                                Constants.accessKey,"dec8");
configuration.setDomain(Constants.host);
ApiClient apiClient = new ApiClient(configuration);
FeatureStoreClient fsclient = new FeatureStoreClient(apiClient);

擷取FeatureView的特徵資料

介面

public FeatureResult getOnlineFeatures(String[] joinIds);//即全表查詢,不起別名
public FeatureResult getOnlineFeatures(String[] joinIds, String[] features, Map<String, String> aliasFields);

參數說明

參數

樣本

說明

joinIds

{"100001167","100001168"}

主鍵欄位數組,根據傳入的主鍵欄位值查詢相關資料。

features

{"user_id","city"}

註:new String[]{"*"},即擷取所有欄位

具體擷取資料表的哪些欄位。

aliasFields

{"user_id":"uId"}

給資料表欄位起的別名,在返回資料時也顯示該別名。

樣本

  • 樣本一:擷取離線特徵FeatureView的特徵資料

    • 擷取FeatureView及離線表同步的資料

      // get FeatureView By name
      //mo1(Offline FeatureView)
      FeatureView mo1 = project.getFeatureViewMap().get("mo1");
      if(mo1==null){
          throw new RuntimeException("This featureView is not exist");
      }
      
      HashMap<String, String> ss = new HashMap<>();
      FeatureResult features = mo1.getOnlineFeatures(new String[]{"100001167", "100004088","100006646"}, new String[]{"*"}, ss);
    • 返回樣本結果

      [
        {
          "user_id":100001167,
          "gender":"male",
          "age":28, 
          "city":"瀋陽市"
          "item_cnt":0,
          "follow_cnt":0,
          "follower_cnt":0,
          "register_time":1696658585,
          "tags":"2",
        }
        {
          "user_id":100004088,
          "gender":"female",
          "age":28, 
          "city":"長春市"
          "item_cnt":0,
          "follow_cnt":8,
          "follower_cnt":0,
          "register_time":1695618449,
          "tags":"1",
        }
        {
          "user_id":100006646,
          "gender":"male",
          "age":28, 
          "city":"長春市"
          "item_cnt":0,
          "follow_cnt":1,
          "follower_cnt":1,
          "register_time":1698213339,
          "tags":"1",
        }
      ]
  • 樣本二:擷取即時特徵FeatureView的特徵資料

    • 擷取FeatureView及線上表的資料

      // get FeatureView By name
      //tfv1(Online FeatureView)
      FeatureView tfv1 = project.getFeatureViewMap().get("tfv1");
      if(tfv1==null){
      throw new RuntimeException("This featureView is not exist");
      }
      //Get data
      FeatureResult rf = tfv1.getOnlineFeatures(new String[]{"35d3d5a52a7515c2ca6bb4d8e965149b", "0ab7e3efacd56983f16503572d2b9915","84dfd3f91dd85ea105bc74a4f0d7a067"}, new String[]{"*"}, ss);
    • 返回樣本結果

      [
        {
          "USER_MD5":"35d3d5a52a7515c2ca6bb4d8e965149b", 
          "USER_NICKNAME":"密斯sini" 
        }
        {
          "USER_MD5":"0ab7e3efacd56983f16503572d2b9915", 
          "USER_NICKNAME":"戀丶你灬"
        }
        {
          "USER_MD5":"84dfd3f91dd85ea105bc74a4f0d7a067", 
          "USER_NICKNAME":"小學生的爹"
        }
      ]
  • 樣本三:擷取序列特徵FeatureView的特徵資料

    • 擷取FeatureView及離線表同步的資料

      // get FeatureView By name
      //ots_seq2(Sequence FeatureView)
      SequenceFeatureView ots_seq2 = project.getSeqFeatureView("ots_seq2");
      if(ots_seq2==null){
          throw new RuntimeException("This featureView is not exist");
      }
      //get data
      FeatureResult features2 = ots_seq2.getOnlineFeatures(new String[]{"157843277", "157843278"});
            
    • 返回樣本結果

      [
        {
          "click _50_seq_playtime":"null;15.0",
          "click_50_seq_event_time":"null;1704684504747",
          "click_50_seq_ts":"625662604;625662604",
          "user_id":"157843277",
          "click_50_seq":"null;200167895",
          "click_50_seq_event":"null;click",
          "click_50_seq_item_id":"null;200167895"
        }
        {
          "click_50_seq_playtime":"null;15.0",
          "click_50_seq_event_time":"null;1704684504747",
          "click_50_seq_ts":"625662604;625662604",
          "user_id":"157843278",
          "click_50_seq":"null;200167895",
          "click_50_seq_event":"null;click",
          "click_50_seq_item_id":"null;200167895"
        }
      ]

擷取ModelFeature裡的特徵資料

介面

public FeatureResult getOnlineFeatures(Map<String, List<String>> joinIds);
public FeatureResult getOnlineFeaturesWithEntity(Map<String, List<String>> joinIds, String featureEntityName);

參數說明

參數

樣本

說明

joinIds

[

"user_id":

{"101598051",

"101598471",

"101601287"

}

]

joinIds的map集合。key為JoinId的名稱,values為JoinId的值。

featureEntityName

"user"

擷取某一側FeatureEntity的指定資料。

樣本

  • 樣本一:擷取ModelFeature特徵資料樣本(不包含序列特徵FeatureView)

    ModelFeature可以關聯多個FeatureEntity,可以設定多個join_id,然後特徵統一返回。

    樣本中有兩個join_iduser_iditem_id。擷取特徵的時候需要設定相同的ID數量。

    //Obtain model features
    Model mf1 = project.getModelFeature("model_ots1");
    HashMap<String, List<String>> mm = new HashMap<>();
    mm.put("user_id", Arrays.asList("101598051", "101598471", "101601287"));
    mm.put("item_id",Arrays.asList("200004157", "200006185", "200034730"));
    • 擷取ModelFeature包含的所有FeatureEntity的特徵資料

      • 擷取ModelFeature包含的所有FeatureEntity的特徵資料

        //Get the data that all associated entities contain
        FeatureResult fr1 = mf1.getOnlineFeatures(mm);
      • 返回樣本結果

        [
          {
            "user_id":101598051,
            "gender":"male",
            "age":28, 
            "city":"杭州市"
            "item_cnt":0,
            "follow_cnt":0,
            "follower_cnt":0,
            "register_time":1695785665,
            "tags":"1",
            "item_id":200004157,
            "author":137649839, 
            "category":2,
            "click_count":0,
            "duration":18.0,
            "praise_count":30,
            "pub_time":1698208690,
            "title":"#健身打卡"
          }
          {
            "user_id":200004157,
            "gender":"female",
            "age":31, 
            "city":"深圳市"
            "item_cnt":0,
            "follow_cnt":1,
            "follower_cnt":0,
            "register_time":1695726582,
            "tags":"1",
            "item_id":200006185,
            "author":134195601, 
            "category":14,
            "click_count":50,
            "duration":55.0,
            "praise_count":21,
            "pub_time":1696700908,
            "title":"#成語故事"
          }
          {
            "user_id":101601287,
            "gender":"female",
            "age":33, 
            "city":"深圳市"
            "item_cnt":0,
            "follow_cnt":0,
            "follower_cnt":55,
            "register_time":1697519102,
            "tags":"0",
            "item_id":200034730,
            "author":112739045, 
            "category":6,
            "click_count":2,
            "duration":9.0,
            "praise_count":0,
            "pub_time":1696568654,
            "title":"#健身打卡"
          }
        ]
    • 擷取ModelFeature指定FeatureEntity的特徵資料

      • 擷取ModelFeature指定FeatureEntity的特徵資料

        //僅擷取server側實體包含的資料
        FeatureResult fr2 = mf1.getOnlineFeaturesWithEntity(mm, "server");
      • 返回樣本結果

        [
        	{
            "item_id":200004157,
            "author":137649839, 
            "category":2,
            "click_count":0,
            "duration":18.0,
            "praise_count":30,
            "pub_time":1698208690,
            "title":"#健身打卡"
          },
          {
            "item_id":200006185,
            "author":134195601, 
            "category":14,
            "click_count":50,
            "duration":55.0,
            "praise_count":21,
            "pub_time":1696700908,
            "title":"#成語故事"
          },
          {
            "item_id":200034730,
            "author":112739045, 
            "category":6,
            "click_count":2,
            "duration":9.0,
            "praise_count":0,
            "pub_time":1696568654,
            "title":"#健身打卡"
          }
        ]
  • 樣本二:擷取ModelFeature特徵資料樣本(包含序列特徵FeatureView)

    ModelFeature可以關聯多個FeatureEntity,可以設定多個join_id,然後特徵統一返回。

    樣本中的join_iduser_id

    //擷取包含序列化特徵的ModelFeatures資料
    Model mdt1 =project.getModelFeature("mdt1");
    if(mdt1==null){
        throw new RuntimeException("This modelFeature is not exist");
    }
    HashMap<String, List<String>> fsmap = new HashMap<>();
    fsmap.put("user_id",Arrays.asList("100001167","100024146"));
    fsmap.put("item_id",Arrays.asList("200138790","200385417"));
    • 擷取ModelFeature裡的所包含的FeatureEntity的特徵資料

      • 擷取ModelFeature裡的所包含的FeatureEntity的特徵資料

        FeatureResult fr3 = mdt2.getOnlineFeatures(fsmap);
      • 返回樣本結果

        [
          {
            "click_50_seq_event_time":"null;1698170945",
            "gender":"male",
            "click_50_seq_ts":"1704292557212;1704292557212",
            "city":"瀋陽市",
            "item_id":"200138790",
            "click_50_seq":"null;204153583",
            "author":186784264,
            "pub_time":1696574947,
            "follower_cnt":0,
            "follow_cnt":0,
            "item_cnt":0,
            "click_count":2,
            "title":"#成語故事",
            "register_time":1696658585,
            "tags":2,
            "duration":13.0,
            "click_50_seq_playtime":"null;98.93932923011255",
            "user_id":"100001167",
            "praise_count":3,
            "click_50_seq_event":null;click,
            "category":20,
            "click_50_seq_item_id":null;204153583,
            "age":28,
          }
          {
          "click_50_seq_event_time":"null;1698180365",
          "gender":"male",
          "click_50_seq_ts":"1704292547792;1704292547792;1704292547792",
          "city":"寧波市",
          "item_id":200385417,
          "click_50_seq":"null;299049390",
          "author":189247964,
          "pub_time":1696432224,
          "follower_cnt":47,
          "follow_cnt":0,
          "item_cnt":0,
          "click_count":4,
          "title":"#成語故事",
          "register_time":1697253820,
          "tags":1,
          "duration":"9.0",
          "click_50_seq_playtime":"null;32.15018252408633",
          "user_id":100024146,
          "praise_count":0,
          "click_50_seq_event":"null;click",
          "category":0,
          "click_50_seq_item_id":"null;299049390",
          "age":28,
          }
          {
          "click_50_seq_event_time":"null;1698170945",
          "gender":"male",
          "click_50_seq_ts":"1704292557212;1704292557212",
          "city":"瀋陽市",
          "item_id":200138790,
          "click_50_seq":"null;204153583",
          "author":186784264,
          "pub_time":1696574947,
          "follower_cnt":0,
          "follow_cnt":0,
          "item_cnt":0,
          "click_count":2,
          "title":"#成語故事",
          "register_time":1696658585,
          "tags":2,
          "duration":13.0,
          "click_50_seq_playtime":"null;98.93932923011255",
          "user_id":"100001167",
          "praise_count":3,
          "click_50_seq_event":"null;click",
          "category":20,
          "click_50_seq_item_id":"null;204153583",
          "age":28,
          }
          {
          "click_50_seq_event_time":"null;1698180365",
          "gender":"male",
          "click_50_seq_ts":"1704292547792;1704292547792",
          "city":"寧波市",
          "item_id":200385417,
          "click_50_seq":"null;299049390",
          "author":189247964,
          "pub_time":1696432224,
          "follower_cnt":47,
          "follow_cnt":0,
          "item_cnt":0,
          "click_count":4,
          "title":"#成語故事",
          "register_time":1697253820,
          "tags":1,
          "duration":9.0
          "click_50_seq_playtime":"null;32.15018252408633",
          "user_id":"100024146",
          "praise_count":"0",
          "click_50_seq_event":"null;click",
          "category":0,
          "click_50_seq_item_id":"null;299049390",
          "age":28,
          }
        ]
    • 擷取ModelFeature裡指定FeatureEntity的特徵資料

      • 擷取ModelFeature裡指定FeatureEntity的特徵資料

        FeatureResult fr4 = mdt1.getOnlineFeaturesWithEntity(fsmap, "client");
      • 返回樣本結果

        [
          {
            "click_50_seq_event_time":"null;1698170945"
            "gender":"male"
            "click_50_seq_ts":"1704292555552;1704292555552"
            "city":"瀋陽市"
            "click_50_seq":"null;204153583"
            "follower_cnt":0
            "follow_cnt":0
            "item_cnt":0
            "register_time":1696658585
            "tags":2
            "click_50_seq_playtime":"null;98.93932923011255"
            "user_id":"100001167"
            "click_50_seq_event":"null;click"
            "click_50_seq_item_id":"null;204153583"
            "age":28
          }
          {
            "click_50_seq_event_time":"null;1698180365",
            "gender":"male",
            "click_50_seq_ts":1704292546132;1704292546132;1704292546132,
            "city":"寧波市",
            "click_50_seq":"null;299049390"
            "follower_cnt":47,
            "follow_cnt":0,
            "item_cnt":0,
            "register_time":1697253820,
            "tags":"1",
            "click_50_seq_playtime":"null;32.15018252408633",
            "user_id":100024146,
            "click_50_seq_event":"null;click",
            "click_50_seq_item_id":"null;299049390",
            "age":28
          }
          {
            "click_50_seq_event_time":"null;1698170945",
            "gender":"male",
            "click_50_seq_ts":"1704292555552;1704292555552",
            "city":"瀋陽市",
            "click_50_seq":"null;204153583",
            "follower_cnt":0,
            "follow_cnt":0,
            "item_cnt":0,
            "register_time":1696658585,
            "tags":2,
            "click_50_seq_playtime":"null;98.93932923011255",
            "user_id":"100001167",
            "click_50_seq_event":"null;click",
            "click_50_seq_item_id":"null;204153583",
            "age":28
          }
          {
            "click_50_seq_event_time":"null;1698180365",
            "gender":"male",
            "click_50_seq_ts":"1704292546132;1704292546132;1704292546132",
            "city":"寧波市",
            "click_50_seq":"null;299049390",
            "follower_cnt":47,
            "follow_cnt":0,
            "item_cnt":0,
            "register_time":1697253820,
            "tags":1,
            "click_50_seq_playtime":"null;32.15018252408633"
            "user_id":"100024146",
            "click_50_seq_event":"null;click",
            "click_50_seq_item_id":"null;299049390",
            "age":28
          }
        ]