Skip to content

Commit 632859d

Browse files
committed
add ttl example
1 parent 537d4ff commit 632859d

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

docs/user_guides/fs/feature_group/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ This section serves to provide guides and examples for the common usage of abstr
88
- [Data Types and Schema management](data_types.md)
99
- [Statistics](statistics.md)
1010
- [Data Validation](data_validation.md)
11-
- [Feature Monitoring](feature_monitoring.md)
11+
- [Feature Monitoring](feature_monitoring.md)
12+
- [Time-To-Live (TTL)](ttl.md)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
## Feature Group TTL Usage Guide
2+
3+
Below are examples showing how to work with TTL (Time To Live) on feature groups.
4+
5+
---
6+
7+
### 1. Create a Feature Group with TTL Enabled
8+
9+
```python
10+
from datetime import datetime, timezone
11+
import pandas as pd
12+
13+
# Assume you already have a feature store handle
14+
# fs = ...
15+
16+
now = datetime.now(timezone.utc)
17+
df = pd.DataFrame(
18+
{
19+
"id": [0, 1, 2],
20+
"timestamp": [now, now, now],
21+
"feature1": [10, 20, 30],
22+
"feature2": ["a", "b", "c"],
23+
}
24+
)
25+
26+
fg = fs.create_feature_group(
27+
name="fg_ttl_example",
28+
version=1,
29+
primary_key=["id"],
30+
event_time="timestamp",
31+
online_enabled=True,
32+
ttl=60, # TTL in seconds
33+
)
34+
35+
fg.insert(
36+
df,
37+
write_options={
38+
"start_offline_materialization": False,
39+
"wait_for_online_ingestion": True,
40+
},
41+
)
42+
43+
fg.read(online=True) # return empty df after ttl
44+
```
45+
46+
---
47+
48+
### 2. Update the TTL Value
49+
50+
```python
51+
# Example: change TTL to a new value (in seconds)
52+
new_ttl_seconds = 120
53+
# Later, update TTL
54+
fg.enable_ttl(new_ttl_seconds)
55+
56+
---
57+
58+
### 3. Disable TTL and Enable It Again
59+
60+
```python
61+
# Disable TTL
62+
fg.disable_ttl()
63+
64+
# ... time passes, you decide to enforce TTL again ...
65+
66+
# Re‑enable TTL with a new value (in seconds)
67+
fg.enable_ttl() # If ttl is not set, the feature group will be enabled with the last TTL value being set.
68+
69+
# Or Re‑enable TTL with a new value (in seconds)
70+
fg.enable_ttl(ttl=90)
71+
```
72+
73+
---
74+
75+
### 4. Enable TTL on an Existing Feature Group (Created Without TTL)
76+
77+
```python
78+
# Assume this FG was created earlier without TTL
79+
fg = fs.get_feature_group(
80+
name="fg_existing_no_ttl",
81+
version=1,
82+
)
83+
84+
# Enable TTL for the first time
85+
fg.enable_ttl(ttl=60) # seconds
86+
87+
```
88+
89+
90+
### Reference
91+
92+
API reference on all possible types of ttl value can be found [here.](https://docs.hopsworks.ai/hopsworks-api/latest/generated/api/feature_group_api/#enable_ttl)

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ nav:
8787
- Notification: user_guides/fs/feature_group/notification.md
8888
- On-Demand Transformations: user_guides/fs/feature_group/on_demand_transformations.md
8989
- Online Ingestion Observability: user_guides/fs/feature_group/online_ingestion_observability.md
90+
- Time-To-Live (TTL): user_guides/fs/feature_group/ttl.md
9091
- Feature View:
9192
- user_guides/fs/feature_view/index.md
9293
- Overview: user_guides/fs/feature_view/overview.md

0 commit comments

Comments
 (0)