Databricks DE ยท Topic 4.4
Beyond files: capture database changes (CDC), pull REST/JSON APIs, and stream events from Kafka / Azure Event Hubs into Delta. This deep dive walks each with the code.
format("kafka")) โ all landing in Delta.โธ match the source to the right mechanism
readStream.format("kafka") with kafka.bootstrap.servers + subscribe โ deserialize value โ write to Delta. Near real-time, exactly-once via checkpoint.from_json into bronze.subscribe (exact) / subscribePattern (regex) / assign (partitions) select topics. startingOffsets = latest or earliest; maxOffsetsPerTrigger caps batch size so a backlog doesn't overwhelm you.
PySpark
df = (spark.readStream.format("kafka") .option("kafka.bootstrap.servers", "host:9092") .option("subscribe", "events") # or subscribePattern / assign .option("startingOffsets", "latest") # "earliest" to backfill .option("maxOffsetsPerTrigger", 500000) # cap per micro-batch .load())
SQL โ read_kafka streaming TVF
CREATE OR REFRESH STREAMING TABLE cat.sch.bronze_raw AS SELECT * FROM STREAM read_kafka(bootstrapServers => 'host:9092', subscribe => 'events');
โธ toggle to see why you must parse value
Deserialize + keep metadata
from pyspark.sql.functions import col, from_json schema = "user_id INT, event STRING, ts TIMESTAMP" parsed = (df.select( from_json(col("value").cast("string"), schema).alias("d"), # deserialize col("topic"), col("partition"), col("offset"), col("timestamp")) .select("d.*", "topic", "partition", "offset", "timestamp")) # keep offsets for replay
The checkpoint gives exactly-once + restart safety (one checkpoint per stream). Event Hubs is consumed through the same Kafka reader (Kafka protocol). Track lag with avgOffsetsBehindLatest.
(parsed.writeStream .option("checkpointLocation", "/Volumes/cat/sch/vol/_ck_kafka") # unique per stream .trigger(availableNow=True) # batch-style catch-up; omit for always-on .toTable("cat.sch.bronze_events"))
# After landing raw JSON to a Volume, parse incrementally with Auto Loader raw = (spark.readStream.format("cloudFiles") .option("cloudFiles.format", "json") .option("cloudFiles.schemaLocation", "/Volumes/cat/sch/vol/_api_schema") .load("/Volumes/cat/sch/vol/api_landing"))
| Source | Mechanism | Notes |
|---|---|---|
| Files in object storage | Auto Loader / COPY INTO (4.1โ4.2) | incremental file ingest |
| SaaS app / OLTP database | Lakeflow Connect (4.3) | managed connector / CDC |
| Kafka / Event Hubs | Structured Streaming format("kafka") | near real-time events |
| REST / JSON API | land raw โ Auto Loader / from_json | handle auth/pagination |
value into a bronze Delta table in near real-time, while a Lakeflow Connect CDC connector mirrors the orders DB into the same lakehouse.value is binary โ must deserialize; startingOffsets=earliest backfills everything (cap with maxOffsetsPerTrigger); REST needs auth/pagination/rate-limit handling.value (binary) โ garbage columns.earliest offsets โ massive unintended backfill.