10.7.4 – Materialized Views
Module: Section 10 – Kafka Ecosystem & Tools
Section: 10.7 – Joins & Enrichment with ksqlDB
Goal: Create queryable materialized views with pull queries
Creating Materialized Views
CREATE TABLE user_stats AS
SELECT
user_id,
COUNT(*) AS order_count,
SUM(amount) AS total_spent,
AVG(amount) AS avg_order_value
FROM orders
GROUP BY user_id
EMIT CHANGES;
Result: Table with running statistics per user
Pull Queries
Point-in-time lookups:
-- Get specific user stats
SELECT * FROM user_stats WHERE user_id = 123;
-- Get top spenders
SELECT * FROM user_stats
ORDER BY total_spent DESC
LIMIT 10;
REST API Integration
curl -X POST http://localhost:8088/query \
-d '{
"ksql": "SELECT * FROM user_stats WHERE user_id = 123;"
}'
Use Case: Serve real-time data to APIs/dashboards
Key Takeaways
- Materialized Views: CREATE TABLE AS SELECT
- Pull Queries: Point-in-time state lookup
- REST API: Serve data to applications
What's Next?
Next Module: 10.8 – ksqlDB in Production