0
0
Apache Airflowdevops~10 mins

XCom with return values in Apache Airflow - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to push a value to XCom using the return statement in a PythonOperator task.

Apache Airflow
def task_function():
    return [1]
Drag options to blanks, or click blank then click option'
ANone
Bprint('Hello from XCom!')
C'Hello from XCom!'
Dxcom_push('Hello from XCom!')
Attempts:
3 left
💡 Hint
Common Mistakes
Using print instead of return
Calling xcom_push directly inside the function
2fill in blank
medium

Complete the code to pull the XCom value from a previous task named 'push_task'.

Apache Airflow
from airflow.operators.python import PythonOperator

def pull_task(ti):
    value = ti.xcom_pull(task_ids=[1])
    print(value)
Drag options to blanks, or click blank then click option'
A'push_task'
Bpush_task
C'pull_task'
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the variable name without quotes
Using the wrong task ID
3fill in blank
hard

Fix the error in the code to correctly return a dictionary as an XCom value.

Apache Airflow
def task_function():
    data = [1]  # dictionary to return
    return data
Drag options to blanks, or click blank then click option'
A'key: value'
B['key', 'value']
C('key', 'value')
D{'key': 'value'}
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a list or tuple instead of a dictionary
Returning a string instead of a dictionary
4fill in blank
hard

Fill both blanks to create a PythonOperator that pushes a return value and another that pulls it.

Apache Airflow
push_task = PythonOperator(
    task_id='push_task',
    python_callable=[1],
    dag=dag
)
pull_task = PythonOperator(
    task_id='pull_task',
    python_callable=[2],
    dag=dag
)
Drag options to blanks, or click blank then click option'
Atask_function_push
Btask_function_pull
Ctask_function
Dpull_function
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same function for both tasks
Mixing up push and pull function names
5fill in blank
hard

Fill all three blanks to create a DAG with two tasks where the second pulls the XCom value from the first.

Apache Airflow
from airflow import DAG
from airflow.operators.python import PythonOperator
from datetime import datetime

def [1]():
    return 'Data from first task'

def [2](ti):
    data = ti.xcom_pull(task_ids=[3])
    print(f'Received: {data}')

dag = DAG('xcom_example', start_date=datetime(2024, 1, 1), schedule_interval='@daily')

push = PythonOperator(task_id='push', python_callable=[1], dag=dag)
pull = PythonOperator(task_id='pull', python_callable=[2], dag=dag)
push >> pull
Drag options to blanks, or click blank then click option'
Apush_task_function
Bpull_task_function
C'push'
D'pull'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong task_id in xcom_pull
Mismatching function names and task_ids