Branching

Apache Airflow เบื้องต้นด้วย Python

Mike Metzger

Data Engineer

Branching

  • ให้ตรรกะเงื่อนไข (เช่น if -> then สำหรับงาน)
  • ใช้ @task.branch

  • รันฟังก์ชัน Python เพื่อคืนค่า task id ถัดไป (หรือรายการ id)

Apache Airflow เบื้องต้นด้วย Python

ตัวอย่าง Branching

@task.branch
def branch_task(logical_date):
  if int(logical_date.month) % 3 == 0:
    # Months 3 - March, 6 - June, 9 - September, 12 - December
    return 'end_of_quarter_task'
  else:
    # All other months
    return 'regular_monthly_task'
Apache Airflow เบื้องต้นด้วย Python

ตัวอย่าง Branching

@task.branch
def branch_task(logical_date):
  if int(logical_date.month) % 3 == 0:
    # Months 3 - March, 6 - June, 9 - September, 12 - December
    return 'end_of_quarter_task'
  else:
    # All other months
    return 'regular_monthly_task'

start_task >> branch_task >> end_of_quarter_task >> end_of_quarter_task2
branch_task >> regular_monthly_task >> regular_monthly_task2
Apache Airflow เบื้องต้นด้วย Python

มุมมองกราฟ Branching

มุมมองกราฟ Airflow ของ DAG แบบ Branching ที่มีสองเส้นทางงานขาออก

Apache Airflow เบื้องต้นด้วย Python

Branching — เดือนสิ้นไตรมาส

มุมมองกราฟ Airflow ที่รันงานสิ้นไตรมาสและข้ามงานรายเดือน

Apache Airflow เบื้องต้นด้วย Python

Branching — เดือนปกติ

มุมมองกราฟ Airflow ที่รันงานรายเดือนปกติและข้ามงานสิ้นไตรมาส

Apache Airflow เบื้องต้นด้วย Python

ตัวแปรวันที่

  • ds - วันที่เชิงตรรกะแบบมีขีด YYYY-MM-DD
  • ds_nodash - วันที่เชิงตรรกะแบบไม่มีขีด YYYYMMDD
  • prev_data_interval_start_success - วันที่ของการรัน DAG ที่สำเร็จครั้งล่าสุด
  • และอื่น ๆ อีกมาก — ดูเอกสาร Airflow variables
1 https://airflow.apache.org/docs/apache-airflow/stable/templates-ref.html#templates-variables
Apache Airflow เบื้องต้นด้วย Python

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

Apache Airflow เบื้องต้นด้วย Python

Preparing Video For Download...