[Alpha] Streaming feature computation with Denormalized
Last updated
Was this helpful?
Was this helpful?
mkdir my-feature-project
cd my-feature-project
python -m venv .venv
source .venv/bin/activate # or `.venv\Scripts\activate` on Windows
pip install denormalized[feast] feastfeast init feature_repomy-feature-project/
├── feature_repo/
│ ├── feature_store.yaml
│ └── sensor_data.py # Feature definitions
├── stream_job.py # Denormalized pipeline
└── main.py # Pipeline runnerfrom feast import Entity, FeatureView, PushSource, Field
from feast.types import Float64, String
# Define your entity
sensor = Entity(
name="sensor",
join_keys=["sensor_name"],
)
# Create a push source for real-time features
source = PushSource(
name="push_sensor_statistics",
batch_source=your_batch_source # Define your batch source
)
# Define your feature view
stats_view = FeatureView(
name="sensor_statistics",
entities=[sensor],
schema=ds.get_feast_schema(), # Denormalized handles this for you!
source=source,
online=True,
)from denormalized import Context, FeastDataStream
from denormalized.datafusion import col, functions as f
from feast import FeatureStore
sample_event = {
"occurred_at_ms": 100,
"sensor_name": "foo",
"reading": 0.0,
}
# Create a stream from your Kafka topic
ds = FeastDataStream(Context().from_topic("temperature", json.dumps(sample_event), "localhost:9092", "occurred_at_ms"))
# Define your feature computations
ds = ds.window(
[col("sensor_name")], # Group by sensor
[
f.count(col("reading")).alias("count"),
f.min(col("reading")).alias("min"),
f.max(col("reading")).alias("max"),
f.avg(col("reading")).alias("average"),
],
1000, # Window size in ms
None # Slide interval (None = tumbling window)
)
feature_store = FeatureStore(repo_path="feature_repo/")
# This single line connects Denormalized to Feast!
ds.write_feast_feature(feature_store, "push_sensor_statistics")