本文介绍如何使用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_id
、user_id
和item_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_id
是user_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 } ]