Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a task that runs a Python function atomically in Airflow.
Apache Airflow
from airflow import DAG from airflow.operators.python import PythonOperator from datetime import datetime def my_task(): print('Task running') dag = DAG('my_dag', start_date=datetime(2024, 1, 1)) task = PythonOperator(task_id='run_my_task', python_callable=[1], dag=dag)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'print' instead of the function name.
Passing a function call like 'my_task()' instead of the function itself.
✗ Incorrect
The PythonOperator requires the function name to run as the python_callable argument. Here, 'my_task' is the function defined to run.
2fill in blank
mediumComplete the code to set the task to run only after the previous task finishes successfully.
Apache Airflow
task1 >> [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same task on both sides.
Using the DAG object instead of a task.
✗ Incorrect
The '>>' operator sets task dependencies. 'task1 >> task2' means task2 runs after task1 completes successfully.
3fill in blank
hardFix the error in the code to ensure the task retries 3 times on failure.
Apache Airflow
task = PythonOperator(task_id='retry_task', python_callable=my_task, retries=[1], dag=dag)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing retries as a string like '3'.
Passing a boolean value instead of a number.
✗ Incorrect
The retries parameter expects an integer number of retries, not a string or boolean.
4fill in blank
hardFill both blanks to create a DAG that runs daily and starts on January 1, 2024.
Apache Airflow
dag = DAG('daily_dag', schedule_interval=[1], start_date=[2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '@hourly' instead of '@daily'.
Using a string instead of a datetime object for start_date.
✗ Incorrect
The schedule_interval '@daily' runs the DAG daily. The start_date must be a datetime object for January 1, 2024.
5fill in blank
hardFill all three blanks to create a task that runs a Python function, retries twice, and has a retry delay of 5 minutes.
Apache Airflow
from datetime import timedelta task = PythonOperator(task_id='retry_task', python_callable=[1], retries=[2], retry_delay=timedelta(minutes=[3]), dag=dag)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'print' instead of the function name.
Passing retries as a string.
Using a wrong value for retry_delay minutes.
✗ Incorrect
The python_callable is 'my_task', retries is 2, and retry_delay is set to 5 minutes using timedelta.