dbt 소스

중급 dbt

Mike Metzger

Data Engineer

dbt 소스란?

  • EL 프로세스로 적재된 데이터의 이름과 설명
  • 데이터 라인리지 정의에 도움
  • 테스트
  • 문서화

데이터 소스

1 Photo by Mika Baumeister on Unsplash
중급 dbt

소스

  • 데이터 라인리지 정보 제공
  • 웨어하우스 내 데이터 흐름 설명
  • Jinja {{ source() }} 함수 사용
    • {{ ref() }}와 유사
  • 원시 데이터 접근을 단순화
select * 
from
  {{ source('raw', 'orders') }}
중급 dbt

소스 정의

  • .yml 파일에 정의
  • 예: models/model_properties.yml
    • 또는 디렉터리 내 다른 .yml 파일
  • sources: 섹션 아래에 작성
  • - name: 옵션으로 소스 이름 지정
  • tables: 아래 각 테이블을 - name:으로 정의
  • 웨어하우스 유형별 옵션 다름. dbt 문서 참고
version: 2

sources:
  - name: raw
    tables:
      - name: phone_orders
      - name: web_orders
중급 dbt

소스 접근

  • {{ source() }} 사용
  • {{ source(source_name, table_name) }}
  • 컴파일된 쿼리에서 올바른 테이블 이름 제공
select * from
  {{ source('raw', 'phone_orders') }}
UNION
select * from
  {{ source('raw', 'web_orders') }}
-- dbt compiled
select * from
  'raw'.'phone_orders'
UNION
select * from
  'raw'.'web_orders'
중급 dbt

소스 테스트

  • 소스에 테스트 적용 가능
  • 모델에 적용하는 방법과 동일
  • models: 대신 sources: 섹션에 정의
  • 소스를 정의한 .yml 파일에 작성
version: 2

sources:
  - name: raw
    tables:
      - name: phone_orders
        columns:
          - name: id
            tests:
              - not_null
              - unique
      - name: web_orders
중급 dbt

Ayo berlatih!

중급 dbt

Preparing Video For Download...