DEV Community

Tanakrit Seangnet
Tanakrit Seangnet

Posted on • Edited on

3 1

Apache Airflow:Trigger DAG with REST API

Airflow ได้ยกเลิกการใช้งาน Experimental Rest API ไปตั้งแต่ v2.0.0 ด้วยเหตุผลว่า Experimental ไม่มีการควบคุมการเข้าถึง จึงได้เกิด Airflow API (Stable) (1.0.0) ขึ้นมา ดูเพิ่มเติม

มาทดลองใช้กัน

สิ่งที่ต้องมี

  • Postman

เปิดใช้งาน Authenticate

แก้ไข auth_backend ใน airflow.cfg

[api]
auth_backend = airflow.api.auth.backend.basic_auth
Enter fullscreen mode Exit fullscreen mode

เราจำเป็นจะต้องเปิดการใช้ Authen ไม่งั้น API v1 จะปฏิเสธการร้องขอ (PermissionDenied) อย่าลืม restart ด้วย

ระบุ URL ลงใน Postman

ในกรณีที่ URL ของเราเป็น Localhost ในการ Trigger จะใช้ HTTP Methods:POST

http://localhost:8080/api/v1/dags/{DAG_ID}/dagRuns
Enter fullscreen mode Exit fullscreen mode

Screenshot (478)

กำหนด Authorization

ระบุ Type เป็น Basic Auth และ Username/Password ต้องถูกสร้างอยู่ใน airflow
Screenshot (479)

กำหนด Headers

KEY: Content-Type VALUE: application/json
Screenshot (480)

กำหนด Body

ระบุเป็น raw ประเภท JSON ใส่ค่า {}
Screenshot (481)

Response

ถ้าไม่มีการทำงานที่ผิดพลาดได้จะรับ Response

{
    "conf": {},
    "dag_id": "covid19_daily",
    "dag_run_id": "manual__2021-02-25T10:04:41.703172+00:00",
    "end_date": null,
    "execution_date": "2021-02-25T10:04:41.703172+00:00",
    "external_trigger": true,
    "start_date": "2021-02-25T10:04:41.708731+00:00",
    "state": "running"
}
Enter fullscreen mode Exit fullscreen mode

เรากลับไปเช็คที่หน้า Web UI ก็จะพบว่า DAG ของเรารันแล้ว เป็นการทำงานแบบ manual
Screenshot (483)

ภายใน web ของ Airflow ก็จะมีคำอธิบาย วิธีการใช้งาน api ต่างๆให้ดูScreenshot (484)

ดูการใช้งาน Airflow REST API เพิ่มเติมได้ที่

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil — patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (0)

Sentry image

Make it make sense

Make sense of fixing your code with straight-forward application monitoring.

Start debugging →

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay