You can download this code by clicking the button below.
This code is now available for download.
The code defines an Airflow task that uses PythonOperator to execute a simple random number generation function. The function generates a random integer between 1 and 100 and prints it.
Technology Stack : Apache Airflow, Python
Code Type : Airflow PythonOperator
Code Difficulty : Intermediate
import random
import json
from airflow.operators.python_operator import PythonOperator
from airflow.utils.dates import days_ago
from airflow.models import DAG
from datetime import datetime
def random_task(**kwargs):
# This function generates a random number and prints it
random_number = random.randint(1, 100)
print(f"Random Number: {random_number}")
return random_number
default_args = {
'owner': 'airflow',
'start_date': days_ago(1),
}
with DAG('random_number_dag', default_args=default_args, schedule_interval='@daily') as dag:
random_task_op = PythonOperator(
task_id='random_task',
python_callable=random_task,
provide_context=True,
)
random_task_op