|
| 1 | +package com.gojek.feast.v1alpha1; |
| 2 | + |
| 3 | +import feast.serving.ServingAPIProto.GetFeastServingInfoRequest; |
| 4 | +import feast.serving.ServingAPIProto.GetFeastServingInfoResponse; |
| 5 | +import feast.serving.ServingAPIProto.GetOnlineFeaturesRequest; |
| 6 | +import feast.serving.ServingAPIProto.GetOnlineFeaturesRequest.EntityRow; |
| 7 | +import feast.serving.ServingAPIProto.GetOnlineFeaturesRequest.FeatureSet; |
| 8 | +import feast.serving.ServingAPIProto.GetOnlineFeaturesResponse; |
| 9 | +import feast.serving.ServingServiceGrpc; |
| 10 | +import io.grpc.ManagedChannel; |
| 11 | +import io.grpc.ManagedChannelBuilder; |
| 12 | +import java.util.List; |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | +import java.util.stream.Collectors; |
| 15 | +import org.slf4j.Logger; |
| 16 | +import org.slf4j.LoggerFactory; |
| 17 | + |
| 18 | +@SuppressWarnings("WeakerAccess") |
| 19 | +public class FeastClient implements AutoCloseable { |
| 20 | + Logger logger = LoggerFactory.getLogger(FeastClient.class); |
| 21 | + |
| 22 | + private static final int CHANNEL_SHUTDOWN_TIMEOUT_SEC = 5; |
| 23 | + |
| 24 | + private final ManagedChannel channel; |
| 25 | + private final ServingServiceGrpc.ServingServiceBlockingStub stub; |
| 26 | + |
| 27 | + /** |
| 28 | + * Create a client to access Feast |
| 29 | + * |
| 30 | + * @param host hostname or ip address of Feast serving GRPC server |
| 31 | + * @param port port number of Feast serving GRPC server |
| 32 | + * @return {@link FeastClient} |
| 33 | + */ |
| 34 | + public static FeastClient create(String host, int port) { |
| 35 | + ManagedChannel channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build(); |
| 36 | + return new FeastClient(channel); |
| 37 | + } |
| 38 | + |
| 39 | + public GetFeastServingInfoResponse getFeastServingInfo() { |
| 40 | + return stub.getFeastServingInfo(GetFeastServingInfoRequest.newBuilder().build()); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Get online features from Feast. |
| 45 | + * |
| 46 | + * <p>See {@link #getOnlineFeatures(List, List, boolean)} |
| 47 | + * |
| 48 | + * @param featureIds list of feature id to retrieve, feature id follows this format |
| 49 | + * [feature_set_name]:[version]:[feature_name] |
| 50 | + * @param rows list of {@link Row} to select the entities to retrieve the features for |
| 51 | + * @return list of {@link Row} containing features |
| 52 | + */ |
| 53 | + public List<Row> getOnlineFeatures(List<String> featureIds, List<Row> rows) { |
| 54 | + return getOnlineFeatures(featureIds, rows, false); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get online features from Feast. |
| 59 | + * |
| 60 | + * <p>Example of retrieving online features for driver feature set, version 1, with features |
| 61 | + * driver_id and driver_name |
| 62 | + * |
| 63 | + * <pre>{@code |
| 64 | + * FeastClient client = FeastClient.create("localhost", 6566); |
| 65 | + * List<String> requestedFeatureIds = Arrays.asList("driver:1:driver_id", "driver:1:driver_name"); |
| 66 | + * List<Row> requestedRows = |
| 67 | + * Arrays.asList(Row.create().set("driver_id", 123), Row.create().set("driver_id", 456)); |
| 68 | + * List<Row> retrievedFeatures = client.getOnlineFeatures(requestedFeatureIds, requestedRows); |
| 69 | + * retrievedFeatures.forEach(System.out::println); |
| 70 | + * }</pre> |
| 71 | + * |
| 72 | + * @param featureIds list of feature id to retrieve, feature id follows this format |
| 73 | + * [feature_set_name]:[version]:[feature_name] |
| 74 | + * @param rows list of {@link Row} to select the entities to retrieve the features for |
| 75 | + * @param omitEntitiesInResponse if true, the returned {@link Row} will not contain field and |
| 76 | + * value for the entity |
| 77 | + * @return list of {@link Row} containing features |
| 78 | + */ |
| 79 | + public List<Row> getOnlineFeatures( |
| 80 | + List<String> featureIds, List<Row> rows, boolean omitEntitiesInResponse) { |
| 81 | + List<FeatureSet> featureSets = RequestUtil.createFeatureSets(featureIds); |
| 82 | + List<EntityRow> entityRows = |
| 83 | + rows.stream() |
| 84 | + .map( |
| 85 | + row -> |
| 86 | + EntityRow.newBuilder() |
| 87 | + .setEntityTimestamp(row.getEntityTimestamp()) |
| 88 | + .putAllFields(row.getFields()) |
| 89 | + .build()) |
| 90 | + .collect(Collectors.toList()); |
| 91 | + |
| 92 | + GetOnlineFeaturesResponse response = |
| 93 | + stub.getOnlineFeatures( |
| 94 | + GetOnlineFeaturesRequest.newBuilder() |
| 95 | + .addAllFeatureSets(featureSets) |
| 96 | + .addAllEntityRows(entityRows) |
| 97 | + .setOmitEntitiesInResponse(omitEntitiesInResponse) |
| 98 | + .build()); |
| 99 | + |
| 100 | + return response.getFieldValuesList().stream() |
| 101 | + .map( |
| 102 | + field -> { |
| 103 | + Row row = Row.create(); |
| 104 | + field.getFieldsMap().forEach(row::set); |
| 105 | + return row; |
| 106 | + }) |
| 107 | + .collect(Collectors.toList()); |
| 108 | + } |
| 109 | + |
| 110 | + private FeastClient(ManagedChannel channel) { |
| 111 | + this.channel = channel; |
| 112 | + stub = ServingServiceGrpc.newBlockingStub(channel); |
| 113 | + } |
| 114 | + |
| 115 | + public void close() throws Exception { |
| 116 | + if (channel != null) { |
| 117 | + channel.shutdown().awaitTermination(CHANNEL_SHUTDOWN_TIMEOUT_SEC, TimeUnit.SECONDS); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments