AWS Glue 설정

AWS Glue 설정

dbt-glue 어댑터에서 table 구성과 incremental 전략(append, insert_overwrite, merge)을 다루는 페이지예요. 많은 부분이 dbt-spark 플러그인에서 상속받아요.

출처: 문서

본문

테이블 구성 (Configuring tables)

모델을 table로 materialize할 때, Apache Spark 모델 구성 외에도 dbt-glue 플러그인 특유의 여러 선택적 config를 포함할 수 있어요.

옵션 설명 필수 여부? 예시
custom_location 기본적으로 어댑터는 location path/database/table 경로에 데이터를 저장해요. 이 기본 동작을 원하지 않으면 이 파라미터로 S3의 커스텀 위치를 설정할 수 있어요 아니요 s3://mycustombucket/mycustompath

Incremental 모델 (Incremental models)

dbt는 내장 구성과 materialization을 통해 유용하고 직관적인 모델링 추상화를 제공하려 해요.

그래서 dbt-glue 플러그인은 incremental_strategy config를 많이 활용해요. 이 config는 incremental materialization이 첫 실행 이후의 실행에서 모델을 어떻게 빌드할지 알려줘요. 다음 세 값 중 하나로 설정할 수 있어요:

  • append (기본값): 기존 데이터를 업데이트하거나 덮어쓰지 않고 새 레코드를 삽입해요.
  • insert_overwrite: partition_by가 지정되면 테이블의 파티션을 새 데이터로 덮어써요. partition_by가 없으면 테이블 전체를 새 데이터로 덮어써요.
  • merge (Apache Hudi 전용): unique_key를 기준으로 레코드를 매칭해요. 이전 레코드를 업데이트하고 새 레코드를 삽입해요. (unique_key를 지정하지 않으면 append와 비슷하게 모든 새 데이터를 삽입해요.)

이 각각의 전략에는 장단점이 있으며 아래에서 논의할게요. 다른 모델 config와 마찬가지로, incremental_strategydbt_project.yml 또는 모델 파일의 config() 블록 안에서 지정할 수 있어요.

참고: 기본 전략은 **insert_overwrite**예요.

append 전략

append 전략을 따르면 dbt는 모든 새 데이터로 insert into 문을 실행해요. 이 전략의 장점은 모든 플랫폼, 파일 유형, 연결 방법, Apache Spark 버전에서 단순하고 동작한다는 거예요. 하지만 이 전략은 기존 데이터를 업데이트, 덮어쓰기, 삭제할 수 없어서, 많은 데이터 소스에서 중복 레코드를 삽입할 가능성이 있어요.

소스 코드

glue_incremental.sql

{{ config(
    materialized='incremental',
    incremental_strategy='append',
) }}

--  All rows returned by this query will be appended to the existing table

select * from {{ ref('events') }}
{% if is_incremental() %}
  where event_ts > (select max(event_ts) from {{ this }})
{% endif %}

실행 코드

glue_incremental.sql

create view spark_incremental__dbt_tmp as

    select * from analytics.events

    where event_ts >= (select max(event_ts) from {{ this }})

;

insert into table analytics.spark_incremental
    select `date_day`, `users` from spark_incremental__dbt_tmp

;

drop view spark_incremental__dbt_tmp

insert_overwrite 전략

이 전략은 모델 config에 partition_by 절과 함께 지정할 때 가장 효과적이에요. dbt는 쿼리에 포함된 모든 파티션을 동적으로 교체하는 원자적 insert overwrite을 실행해요. 이 incremental 전략을 사용할 때는 파티션의 모든 관련 데이터를 다시 선택해야 해요.

partition_by를 지정하지 않으면 insert_overwrite 전략은 테이블의 모든 내용을 원자적으로 교체해, 기존 데이터를 새 레코드로만 덮어써요. 하지만 테이블의 컬럼 스키마는 그대로 유지돼요. 이는 테이블 내용이 덮어써지는 동안 다운타임을 최소화하므로 일부 제한된 상황에서 바람직할 수 있어요. 다른 데이터베이스에서 truncate + insert를 실행하는 것과 비슷한 연산이에요. Delta 형식 테이블의 원자적 교체에는 table materialization(create or replace 실행)을 사용하세요.

소스 코드

spark_incremental.sql

{{ config(
    materialized='incremental',
    partition_by=['date_day'],
    file_format='parquet'
) }}

/*
  Every partition returned by this query will be overwritten
  when this model runs
*/

with new_events as (

    select * from {{ ref('events') }}

    {% if is_incremental() %}
    where date_day >= date_add(current_date, -1)
    {% endif %}

)

select
    date_day,
    count(*) as users

from events
group by 1

실행 코드

spark_incremental.sql

create view spark_incremental__dbt_tmp as

    with new_events as (

        select * from analytics.events


        where date_day >= date_add(current_date, -1)


    )

    select
        date_day,
        count(*) as users

    from events
    group by 1

;

insert overwrite table analytics.spark_incremental
    partition (date_day)
    select `date_day`, `users` from spark_incremental__dbt_tmp

;

drop view spark_incremental__dbt_tmp

insert_overwrite를 incremental 전략으로 지정하는 것은 선택 사항이에요. 지정하지 않았을 때 사용되는 기본 전략이니까요.

merge 전략

사용 참고: merge incremental 전략에는 다음이 필요해요:

  • file_format: hudi
  • extra jars로 hudi 라이브러리를 가진 AWS Glue runtime 2

profiles.yml의 extra_jars 옵션을 사용해 클래스패스에 hudi 라이브러리를 extra jars로 추가할 수 있어요. 예시:

extra_jars: "s3://dbt-glue-hudi/Dependencies/hudi-spark.jar,s3://dbt-glue-hudi/Dependencies/spark-avro_2.11-2.4.4.jar"

dbt는 Snowflake와 BigQuery의 기본 merge 동작과 거의 동일하게 보이는 원자적 merge을 실행해요. unique_key가 지정되면(권장), dbt는 키 컬럼에서 매칭되는 새 레코드의 값으로 이전 레코드를 업데이트해요. unique_key를 지정하지 않으면 dbt는 매칭 기준을 생략하고 단순히 모든 새 레코드를 삽입해요(append 전략과 비슷).

소스 코드

hudi_incremental.sql

{{ config(
    materialized='incremental',
    incremental_strategy='merge',
    unique_key='user_id',
    file_format='hudi'
) }}

with new_events as (

    select * from {{ ref('events') }}

    {% if is_incremental() %}
    where date_day >= date_add(current_date, -1)
    {% endif %}

)

select
    user_id,
    max(date_day) as last_seen

from events
group by 1

모델 설명 영속화 (Persisting model descriptions)

관계 레벨 docs 영속화는 dbt-spark에서 상속받았어요. 자세한 내용은 Apache Spark 모델 구성을 참고하세요.

항상 schema, 절대 database 아님

이 섹션도 dbt-spark에서 상속받았어요. 자세한 내용은 Apache Spark 모델 구성을 참고하세요.

더 알아보기 (Learn more)

  • incremental 전략에 대한 자세한 설명은 incremental-strategy 문서를 참고하세요.
  • dbt-spark 모델 구성에서 기타 옵션을 확인하세요: spark-configs.