anchors
anchors
앵커(anchor)는 단일 YAML 파일 안에서 설정 블록을 재사용할 수 있게 해주는 YAML 기능이에요. dbt v1.10에서 anchors: 키가 도입되어, 단독으로는 유효하지 않거나 템플릿 데이터로만 존재하는 설정 조각을 담을 수 있어요. 이를 통해 파일 검증 중 그 조각들이 거부되지 않도록 보장해요.
출처: 문서
본문
Definition
앵커는 단일 YAML 파일 안에서 설정 블록을 재사용하게 해주는 YAML 기능이에요. dbt v1.10에서 anchors: 키가 도입되어, 단독으로는 유효하지 않거나 템플릿 데이터로만 존재하는 설정 조각을 둘 수 있어요. anchors: 키를 쓰면 이 조각들이 파일 검증 중 거부되지 않아요.
dbt v1.10 이상에서는 잘못된 앵커가 경고를 일으키고, dbt v2에서는 오류가 돼요.
참고: dbt v1.9 이하에서도 앵커를 정의할 수 있지만, 이 버전들에는 앵커 전용 위치가 없어요. 독립 앵커를 정의해야 한다면 YAML 파일 최상위에 둘 수 있어요.
YAML anchor 문법
Anchors와 aliases
YAML 앵커를 정의하려면 YAML 파일에 anchors: 블록을 추가하고 앵커 이름 앞에 & 기호를 붙여요(예: &id_column_alias). 그러면 별칭(alias)이 생기는데, 다른 곳에서 이 별칭을 * 문자로 접두 붙여 참조할 수 있어요.
다음 예시는 별칭이 *id_column_alias인 앵커를 만들어요. id 컬럼, 그 설명, 데이터 타입, 데이터 테스트가 모두 my_first_model, my_second_model, my_third_model에 적용돼요.
models/_models.yml
anchors:
- &id_column_alias
name: id
description: This is a unique identifier.
data_type: int
data_tests:
- not_null
- unique
models:
- name: my_first_model
columns:
- *id_column_alias
- name: unrelated_column_a
description: This column is not repeated in other models.
- name: unrelated_column_b
- name: my_second_model
columns:
- *id_column_alias
- name: unrelated_column_c
- name: my_third_model
columns:
- *id_column_alias
- name: unrelated_column_d
내부적으로 별칭은 앵커가 정의한 객체로 치환돼요.
Merge 문법
때로는 앵커가 대부분 같지만 한 부분만 덮어써야 할 때가 있어요. 앵커가 딕셔너리/매핑(리스트나 스칼라 값이 아닌)을 가리킬 때 <<: 병합 키를 쓸 수 있어요.
models/_models.yml
anchors:
- &id_column_alias
name: id
description: This is a unique identifier.
data_type: int
data_tests:
- not_null
- unique
- &source_template_alias
database: RAW
loader: fivetran
config:
freshness:
warn_after: {count: 1, period: day}
models:
- name: my_first_model
columns:
- *id_column_alias # brings in the full anchor defined above
- name: unrelated_column_a
description: This column is not repeated in other models.
- name: unrelated_column_b
- name: my_second_model
columns:
- <<: *id_column_alias
data_type: bigint # overrides the data_type from int to bigint, while inheriting the name, description, and data tests
- name: unrelated_column_c
- name: my_third_model
columns:
- <<: *id_column_alias
config:
meta:
extra_key: extra_value # adds config.meta.extra_key to just this version of the id column, in addition to the name, description, data type, and data tests
- name: unrelated_column_d
sources:
# both sources start with their database, loader, and freshness expectations set from the anchor, and merge in additional keys
- <<: *source_template_alias
name: salesforce
schema: etl_salesforce_schema
tables:
- name: opportunities
- name: users
- <<: *source_template_alias
name: hubspot
schema: etl_hubspot_schema
tables:
- name: contacts
Usage notes
-
dbt의 이전 버전(v1.9 이하)에는 전용
anchors:키가 없어요. 독립 앵커를 정의해야 한다면 파일 최상위에 둘 수 있어요. -
앵커로 정의된 리스트에는 추가 요소를 병합할 수 없어요. 예를 들어 여러 컬럼을 포함하는 앵커를 정의했다면, 리스트 끝에 컬럼을 추가로 붙일 수 없어요. 대신 각 컬럼을 개별 앵커로 정의하고 각각을 해당 테이블에 추가하세요.
-
이미 더 큰 유효한 YAML 객체 안에 정의된 앵커는
anchors:키로 옮길 필요가 없어요. 예를 들어 다음&customer_id_tests앵커는 기존columns블록의 유효한 부분이라 옮길 필요가 없어요.
models:
- name: my_first_model
columns:
- name: customer_id
data_tests: &customer_id_tests
- not_null
- unique
- name: order_id
data_tests: *customer_id_tests
더 알아보기 (Learn more)
- Merlin's YAML anchors 가이드 — YAML 앵커/별칭 문법.
- dbt v1.10 릴리스 노트 —
anchors:키 도입.