dbt sources

dbt ระดับกลาง

Mike Metzger

Data Engineer

dbt source คืออะไร?

  • ชื่อและคำอธิบายข้อมูลที่โหลดโดยกระบวนการ EL
  • ช่วยกำหนด data lineage
  • การทดสอบ
  • เอกสารประกอบ

แหล่งข้อมูล

1 Photo by Mika Baumeister on Unsplash
dbt ระดับกลาง

Sources

  • ให้ข้อมูล data lineage
  • อธิบายการไหลของข้อมูลใน warehouse
  • ใช้ฟังก์ชัน Jinja {{ source() }}
    • คล้ายกับฟังก์ชัน {{ ref() }}
  • ช่วยให้เข้าถึงข้อมูลดิบได้ง่ายขึ้น
select * 
from
  {{ source('raw', 'orders') }}
dbt ระดับกลาง

การกำหนด source

  • กำหนดในไฟล์ .yml
  • ไฟล์อาจเป็น models/model_properties.yml
    • หรือไฟล์ .yml อื่น ๆ ในไดเรกทอรี
  • ในส่วน sources:
  • ตั้งชื่อ source ด้วยตัวเลือก - name:
  • กำหนดตารางแต่ละตารางด้วยตัวเลือก - name: ใต้ tables:
  • มีตัวเลือกอื่น ๆ ขึ้นอยู่กับประเภท data warehouse โปรดดูเอกสาร dbt
version: 2

sources:
  - name: raw
    tables:
      - name: phone_orders
      - name: web_orders
dbt ระดับกลาง

การเข้าถึง sources

  • ใช้ {{ 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 ระดับกลาง

การทดสอบ sources

  • สามารถกำหนดการทดสอบกับ sources ได้
  • ใช้วิธีเดียวกับการทดสอบโมเดล
  • กำหนดในส่วน sources: แทน models:
  • ในไฟล์ .yml ที่กำหนด sources ไว้
version: 2

sources:
  - name: raw
    tables:
      - name: phone_orders
        columns:
          - name: id
            tests:
              - not_null
              - unique
      - name: web_orders
dbt ระดับกลาง

มาฝึกกันเถอะ!

dbt ระดับกลาง

Preparing Video For Download...