id: clickhouse-event-ingestion-ttl-rollup
namespace: company.team
description: |
Bulk-load a batch of real-time events into ClickHouse using asynchronous inserts, keep an
hourly rollup current automatically through a materialized view, and force the table's TTL
to expire rows older than 7 days on every run instead of waiting on ClickHouse's own
background merge schedule.
inputs:
- id: events_file
type: FILE
description: Batch of newline-delimited JSON event records to ingest, for
example a file landed by an upstream Kafka Connect sink or a scheduled
export job.
tasks:
- id: ensure_schema
type: io.kestra.plugin.jdbc.clickhouse.Queries
description: Idempotently create the raw events table with a 7 day TTL, an
aggregating rollup table, and a materialized view that keeps the rollup
current as events land.
url: "{{ secret('CLICKHOUSE_URL') }}"
username: "{{ secret('CLICKHOUSE_USERNAME') }}"
password: "{{ secret('CLICKHOUSE_PASSWORD') }}"
transaction: false
sql: |
CREATE TABLE IF NOT EXISTS raw_events
(
event_id String,
device_id String,
event_type String,
event_time DateTime,
value Float64
)
ENGINE = MergeTree()
PARTITION BY toYYYYMMDD(event_time)
ORDER BY (device_id, event_time)
TTL event_time + INTERVAL 7 DAY DELETE;
CREATE TABLE IF NOT EXISTS events_rollup
(
event_hour DateTime,
event_type String,
event_count AggregateFunction(count, UInt64),
value_sum AggregateFunction(sum, Float64)
)
ENGINE = AggregatingMergeTree()
ORDER BY (event_hour, event_type);
CREATE MATERIALIZED VIEW IF NOT EXISTS events_rollup_mv
TO events_rollup
AS SELECT
toStartOfHour(event_time) AS event_hour,
event_type,
countState() AS event_count,
sumState(value) AS value_sum
FROM raw_events
GROUP BY event_hour, event_type;
- id: events_to_ion
type: io.kestra.plugin.serdes.json.JsonToIon
description: Convert the landed NDJSON batch to ION so it can be bulk-loaded
with a JDBC batch insert.
from: "{{ inputs.events_file }}"
- id: ingest_events
type: io.kestra.plugin.jdbc.clickhouse.BulkInsert
description: Bulk-insert the batch into raw_events in JDBC batches of 5,000 rows
using ClickHouse's asynchronous insert mode; the materialized view keeps
events_rollup current automatically, so no separate aggregation step runs
after ingestion.
url: "{{ secret('CLICKHOUSE_URL') }}"
username: "{{ secret('CLICKHOUSE_USERNAME') }}"
password: "{{ secret('CLICKHOUSE_PASSWORD') }}"
from: "{{ outputs.events_to_ion.uri }}"
chunk: 5000
sql: |
INSERT INTO raw_events (event_id, device_id, event_type, event_time, value)
SETTINGS async_insert = 1, wait_for_async_insert = 1
VALUES (?, ?, ?, ?, ?)
- id: enforce_ttl
type: io.kestra.plugin.jdbc.clickhouse.Queries
description: Force ClickHouse to apply the table's TTL immediately, so rows past
the 7 day retention window are dropped as part of this run instead of
waiting on the next background merge.
url: "{{ secret('CLICKHOUSE_URL') }}"
username: "{{ secret('CLICKHOUSE_USERNAME') }}"
password: "{{ secret('CLICKHOUSE_PASSWORD') }}"
transaction: false
sql: OPTIMIZE TABLE raw_events FINAL;
- id: preview_rollup
type: io.kestra.plugin.jdbc.clickhouse.Query
description: Read back the last 24 hourly rollups by merging the
AggregatingMergeTree state, and store the result for downstream consumers.
url: "{{ secret('CLICKHOUSE_URL') }}"
username: "{{ secret('CLICKHOUSE_USERNAME') }}"
password: "{{ secret('CLICKHOUSE_PASSWORD') }}"
sql: |
SELECT
event_hour,
event_type,
countMerge(event_count) AS events,
round(sumMerge(value_sum), 2) AS total_value
FROM events_rollup
GROUP BY event_hour, event_type
ORDER BY event_hour DESC
LIMIT 24;
fetchType: STORE
errors:
- id: alert_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Alert Slack if schema setup, ingestion, TTL enforcement, or the
rollup read fails.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"text": "ClickHouse event ingestion failed for execution {{ execution.id }}."
}