एक @task.branch परिभाषित करें
Airflow में conditional logic की शक्ति के बारे में जानने के बाद, आप @task.branch डेकोरेटर को आज़माना चाहते हैं. आप अलग कोड पाथ चलाना चाहेंगे अगर current execution date नया साल दर्शाती है (जैसे, 2026 बनाम 2025).
Dag आपके लिए परिभाषित है, साथ ही संबंधित tasks भी. आपका मौजूदा काम @task.branch को इम्प्लीमेंट करना है.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Apache Airflow परिचय
अभ्यास निर्देश
- current और previous execution year की तुलना करने के लिए
year_checkमें उपयुक्त Airflow date template variables को parameters के रूप में जोड़ें. - हर date parameter से पहले 4 characters slice करके
current_yearऔरprevious_yearअसाइन करें. current_year_taskऔरnew_year_taskपर dependencies सेट करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
@dag(start_date=datetime(2026,5,1), schedule='@monthly')
def process_yearly_expenses():
# Create a function to determine if years are different
@task.branch
def year_check(____, ____):
current_year = int(____[0:4])
previous_year = int(____[0:4])
if current_year == previous_year:
return 'current_year_task'
else:
return 'new_year_task'
# Define the dependencies
branch_task __ current_year_task
____ >> new_year_task