스냅샷 구성
스냅샷 구성 (Snapshot configurations)
스냅샷 구성에는 스냅샷 전용 구성과 일반 구성이 있어요. 스냅샷은 dbt_project.yml, 속성 파일, 또는 {{ config() }} 매크로로 구성할 수 있어요.
출처: 문서
본문
관련 문서: Snapshots, dbt snapshot 명령어.
Snapshots에 대한 영상 튜토리얼은 dbt Learn의 Snapshots 코스를 확인해 주세요.
Available configurations
Snapshot-specific configurations
리소스 전용 구성은 여러 리소스 타입이 아닌 dbt 리소스 타입 하나에만 적용돼요. 이 설정은 프로젝트 파일(dbt_project.yml), 속성 파일, 또는 리소스 파일 안에서 {{ config() }} 매크로로 정의할 수 있어요.
다음 리소스 전용 구성은 Snapshots에만 사용할 수 있어요:
dbt_project.yml (dbt v1.9 이상 적용)
snapshots:
<resource-path>:
+schema: <string>
+database: <string>
+alias: <string>
+unique_key: <column_name_or_expression>
+strategy: timestamp | check
+updated_at: <column_name>
+check_cols: [<column_name>] | all
+snapshot_meta_column_names: {<dictionary>}
+dbt_valid_to_current: <string>
+hard_deletes: string
snapshots/schema.yml (dbt v1.9 이상 적용)
snapshots:
- name: <string>
relation: ref() | source()
config:
database: <string>
schema: <string>
unique_key: <column_name_or_expression>
strategy: timestamp | check
updated_at: <column_name>
check_cols: [<column_name>] | all
snapshot_meta_column_names: {<dictionary>}
dbt_valid_to_current: <string>
hard_deletes: string
Snapshot configuration migration
dbt v1.9에서 도입된 최신 스냅샷 구성(예: snapshot_meta_column_names, dbt_valid_to_current, hard_deletes)은 새 스냅샷에 가장 적합하지만, 테이블 스키마와 구성을 신중히 마이그레이션하면 기존 스냅샷에도 적용할 수 있어요. 스냅샷 불일치를 피하려면 주의해야 해요.
방법은 다음과 같아요:
- 데이터 플랫폼에서 백업 스냅샷 테이블을 만들어요. 새 테이블로 복사할 수 있어요:
create table my_snapshot_table_backup as
select * from my_snapshot_table;
이렇게 하면 마이그레이션 중 문제가 생겨도 스냅샷을 복원할 수 있어요.
2. 새 구성을 사용하려면 alter 문으로 기존 스냅샷 테이블에 필요한 컬럼을 추가해요. dbt_valid_to_current와 snapshot_meta_column_names를 사용하려는 경우 추가할 예시는 다음과 같아요:
alter table my_snapshot_table
add column dbt_valid_from timestamp,
add column dbt_valid_to timestamp;
- 그런 다음 스냅샷 구성을 갱신해요:
snapshots:
- name: orders_snapshot
relation: source('something','orders')
config:
strategy: timestamp
updated_at: updated_at
unique_key: id
dbt_valid_to_current: "to_date('9999-12-31')"
snapshot_meta_column_names:
dbt_valid_from: start_date
dbt_valid_to: end_date
- 개발 또는 스테이징에서
dbt snapshot을 실행해 새 구성을 여러 개 적용하기 전에 각 변경을 테스트해요. - 스냅샷 실행이 에러 없이 완료되고, 새 컬럼이 생성되며, 이력 로직이 기대대로 동작하는지 확인해요. 테이블은 다음과 같아야 해요:
|
id|start_date|end_date|updated_at| |---|---|---|---| | 1 | 2024-10-01 09:00:00 | 2024-10-03 08:00:00 | 2024-10-01 09:00:00 | | 2 | 2024-10-03 08:00:00 | 9999-12-31 00:00:00 | 2024-10-03 08:00:00 | | 3 | 2024-10-02 11:15:00 | 9999-12-31 00:00:00 | 2024-10-02 11:15:00 |
참고:
end_date컬럼(snapshot_meta_column_names로 정의)은 새로 삽입된 레코드에 대해 기본NULL대신dbt_valid_to_current에 구성된 값(9999-12-31)을 사용해요. 기존 레코드의end_date는NULL이에요. warning —dbt_valid_to_current같은 최신 구성을 데이터를 마이그레이션하지 않고 사용하면 새 데이터와 옛 데이터가 섞여 다운스트림 결과가 잘못될 수 있어요.
General configurations
일반 구성은 여러 리소스 타입에 적용되는 더 폭넓은 운영 설정을 제공해요. 리소스 전용 구성과 마찬가지로 프로젝트 파일, 속성 파일, 또는 리소스 전용 파일에서 설정할 수 있어요.
dbt_project.yml (dbt v1.9 이상 적용)
snapshots:
<resource-path>:
+enabled: true | false
+tags: <string> | [<string>]
+alias: <string>
+pre-hook: <sql-statement> | [<sql-statement>]
+post-hook: <sql-statement> | [<sql-statement>]
+persist_docs: {<dict>}
+grants: {<dict>}
+event_time: my_time_field
snapshots/properties.yml (dbt v1.9 이상 적용)
snapshots:
- name: [<snapshot-name>]
relation: source('my_source', 'my_table')
config:
enabled: true | false
tags: <string> | [<string>]
alias: <string>
pre_hook: <sql-statement> | [<sql-statement>]
post_hook: <sql-statement> | [<sql-statement>]
persist_docs: {<dict>}
grants: {<dictionary>}
event_time: my_time_field
Configuring snapshots
스냅샷은 여러 방식으로 구성할 수 있어요:
- (dbt v1.9 이상) YAML 파일에서
config리소스 속성으로 정의 — 일반적으로 snapshots 디렉터리 또는 원하는 폴더에. dbt_project.yml파일의snapshots:키 아래 — 스냅샷 또는 스냅샷 디렉터리에 구성을 적용하려면 리소스 경로를 중첩된 딕셔너리 키로 정의해요.
스냅샷 구성은 위 순서대로 계층적으로 적용되며 높은 쪽이 우선해요. tests 속성을 사용해 스냅샷에 데이터 테스트를 적용할 수도 있어요.
Examples
(dbt v1.9 이상 적용) 다음 예시는 dbt_project.yml 파일과 .yml 파일로 스냅샷을 구성하는 방법을 보여줘요.
모든 스냅샷에 구성 적용
설치된 패키지의 스냅샷을 포함한 모든 스냅샷에 구성을 적용하려면 snapshots 키 바로 아래에 중첩해요:
dbt_project.yml
snapshots:
+unique_key: id
프로젝트의 모든 스냅샷에 구성 적용
프로젝트의 모든 스냅샷에만(즉 설치된 패키지의 스냅샷 제외) 구성을 적용하려면 프로젝트 이름을 리소스 경로의 일부로 제공해요.
jaffle_shop 프로젝트의 경우:
dbt_project.yml
snapshots:
jaffle_shop:
+unique_key: id
마찬가지로 설치된 패키지의 이름을 사용해 그 패키지의 스냅샷을 구성할 수도 있어요.
스냅샷 하나에만 구성 적용
(dbt v1.9 이상 적용)
snapshots/postgres_app/order_snapshot.yml
snapshots:
- name: orders_snapshot
relation: source('jaffle_shop', 'orders')
config:
unique_key: id
strategy: timestamp
updated_at: updated_at
persist_docs:
relation: true
columns: true
프로 팁: 스냅샷에 소스를 사용해 보세요: select * from {{ source('jaffle_shop', 'orders') }}
dbt_project.yml 파일에서 전체 리소스 경로(프로젝트 이름과 하위 디렉터리 포함)를 사용해 개별 스냅샷을 구성할 수도 있어요.
jaffle_shop 프로젝트에서 snapshots/postgres_app/ 디렉터리에 orders_snapshot이라는 스냅샷(위와 동일)이 있다면 다음과 같아요:
dbt_project.yml
snapshots:
jaffle_shop:
postgres_app:
orders_snapshot:
+unique_key: id
+strategy: timestamp
+updated_at: updated_at
스냅샷의 config 블록에서도 공통 구성을 정의할 수 있지만, 스냅샷의 필수 구성에는 권장하지 않아요.
dbt_project.yml
snapshots:
- name: orders_snapshot
+persist_docs:
relation: true
columns: true
더 알아보기 (Learn more)
- Snapshots — 스냅샷 개념
- Snapshot properties — 스냅샷 속성
- Timestamps and check cols — 스냅샷 전략