SCD2 cu snapshot-uri dbt

dbt intermediar

Mike Metzger

Data Engineer

Ce este un snapshot?

  • O privire asupra modificărilor unui set de date în timp
  • Ilustrează stările unui obiect, cum ar fi
    • Statusul comenzii
    • Starea de producție
    • Statusul livrării

Comandă

1 Photo by micheile henderson on Unsplash
dbt intermediar

SCD2

  • Dimensiune cu modificare lentă
  • Urmărește modificările în timp
  • dbt implementează SCD2 cu snapshot-uri

Date în schimbare

1 Photo by Luke Chesser on Unsplash
dbt intermediar

Exemplu SCD2

  • Statusul comenzii
  • Stări disponibile:
    • Primită
    • Ambalată
    • Expediată
id order_status last_updated
1 Shipped 2023-07-01 11:30
id order_status last_updated
1 Received 2023-07-01 10:45
1 Packed 2023-07-01 11:15
1 Shipped 2023-07-01 11:30
dbt intermediar

SCD2 în dbt

  • dbt folosește snapshot-uri pentru a implementa SCD2
  • Poate urmări modificările automat
  • Adaugă coloane suplimentare în output
    • dbt_valid_from
    • dbt_valid_to
id order_status last_updated dbt_valid_from dbt_valid_to
1 Received 2023-07-01 10:45 2023-07-01 10:45 2023-07-01 11:15
1 Packed 2023-07-01 11:15 2023-07-01 11:15 2023-07-01 11:30
1 Shipped 2023-07-01 11:30 2023-07-01 11:30 null
dbt intermediar

Implementarea snapshot-urilor dbt

  • Fișier SQL, snapshots/snapshot_name.sql
{% snapshot snapshot_orders %}

{{ config(
target_schema='snapshots',
strategy='timestamp',
unique_key='id',
updated_at='last_updated'
) }}
select * from {{ source('raw', 'orders') }}
{% endsnapshot %}
dbt intermediar

dbt snapshot

  • Rulați dbt snapshot
  • Creați un model nou folosind comanda ref() pentru a interoga snapshot-ul
    • select * from {{ ref('snapshot_orders') }}
  • Rulați dbt snapshot frecvent pentru a detecta modificările
    • Planificați actualizări automate
dbt intermediar

Să exersăm!

dbt intermediar

Preparing Video For Download...