from airflow import DAG from airflow.providers.apache.spark.operators.spark_submit import SparkSubmitOperator from datetime import datetime default_args = { 'start_date': datetime(2024, 1, 1), } dag = DAG( 'spark_pipeline', default_args=default_args, schedule_interval='0 12 * * MON-FRI', catchup=False ) spark_task = SparkSubmitOperator( task_id='run_spark_job', application='/path/to/spark_app.py', conn_id='spark_default', dag=dag )
The cron expression '0 12 * * MON-FRI' means the job runs at minute 0, hour 12 (noon), every day of the month, every month, but only on Monday through Friday.
So the DAG runs every weekday at 12:00 PM.
from pyspark.sql import SparkSession spark = SparkSession.builder.appName('TestApp').getOrCreate() data = [ (1, 'Alice', 29), (2, 'Bob', 31), (3, 'Cathy', 25) ] columns = ['id', 'name', 'age'] df = spark.createDataFrame(data, columns) df_filtered = df.filter(df.age > 28) df_filtered.printSchema()
Spark infers integer types for numeric literals in Python lists as IntegerType by default, and strings as StringType.
Since the data is a list of tuples with integers and strings, the schema will have integer types for 'id' and 'age', and string for 'name'.
Nullable is true by default when creating DataFrame from Python data.
spark_task = SparkSubmitOperator(
task_id='run_spark_job',
application='/home/airflow/dags/spark_jobs/my_spark_app.py',
conn_id='spark_default',
dag=dag
)FileNotFoundError usually means the Spark cluster cannot find the application file at the given path.
In Airflow, the path must be accessible from the Spark cluster nodes, not just the Airflow scheduler machine.
In Airflow, 'task1 >> task2' means task1 runs before task2.
So 'extract_data >> transform_data' ensures 'transform_data' runs after 'extract_data'.
Option B is also valid but less commonly used; however, only one correct answer allowed, so we pick the bitshift operator which is clearer.
Airflow is a workflow orchestration tool that schedules and manages tasks, including Spark jobs.
It does not optimize Spark performance or replace Spark clusters.
It also does not handle data storage directly.
