1616 */
1717package feast .storage .connectors .bigtable .retriever ;
1818
19- import com .google .api .gax .rpc .ServerStream ;
2019import com .google .cloud .bigtable .data .v2 .BigtableDataClient ;
2120import com .google .cloud .bigtable .data .v2 .models .Filters ;
2221import com .google .cloud .bigtable .data .v2 .models .Query ;
3029import feast .storage .api .retriever .NativeFeature ;
3130import feast .storage .api .retriever .OnlineRetrieverV2 ;
3231import java .io .IOException ;
33- import java .util .List ;
34- import java .util .Objects ;
32+ import java .util .* ;
33+ import java .util .function . Function ;
3534import java .util .stream .Collectors ;
35+ import java .util .stream .Stream ;
3636import java .util .stream .StreamSupport ;
3737import org .apache .avro .AvroRuntimeException ;
3838import org .apache .avro .Schema ;
@@ -143,40 +143,47 @@ public List<List<Feature>> getOnlineFeatures(
143143 List <String > columnFamilies = getColumnFamilies (featureReferences );
144144 String tableName = getTableName (project , entityNames );
145145
146- ServerStream <Row > rowsFromBigTable =
147- getFeaturesFromBigTable (tableName , entityRows , columnFamilies , entityNames );
146+ List <ByteString > rowKeys =
147+ entityRows .stream ()
148+ .map (row -> convertEntityValueToBigTableKey (row , entityNames ))
149+ .collect (Collectors .toList ());
150+ Map <ByteString , Row > rowsFromBigTable =
151+ getFeaturesFromBigTable (tableName , rowKeys , columnFamilies );
148152 List <List <Feature >> features =
149- convertRowToFeature (tableName , rowsFromBigTable , featureReferences );
153+ convertRowToFeature (tableName , rowKeys , rowsFromBigTable , featureReferences );
150154
151155 return features ;
152156 }
153157
154- private ServerStream <Row > getFeaturesFromBigTable (
155- String tableName ,
156- List <EntityRow > entityRows ,
157- List <String > columnFamilies ,
158- List <String > entityNames ) {
158+ private Map <ByteString , Row > getFeaturesFromBigTable (
159+ String tableName , List <ByteString > rowKeys , List <String > columnFamilies ) {
159160
160161 Query rowQuery = Query .create (tableName );
161162 Filters .InterleaveFilter familyFilter = Filters .FILTERS .interleave ();
162163 columnFamilies .forEach (cf -> familyFilter .filter (Filters .FILTERS .family ().exactMatch (cf )));
163164
164- for (EntityRow row : entityRows ) {
165- ByteString rowKey = convertEntityValueToBigTableKey (row , entityNames );
166- rowQuery = rowQuery .rowKey (rowKey );
165+ for (ByteString rowKey : rowKeys ) {
166+ rowQuery .rowKey (rowKey );
167167 }
168168
169- return client .readRows (rowQuery );
169+ return StreamSupport .stream (client .readRows (rowQuery ).spliterator (), false )
170+ .collect (Collectors .toMap (Row ::getKey , Function .identity ()));
170171 }
171172
172173 private List <List <Feature >> convertRowToFeature (
173- String tableName , ServerStream <Row > rows , List <FeatureReferenceV2 > featureReferences ) {
174+ String tableName ,
175+ List <ByteString > rowKeys ,
176+ Map <ByteString , Row > rows ,
177+ List <FeatureReferenceV2 > featureReferences ) {
174178
175- return StreamSupport .stream (rows .spliterator (), false )
176- .flatMap (
177- row ->
178- row .getCells ().stream ()
179- .map (
179+ return rowKeys .stream ()
180+ .map (
181+ rowKey -> {
182+ if (!rows .containsKey (rowKey )) {
183+ return Collections .<Feature >emptyList ();
184+ } else {
185+ return rows .get (rowKey ).getCells ().stream ()
186+ .flatMap (
180187 rowCell -> {
181188 String family = rowCell .getFamily ();
182189 ByteString value = rowCell .getValue ();
@@ -200,8 +207,11 @@ private List<List<Feature>> convertRowToFeature(
200207 throw new RuntimeException ("Failed to decode features from BigTable" );
201208 }
202209
203- return features ;
204- }))
210+ return features .stream ();
211+ })
212+ .collect (Collectors .toList ());
213+ }
214+ })
205215 .collect (Collectors .toList ());
206216 }
207217}
0 commit comments