@@ -109,7 +109,7 @@ from feast import (
109109from feast.on_demand_feature_view import on_demand_feature_view
110110from feast.types import Float32, Float64, Int64
111111
112- # Define an entity for the driver. You can think of entity as a primary key used to
112+ # Define an entity for the driver. You can think of an entity as a primary key used to
113113# fetch features.
114114driver = Entity(name="driver", join_keys=["driver_id"])
115115
@@ -138,7 +138,7 @@ driver_stats_fv = FeatureView(
138138 schema=[
139139 Field(name="conv_rate", dtype=Float32),
140140 Field(name="acc_rate", dtype=Float32),
141- Field(name="avg_daily_trips", dtype=Int64),
141+ Field(name="avg_daily_trips", dtype=Int64, description="Average daily trips" ),
142142 ],
143143 online=True,
144144 source=driver_stats_source,
@@ -147,12 +147,6 @@ driver_stats_fv = FeatureView(
147147 tags={"team" : " driver_performance" },
148148)
149149
150- # Defines a way to push data (to be available offline, online or both) into Feast.
151- driver_stats_push_source = PushSource(
152- name="driver_stats_push_source",
153- batch_source=driver_stats_source,
154- )
155-
156150# Define a request data source which encodes features / information only
157151# available at request time (e.g. part of the user initiated HTTP request)
158152input_request = RequestSource(
@@ -191,6 +185,51 @@ driver_activity_v1 = FeatureService(
191185driver_activity_v2 = FeatureService(
192186 name="driver_activity_v2", features=[driver_stats_fv, transformed_conv_rate]
193187)
188+
189+ # Defines a way to push data (to be available offline, online or both) into Feast.
190+ driver_stats_push_source = PushSource(
191+ name="driver_stats_push_source",
192+ batch_source=driver_stats_source,
193+ )
194+
195+ # Defines a slightly modified version of the feature view from above, where the source
196+ # has been changed to the push source. This allows fresh features to be directly pushed
197+ # to the online store for this feature view.
198+ driver_stats_fresh_fv = FeatureView(
199+ name="driver_hourly_stats_fresh",
200+ entities=[driver],
201+ ttl=timedelta(days=1),
202+ schema=[
203+ Field(name="conv_rate", dtype=Float32),
204+ Field(name="acc_rate", dtype=Float32),
205+ Field(name="avg_daily_trips", dtype=Int64),
206+ ],
207+ online=True,
208+ source=driver_stats_push_source, # Changed from above
209+ tags={"team" : " driver_performance" },
210+ )
211+
212+
213+ # Define an on demand feature view which can generate new features based on
214+ # existing feature views and RequestSource features
215+ @on_demand_feature_view(
216+ sources=[driver_stats_fresh_fv, input_request], # relies on fresh version of FV
217+ schema=[
218+ Field(name="conv_rate_plus_val1", dtype=Float64),
219+ Field(name="conv_rate_plus_val2", dtype=Float64),
220+ ],
221+ )
222+ def transformed_conv_rate_fresh(inputs : pd.DataFrame) -> pd.DataFrame:
223+ df = pd.DataFrame()
224+ df["conv_rate_plus_val1"] = inputs["conv_rate"] + inputs["val_to_add"]
225+ df["conv_rate_plus_val2"] = inputs["conv_rate"] + inputs["val_to_add_2"]
226+ return df
227+
228+
229+ driver_activity_v3 = FeatureService(
230+ name="driver_activity_v3",
231+ features=[driver_stats_fresh_fv, transformed_conv_rate_fresh],
232+ )
194233```
195234{% endtab %}
196235{% endtabs %}
@@ -254,10 +293,14 @@ feast apply
254293```
255294Created entity driver
256295Created feature view driver_hourly_stats
296+ Created feature view driver_hourly_stats_fresh
257297Created on demand feature view transformed_conv_rate
298+ Created on demand feature view transformed_conv_rate_fresh
299+ Created feature service driver_activity_v3
258300Created feature service driver_activity_v1
259301Created feature service driver_activity_v2
260302
303+ Created sqlite table my_project_driver_hourly_stats_fresh
261304Created sqlite table my_project_driver_hourly_stats
262305```
263306{% endtab %}
@@ -334,28 +377,40 @@ print(training_df.head())
334377----- Feature schema -----
335378
336379< class ' pandas.core.frame.DataFrame' >
337- Int64Index: 3 entries, 0 to 2
338- Data columns (total 6 columns):
339- # Column Non-Null Count Dtype
340- --- ------ -------------- -----
341- 0 event_timestamp 3 non-null datetime64[ns, UTC]
342- 1 driver_id 3 non-null int64
343- 2 label_driver_reported_satisfaction 3 non-null int64
344- 3 conv_rate 3 non-null float32
345- 4 acc_rate 3 non-null float32
346- 5 avg_daily_trips 3 non-null int32
347- dtypes: datetime64[ns, UTC](1), float32(2), int32(1), int64(2)
348- memory usage: 132.0 bytes
380+ RangeIndex: 3 entries, 0 to 2
381+ Data columns (total 10 columns):
382+ # Column Non-Null Count Dtype
383+ --- ------ -------------- -----
384+ 0 driver_id 3 non-null int64
385+ 1 event_timestamp 3 non-null datetime64[ns, UTC]
386+ 2 label_driver_reported_satisfaction 3 non-null int64
387+ 3 val_to_add 3 non-null int64
388+ 4 val_to_add_2 3 non-null int64
389+ 5 conv_rate 3 non-null float32
390+ 6 acc_rate 3 non-null float32
391+ 7 avg_daily_trips 3 non-null int32
392+ 8 conv_rate_plus_val1 3 non-null float64
393+ 9 conv_rate_plus_val2 3 non-null float64
394+ dtypes: datetime64[ns, UTC](1), float32(2), float64(2), int32(1), int64(4)
395+ memory usage: 336.0 bytes
349396None
350397
351398----- Example features -----
352399
353- event_timestamp driver_id ... acc_rate avg_daily_trips
354- 0 2021-08-23 15:12:55.489091+00:00 1003 ... 0.077863 741
355- 1 2021-08-23 15:49:55.489089+00:00 1002 ... 0.074327 113
356- 2 2021-08-23 16:14:55.489075+00:00 1001 ... 0.105046 347
400+ driver_id event_timestamp label_driver_reported_satisfaction \
401+ 0 1001 2021-04-12 10:59:42+00:00 1
402+ 1 1002 2021-04-12 08:12:10+00:00 5
403+ 2 1003 2021-04-12 16:40:26+00:00 3
404+
405+ val_to_add val_to_add_2 conv_rate acc_rate avg_daily_trips \
406+ 0 1 10 0.800648 0.265174 643
407+ 1 2 20 0.644141 0.996602 765
408+ 2 3 30 0.855432 0.546345 954
357409
358- [3 rows x 6 columns]
410+ conv_rate_plus_val1 conv_rate_plus_val2
411+ 0 1.800648 10.800648
412+ 1 2.644141 20.644141
413+ 2 3.855432 30.855432
359414```
360415{% endtab %}
361416{% endtabs %}
@@ -389,10 +444,20 @@ print(training_df.head())
389444```
390445----- Example features -----
391446
392- driver_id event_timestamp ... acc_rate avg_daily_trips conv_rate_plus_val1
393- 0 1001 2022-08-08 18:22:06.555018+00:00 ... 0.864639 359 1.663844
394- 1 1002 2022-08-08 18:22:06.555018+00:00 ... 0.695982 311 2.151189
395- 2 1003 2022-08-08 18:22:06.555018+00:00 ... 0.949191 789 3.769165
447+ driver_id event_timestamp \
448+ 0 1001 2024-04-19 14:58:16.452895+00:00
449+ 1 1002 2024-04-19 14:58:16.452895+00:00
450+ 2 1003 2024-04-19 14:58:16.452895+00:00
451+
452+ label_driver_reported_satisfaction val_to_add val_to_add_2 conv_rate \
453+ 0 1 1 10 0.535773
454+ 1 5 2 20 0.171976
455+ 2 3 3 30 0.275669
456+
457+ acc_rate avg_daily_trips conv_rate_plus_val1 conv_rate_plus_val2
458+ 0 0.689705 428 1.535773 10.535773
459+ 1 0.737113 369 2.171976 20.171976
460+ 2 0.156630 116 3.275669 30.275669
396461```
397462{% endtab %}
398463{% endtabs %}
@@ -413,11 +478,13 @@ feast materialize-incremental $CURRENT_TIME
413478{% tabs %}
414479{% tab title="Output" %}
415480``` bash
416- Materializing 1 feature views to 2021-08-23 16:25:46+00:00 into the sqlite online
417- store.
481+ Materializing 2 feature views to 2024-04-19 10:59:58-04:00 into the sqlite online store.
418482
419- driver_hourly_stats from 2021-08-22 16:25:47+00:00 to 2021-08-23 16:25:46+00:00:
420- 100%| ████████████████████████████████████████████| 5/5 [00:00< 00:00, 592.05it/s]
483+ driver_hourly_stats from 2024-04-18 15:00:46-04:00 to 2024-04-19 10:59:58-04:00:
484+ 100%| ████████████████████████████████████████████████████████████████| 5/5 [00:00< 00:00, 370.32it/s]
485+ driver_hourly_stats_fresh from 2024-04-18 15:00:46-04:00 to 2024-04-19 10:59:58-04:00:
486+ 100%| ███████████████████████████████████████████████████████████████| 5/5 [00:00< 00:00, 1046.64it/s]
487+ Materializing 2 feature views to 2024-04-19 10:59:58-04:00 into the sqlite online store.
421488```
422489{% endtab %}
423490{% endtabs %}
@@ -458,11 +525,11 @@ pprint(feature_vector)
458525{% tab title="Output" %}
459526``` bash
460527{
461- ' acc_rate' : [0.5732735991477966 , 0.7828438878059387 ],
462- ' avg_daily_trips' : [33, 984 ],
463- ' conv_rate' : [0.15498852729797363 , 0.6263588070869446 ],
528+ ' acc_rate' : [0.25351759791374207 , 0.8949751853942871 ],
529+ ' avg_daily_trips' : [712, 791 ],
530+ ' conv_rate' : [0.5038306713104248 , 0.9839504361152649 ],
464531 ' driver_id' : [1004, 1005]
465- }
532+ }
466533```
467534{% endtab %}
468535{% endtabs %}
@@ -479,7 +546,7 @@ The `driver_activity_v1` feature service pulls all features from the `driver_hou
479546``` python
480547from feast import FeatureService
481548driver_stats_fs = FeatureService(
482- name = " driver_activity_v1" , features = [driver_hourly_stats_view ]
549+ name = " driver_activity_v1" , features = [driver_stats_fv ]
483550)
484551```
485552
0 commit comments