|
| 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) |
0 commit comments