All Products
Search
Document Center

Platform For AI:FeatureStore SDK for Java

Last Updated:Jul 31, 2024

This topic describes how to use FeatureStore SDK for Java to read data such as offline features, real-time features, and sequence features from online data stores.

Prerequisites

  • A project, feature entities, feature views, and model features are created in FeatureStore. Data is synchronized between the online and offline data stores. For more information, see Configure a project in FeatureStore.

  • An AccessKey pair of your Alibaba Cloud account is obtained. For more information, see Create an AccessKey pair.

    We recommend that you store your AccessKey ID and AccessKey secret in environment variables. For more information, see the Step 2: Configure environment variables section of the "Get started with Alibaba Cloud Darabonba SDK for Python topic.

Install FeatureStore SDK for Java

Download and install FeatureStore SDK for Java.

Initialize a FeatureStore client

API

Initialize the Configuration class.

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

Parameters

Parameter

Example

Description

regionId

cn-hangzhou

The ID of the region in which the FeatureStore client resides.

accessKeyId

System.getenv("ACCESS_KEY_ID")

The AccessKey ID that is obtained from an environment variable.

accessKeySecret

System.getenv("ACCESS_KEY_SECRET")

The AccessKey secret that is obtained from an environment variable.

projectName

holo_pro

The name of the FeatureStore project.

Note

The FeatureStore client must run in a virtual private cloud (VPC) to allow FeatureStore SDK for Java to directly connect to the online data stores. For example, FeatureStore SDK for Java can access a Hologres instance or a GraphCompute instance only over a specific VPC.

Example

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");// The System.getenv("") method is used to obtain the value of an environment variable.
        accessKey = System.getenv("ACCESS_KEY_SECRET");
    }
}
// Initialize the Configuration class.
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);

Retrieve feature data from feature views

API

public FeatureResult getOnlineFeatures(String[] joinIds);// Query the full table without specifying aliases for specific fields.
public FeatureResult getOnlineFeatures(String[] joinIds, String[] features, Map<String, String> aliasFields);

Parameters

Parameter

Example

Description

joinIds

{"100001167","100001168"}

The array of values of the primary key field. Data is queried based on the specified values of the primary key field.

features

{"user_id","city"}

Note: You can use new String[]{"*"} to query all fields in the table.

The fields to be queried in the table.

aliasFields

{"user_id":"uId"}

The aliases that are specified for fields in the table. The aliases are displayed in the data returned.

Examples

  • Example 1: Retrieve feature data from an offline feature view

    • Obtain an offline feature view and retrieve feature data from the feature view

      // 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);
    • Sample response

      [
        {
          "user_id":100001167,
          "gender":"male",
          "age":28, 
          "city":"Shenyang"
          "item_cnt":0,
          "follow_cnt":0,
          "follower_cnt":0,
          "register_time":1696658585,
          "tags":"2",
        }
        {
          "user_id":100004088,
          "gender":"female",
          "age":28, 
          "city":"Changchun"
          "item_cnt":0,
          "follow_cnt":8,
          "follower_cnt":0,
          "register_time":1695618449,
          "tags":"1",
        }
        {
          "user_id":100006646,
          "gender":"male",
          "age":28, 
          "city":"Changchun"
          "item_cnt":0,
          "follow_cnt":1,
          "follower_cnt":1,
          "register_time":1698213339,
          "tags":"1",
        }
      ]
  • Example 2: Retrieve feature data from a real-time feature view

    • Obtain a real-time feature view and retrieve feature data from the feature view

      // 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);
    • Sample response

      [
        {
          "USER_MD5":"35d3d5a52a7515c2ca6bb4d8e965149b", 
          "USER_NICKNAME":"Tom" 
        }
        {
          "USER_MD5":"0ab7e3efacd56983f16503572d2b9915", 
          "USER_NICKNAME":"Jack"
        }
        {
          "USER_MD5":"84dfd3f91dd85ea105bc74a4f0d7a067", 
          "USER_NICKNAME":"Jerry"
        }
      ]
  • Example 3: Retrieve feature data from a sequence feature view

    • Obtain a sequence feature view and retrieve feature data from the feature view

      // 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"});
            
    • Sample response

      [
        {
          "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"
        }
      ]

Retrieve feature data from model features

API

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

Parameters

Parameter

Example

Description

joinIds

[

"user_id":

{"101598051",

"101598471",

"101601287"

}

]

The map of join ID values. Each map element is a key-value pair of a specific join ID. The key is the name of the join ID, whereas the value is the value of the join ID.

featureEntityName

"user"

The name of the feature entity for which you want to query feature data.

Examples

  • Example 1: Retrieve feature data from a model feature, excluding sequence feature data

    Each model feature can be associated with multiple feature entities. You can specify multiple join IDs to retrieve the corresponding features at a time.

    In this example, the following join IDs are specified: user_id and item_id. For each join ID, the same number of values must be specified.

    //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"));
    • Retrieve feature data from all the feature entities that are associated with a model feature

      • Retrieve feature data from all the feature entities that are associated with a model feature

        //Get the data that all associated entities contain
        FeatureResult fr1 = mf1.getOnlineFeatures(mm);
      • Sample response

        [
          {
            "user_id":101598051,
            "gender":"male",
            "age":28, 
            "city":"Hangzhou",
            "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":"#Workout Tracking"
          }
          {
            "user_id":200004157,
            "gender":"female",
            "age":31, 
            "city":"Shenzhen"
            "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":"#Idiom Story"
          }
          {
            "user_id":101601287,
            "gender":"female",
            "age":33, 
            "city":"Shenzhen"
            "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":"#Workout Tracking"
          }
        ]
    • Retrieve feature data of a specific feature entity that is associated with a model feature

      • Retrieve feature data of a specific feature entity that is associated with a model feature

        // Retrieve only the feature data of the server feature entity.
        FeatureResult fr2 = mf1.getOnlineFeaturesWithEntity(mm, "server");
      • Sample response

        [
        	{
            "item_id":200004157, 
            "author":137649839, 
            "category":2,
            "click_count":0,
            "duration":18.0,
            "praise_count":30,
            "pub_time":1698208690,
            "title":"#Workout Tracking"
          },
          {
            "item_id":200006185,
            "author":134195601, 
            "category":14,
            "click_count":50,
            "duration":55.0,
            "praise_count":21,
            "pub_time":1696700908,
            "title":"#Idiom Story"
          },
          {
            "item_id":200034730,
            "author":112739045, 
            "category":6,
            "click_count":2,
            "duration":9.0,
            "praise_count":0,
            "pub_time":1696568654,
            "title":"#Workout Tracking"
          }
        ]
  • Example 2: Retrieve feature data from a model feature, including sequence feature data

    Each model feature can be associated with multiple feature entities. You can specify multiple join IDs to retrieve the corresponding features at a time.

    In this example, the following join ID is used: user_id.

    // Retrieve feature data from a model feature, including sequence feature data.
    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"));
    • Retrieve feature data from all the feature entities that are associated with a model feature

      • Retrieve feature data from all the feature entities that are associated with a model feature

        FeatureResult fr3 = mdt2.getOnlineFeatures(fsmap);
      • Sample response

        [
          {
            "click_50_seq_event_time":"null;1698170945",
            "gender":"male",
            "click_50_seq_ts":"1704292557212;1704292557212",
            "city":"Shenyang",
            "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":"#Idiom Story",
            "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":"Ningbo",
          "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":"#Idiom Story",
          "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":"Shenyang",
          "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":"#Idiom Story",
          "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":"Ningbo",
          "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":"#Idiom Story",
          "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,
          }
        ]
    • Retrieve feature data of a specific feature entity that is associated with a model feature

      • Retrieve feature data of a specific feature entity that is associated with a model feature

        FeatureResult fr4 = mdt1.getOnlineFeaturesWithEntity(fsmap, "client");
      • Sample response

        [
          {
            "click_50_seq_event_time":"null;1698170945"
            "gender":"male"
            "click_50_seq_ts":"1704292555552;1704292555552"
            "city":"Shenyang"
            "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":"Ningbo",
            "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":"Shenyang",
            "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":"Ningbo",
            "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
          }
        ]