{"id":180,"date":"2026-06-28T10:04:33","date_gmt":"2026-06-28T08:04:33","guid":{"rendered":"https:\/\/codeswarm.io\/?p=180"},"modified":"2026-07-24T10:20:25","modified_gmt":"2026-07-24T08:20:25","slug":"airflow-using-with-hdfs-and-apache-spark","status":"publish","type":"post","link":"https:\/\/codeswarm.io\/?p=180","title":{"rendered":"Airflow using with HDFS and Apache Spark"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Using Apache Airflow with Apache Spark and HDFS typically involves setting up a workflow where Airflow orchestrates the execution of Spark jobs, and the data processed by Spark is stored or retrieved from HDFS. Here&#8217;s an overview of how to set up such a system:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Airflow setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Airflow is used to schedule and orchestrate the workflow. You&#8217;ll need to have Apache Airflow installed and configured with the necessary components for your Spark jobs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Spark setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Spark needs to be set up in a cluster environment (e.g., standalone mode, YARN, or Kubernetes). The Spark jobs will typically read and write data to\/from HDFS.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">HDFS setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">HDFS (Hadoop Distributed File System) serves as the distributed storage system where data is stored and retrieved by Spark jobs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Airflow and Spark Integration<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We need a Scala-based Spark application that will read\/write data to\/from HDFS or perform any transformation required. We will then submit this application using Airflow&#8217;s <code>SparkSubmitOperator<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s an example of a simple Scala-based Spark application that reads data from HDFS, processes it, and saves it back to HDFS.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"scala\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import org.apache.spark.sql.SparkSession\n\nobject SparkJob {\n  def main(args: Array[String]): Unit = {\n    \/\/ Initialize Spark session\n    val spark = SparkSession.builder\n      .appName(\"Scala Spark Job\")\n      .config(\"spark.hadoop.fs.defaultFS\", \"hdfs:\/\/namenode_host:8020\")\n      .getOrCreate()\n\n    \/\/ Read data from HDFS\n    val inputData = spark.read.text(\"hdfs:\/\/namenode_host:8020\/input_data.txt\")\n\n    \/\/ Perform transformation (example: convert to uppercase)\n    val transformedData = inputData.rdd.map(line => line.getString(0).toUpperCase())\n\n    \/\/ Save transformed data back to HDFS\n    transformedData.saveAsTextFile(\"hdfs:\/\/namenode_host:8020\/output_data\")\n    \n    spark.stop()\n  }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To build the Scala application we use a build tool like <strong>SBT<\/strong> (Scala Build Tool) to package the Scala application as a JAR file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"scala\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">name := \"SparkJob\"\n\nversion := \"1.0\"\n\nscalaVersion := \"2.12.10\"\n\nlibraryDependencies += \"org.apache.spark\" %% \"spark-core\" % \"3.1.2\"\nlibraryDependencies += \"org.apache.spark\" %% \"spark-sql\" % \"3.1.2\"<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then build the JAR file with the following command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sbt package<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will create a JAR file under the <code>target\/scala-2.12\/<\/code> directory, e.g., <code>target\/scala-2.12\/sparkjob_2.12-1.0.jar<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In next step we need to install Apache Airflow and configure the necessary connections (like <code>spark_default<\/code> connection) to submit the Spark job to our cluster. Once the Scala Spark job is ready and packaged, we can submit it to our Spark cluster using the <code>SparkSubmitOperator<\/code> in Airflow. Here is how you can do that:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from airflow import DAG\nfrom airflow.providers.apache.spark.operators.spark_submit import SparkSubmitOperator\nfrom datetime import datetime\n\ndefault_args = {\n    'owner': 'airflow',\n    'start_date': datetime(2025, 6, 2),\n}\n\ndag = DAG(\n    'scala_spark_hdfs_etl',\n    default_args=default_args,\n    schedule_interval=None,\n)\n\nspark_submit_task = SparkSubmitOperator(\n    task_id='submit_spark_job',\n    conn_id='spark_default',  # Airflow Spark connection ID\n    application='\/path\/to\/target\/scala-2.12\/sparkjob_2.12-1.0.jar',  # Path to the Scala JAR file\n    name='scala-spark-job',\n    conf={'spark.hadoop.fs.defaultFS': 'hdfs:\/\/namenode_host:8020'},\n    dag=dag,\n)\n\nspark_submit_task<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>application<\/code><\/strong>: Path to the JAR file generated by your Scala application.<\/li>\n\n\n\n<li><strong><code>conn_id<\/code><\/strong>: Connection ID for Spark. You may need to configure this in Airflow (this typically points to your Spark cluster, either YARN or standalone).<\/li>\n\n\n\n<li><strong><code>conf<\/code><\/strong>: Spark configuration, such as the HDFS URI.<\/li>\n\n\n\n<li><strong><code>task_id<\/code><\/strong>: Unique identifier for the Airflow task.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Airflow Workflow<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now that the DAG is set up, Airflow will submit the Spark job (written in Scala) to your Spark cluster. The flow will execute the Spark job as part of the DAG.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Scheduling<\/strong>: You can set up a schedule for when to run the Scala-based Spark job.<\/li>\n\n\n\n<li><strong>Monitoring<\/strong>: Airflow will monitor the task and show logs, task success\/failure, and retries in the UI.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Optional &#8211; Handle HDFS Files in Scala<\/strong>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you need to interact with HDFS directly within your Scala Spark job (for example, listing files in HDFS or checking for certain files), you can use the HDFS API in Spark. Here&#8217;s an example to list HDFS files in your job:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"scala\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">val hdfsPath = \"hdfs:\/\/namenode_host:8020\/input_data\/\"\nval fs = org.apache.hadoop.fs.FileSystem.get(new java.net.URI(hdfsPath), spark.sparkContext.hadoopConfiguration)\nval status = fs.listStatus(new org.apache.hadoop.fs.Path(hdfsPath))\n\nstatus.foreach(file => println(file.getPath))<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Summary:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Scala Application<\/strong>: Write your Spark job in Scala and package it as a JAR.<\/li>\n\n\n\n<li><strong>Airflow DAG<\/strong>: Use the <code>SparkSubmitOperator<\/code> to submit your Scala-based Spark job.<\/li>\n\n\n\n<li><strong>HDFS<\/strong>: Use HDFS paths and configurations in your Spark job to read\/write data.<\/li>\n\n\n\n<li><strong>Scheduling and Monitoring<\/strong>: Airflow handles scheduling and monitoring the execution of your Spark jobs.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using Apache Airflow with Apache Spark and HDFS typically involves setting up a workflow where Airflow orchestrates the execution of Spark jobs, and the data processed by Spark is stored or retrieved from HDFS. Here&#8217;s an overview of how to set up such a system: Airflow setup Airflow is used to schedule and orchestrate the &hellip; <a href=\"https:\/\/codeswarm.io\/?p=180\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Airflow using with HDFS and Apache Spark<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,23],"tags":[33,22,34,19],"class_list":["post-180","post","type-post","status-publish","format-standard","hentry","category-big-data","category-programming","tag-airflow","tag-spark","tag-python","tag-scala"],"_links":{"self":[{"href":"https:\/\/codeswarm.io\/index.php?rest_route=\/wp\/v2\/posts\/180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeswarm.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeswarm.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeswarm.io\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeswarm.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=180"}],"version-history":[{"count":1,"href":"https:\/\/codeswarm.io\/index.php?rest_route=\/wp\/v2\/posts\/180\/revisions"}],"predecessor-version":[{"id":181,"href":"https:\/\/codeswarm.io\/index.php?rest_route=\/wp\/v2\/posts\/180\/revisions\/181"}],"wp:attachment":[{"href":"https:\/\/codeswarm.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeswarm.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeswarm.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}