dbt(data build tool)는 데이터 분석가와 엔지니어가 SQL로 데이터 변환 파이프라인을 관리하는 오픈소스 도구다. ELT 패러다임에서 "T(Transform)" 단계를 담당하며, 소프트웨어 엔지니어링 모범 사례(테스트, 버전 관리, 문서화)를 데이터 작업에 도입한다.
핵심 특징
모델: SQL SELECT 문 → 테이블/뷰 자동 생성
테스트: 데이터 품질 자동 검증
문서화: 자동 lineage 그래프, 메타데이터
재사용: 매크로(Jinja 템플릿), 패키지
dbt 모델 작성
sql
-- models/staging/stg_orders.sql
with source as (
select * from {{ source('raw', 'orders') }}
),
renamed as (
select
id as order_id,
user_id,
status,
created_at::date as order_date,
amount / 100.0 as amount_usd
from source
where status != 'deleted'
)
select * from renamed
sql
-- models/marts/fct_monthly_revenue.sql
{{ config(materialized='table') }}
with orders as (
select * from {{ ref('stg_orders') }}
),
revenue as (
select
date_trunc('month', order_date) as month,
sum(amount_usd) as total_revenue,
count(distinct user_id) as unique_customers
from orders
where status = 'completed'
group by 1
)
select * from revenue
테스트 및 실행
yaml
# models/schema.yml
models:
- name: stg_orders
columns:
- name: order_id
tests:
- unique
- not_null
- name: amount_usd
tests:
- not_null
- dbt_utils.expression_is_true:
expression: ">= 0"
bash
dbt run # 모델 실행
dbt test # 테스트 실행
dbt docs generate && dbt docs serve # 문서 생성
관련 개념