dbt streaming tables
Welcome! I am excited to help you master dbt and Databricks Streaming Tables. Before we jump in, let's make sure this session is perfectly tailored to your current workflow.
To get us started, could you share a bit about your experience level with dbt and Databricks, and what specifically brings you to streaming tables today?
I know dbt well, but I'm new to Databricks streaming.
It's great to have you here! Since you already know inside and out, we can skip the basics of modeling and focus exactly on how Databricks handles continuous data streams under the hood.
What is a Streaming Table?
How dbt Interfaces with Streaming Tables
dbt-databricks adapter, you can use a native materialization specifically designed for this pattern:
materialized='streaming_table'
When you use this configuration, dbt issues a special statement directly to Databricks to spin up and register the streaming table. Every subsequent run will incrementally fetch new rows from your source files or message queues.Show me a dbt model example using this materialization
To show you exactly how this fits together, let's look at a concrete dbt model using the streaming_table materialization.
The Code: Ingesting Raw JSON Streams
{{
config(
materialized='streaming_table'
)
}}
SELECT
id AS trip_id,
origin,
destination,
dep_delay,
-- 1. Capturing the source file name
_metadata.file_path AS source_file,
-- 2. Capturing the exact ingestion time
_metadata.file_modification_time AS file_timestamp
FROM
stream(read_files(
'/Volumes/raw_data/flights/incoming_json',
format => 'json'
))
Unpacking the Streaming Magic
-
The
stream()Wrapper andread_files(): Instead of referencing a normal table or view, we querystream(read_files(...)). The table-valued function leverages Databricks' Auto Loader engine to automatically detect when new files land, infer their schema, and pull them in. Wrapping it instream()tells Databricks that this is a continuous, incremental feed rather than a one-time batch read. -
The
_metadataColumn: When you read files usingread_files, Databricks implicitly attaches a virtual column named_metadata. This is incredibly valuable for auditing because it lets you extract the exact path of the file that supplied the row (using_metadata.file_path) and when that file was modified (using_metadata.file_modification_time). This happens without having to write custom file-parsing scripts.