30 Mins

1. Introduction to dbt

Defining Analytics Engineering and dbt Fundamentals

The role of the Analytics Engineer has emerged as a bridge between Data Engineering and Data Analysis. While Data Engineers focus on pipeline infrastructure and Data Analysts focus on business insights, Analytics Engineers leverage software engineering best practices—such as version control, automated testing, and modularity—to manage the transformation layer of the data warehouse. dbt (data build tool) is the industry-standard framework enabling this workflow.

dbt allows users to write modular SQL queries as 'models,' which are essentially SELECT statements. dbt handles the boilerplate code required to materialize these models as tables or views in your data warehouse. By separating the transformation logic from the database connection management, dbt enables clean, maintainable codebases that follow the DRY (Don't Repeat Yourself) principle.

Operator Note: Analytics Engineering is defined by the transformation of raw data into clean, business-ready models, shifting the focus from 'moving data' to 'modeling data'.
Bash
# To initialize a new project, use the dbt CLI
dbt init my_analytics_project
# This creates the necessary directory structure for models, tests, and configurations

Project Architecture: Configuration and Profiles

A dbt project is structured around specific files that govern its operation. The dbt_project.yml file acts as the primary configuration file, defining project variables, model configurations, and directory structure. It informs dbt how to identify files as models or tests within the project hierarchy. By setting materialization defaults here, you can dictate if your models are built as tables, views, or incremental loads.

The profiles.yml file is where connection details reside. Crucially, this file is never committed to version control because it contains sensitive database credentials. By defining your warehouse targets in this file, you can switch between development, staging, and production environments seamlessly without modifying your model code.

The project layout is strictly defined: models/ holds your SQL files, seeds/ holds your static CSV data, and macros/ contains reusable SQL snippets that prevent code duplication across your warehouse.

Operator Note: Always keep your 'profiles.yml' file outside the project directory in your ~/.dbt/ folder to ensure credentials are not accidentally pushed to GitHub or other repositories.
Text
# dbt_project.yml configuration example
name: 'my_project'
version: '1.0.0'
profile: 'my_profile'
models:
  my_project:
    staging:
      +materialized: view
    marts:
      +materialized: table

The Compiler Model and Execution Lifecycle

At the core of dbt is its compilation engine. When you execute a command, dbt parses your SQL models, which contain Jinja2 template tags. Jinja2 enables dynamic SQL, allowing you to reference other models using the ref() function. The compiler evaluates these functions and generates the final, executable DDL/DML statements—like CREATE TABLE AS or INSERT INTO—that are then sent to your data warehouse.

The distinction between dbt compile and dbt run is critical for debugging. 'dbt compile' merely translates your Jinja-templated SQL into pure, executable SQL saved in the target/ folder without interacting with the database. 'dbt run,' conversely, executes the compiled code against your connection, performing the actual materialization within your warehouse.

Operator Note: Always use the ref() function rather than hardcoding table names. This allows dbt to build a directed acyclic graph (DAG) of your project, ensuring models are built in the correct dependency order.
SQL
-- Example of a dbt model file: models/orders.sql
-- The ref function creates dependency graph lineage
SELECT
  order_id,
  customer_id,
  sum(amount) as total_revenue
FROM {{ ref('stg_payments') }}
GROUP BY 1, 2

Knowledge Check

Ready to test your understanding of 1. Introduction to dbt?

Back