IoT Sensor Data & Monitoring
This pattern handles bursty event streams and immediate alerting without long-lived hot retention.
Core pattern
STREAMfor short-lived raw readings (TTL_SECONDS)SHAREDfor persisted rollups/aggregates- live subscriptions for threshold detection
- optional topic worker for downstream alert pipelines
Example schema
CREATE TABLE iot.sensor_data (
event_id BIGINT PRIMARY KEY DEFAULT SNOWFLAKE_ID(),
sensor_id TEXT NOT NULL,
temperature DOUBLE,
humidity DOUBLE,
timestamp TIMESTAMP DEFAULT NOW()
) WITH (TYPE = 'STREAM', TTL_SECONDS = 10);
-- Aggregated metrics (persisted)
CREATE TABLE iot.metrics (
sensor_id TEXT PRIMARY KEY,
avg_temp DOUBLE,
max_temp DOUBLE,
min_temp DOUBLE,
hour TIMESTAMP
) WITH (TYPE = 'SHARED', FLUSH_POLICY = 'interval:3600');Example implementation
await client.subscribeWithSql(
'SELECT * FROM iot.sensor_data WHERE temperature > 80 OR humidity > 95',
(event) => {
if (event.type === 'change' && event.change_type === 'insert') {
triggerAlert(event.rows);
}
}
);Code references
- STREAM runtime behavior: stream_table_provider.rs
- Starter integration scaffold: examples/simple-typescript
Last updated on