5. dbt Models¶
In this step you'll create two dbt models that transform the raw Postgres staging data into clean analytical tables.
dbt project structure¶
After degen install, your dbt project lives at sales_pipeline/. The structure:
sales_pipeline/ ← dbt project root
├── dbt_project.yml
├── models/
│ └── example/ ← generated examples (you can delete these)
└── tests/
You'll create two new models:
models/
staging/
stg_sales.sql ← clean up the raw Postgres table
stg_sales.yml ← schema + tests
marts/
sales_summary.sql ← aggregate by product and region
sales_summary.yml ← schema + tests
Create the staging model¶
Create sales_pipeline/models/staging/stg_sales.sql:
Create sales_pipeline/models/staging/stg_sales.yml:
Create the mart model¶
Create sales_pipeline/models/marts/sales_summary.sql:
Create sales_pipeline/models/marts/sales_summary.yml:
| sales_pipeline/models/marts/sales_summary.yml | |
|---|---|
Update dbt_project.yml¶
Tell dbt to materialize staging as views and marts as tables:
| sales_pipeline/dbt_project.yml | |
|---|---|
Run dbt manually¶
Test both models before Airflow runs them:
18:42:01 1 of 2 START sql view model public.stg_sales .................. [RUN]
18:42:01 1 of 2 OK created sql view model public.stg_sales ............. [CREATE VIEW in 0.41s]
18:42:01 2 of 2 START sql table model public.sales_summary .............. [RUN]
18:42:02 2 of 2 OK created sql table model public.sales_summary ......... [SELECT 200 in 0.58s]
18:42:02 Done. PASS=2 WARN=0 ERROR=0 SKIP=0 TOTAL=2
Run the tests:
18:42:05 1 of 8 PASS not_null_stg_sales_sale_date
18:42:05 2 of 8 PASS not_null_stg_sales_product
18:42:05 3 of 8 PASS accepted_values_stg_sales_region
18:42:05 4 of 8 PASS not_null_stg_sales_revenue
18:42:05 5 of 8 PASS not_null_sales_summary_sale_date
18:42:05 6 of 8 PASS not_null_sales_summary_product
18:42:05 7 of 8 PASS not_null_sales_summary_total_revenue
18:42:05 Done. PASS=7 WARN=0 ERROR=0
Browse the lineage graph¶
Open http://localhost:8081 and click on sales_summary in the lineage graph. You'll see the full chain:
Click any model to see its compiled SQL, column descriptions, and test results.
View data in pgAdmin¶
Open http://localhost:5050 → DEGEN Postgres → degen → Schemas → public → Tables.
You'll see:
- staging_sales — 200 raw rows from PySpark
- sales_summary — ~20 rows (200 rows aggregated by product × region × date)