Featured Article

Apache Spark Hadoop Tutorial Mastering Big Data Processing从零开始指南

Kenneth Jul 13, 2026

Apache Spark and Hadoop are two powerhouse technologies in the world of big data processing, and understanding how to use them is a vital skill for any data engineer or scientist. This tutorial will guide you through the essentials of using Apache Spark with Hadoop, covering all the necessary steps to get you up and running with these powerful tools.

Spark Architecture Tutorial: A Beginner-Friendly Guide to How Apache Spark Works
Spark Architecture Tutorial: A Beginner-Friendly Guide to How Apache Spark Works

Before we dive in, let's ensure you have a basic understanding of both technologies. Apache Spark is a fast and general engine for large-scale data processing, while Hadoop is a framework that allows distributed processing of large data sets across clusters of computers. Apache Spark runs on top of Hadoop and can use Hadoop Distributed File System (HDFS) for data storage.

a yellow elephant standing in front of a black background with the words hadoop commands
a yellow elephant standing in front of a black background with the words hadoop commands

Setting Up the Environment

Tobegin our journey, we need to set up a development environment with Spark and Hadoop.

Apache Spark - DZone Refcards
Apache Spark - DZone Refcards

First, you'll need to download and install Apache Hadoop. You can follow the official guide here for detailed instructions. Remember to add Hadoop's bin directory to your system's PATH.

Installing Apache Spark

two different logos that are on top of each other
two different logos that are on top of each other

After setting up Hadoop, it's time to install Apache Spark. You can download it from the official download page. Ensure you download the version that corresponds to your Hadoop version. Extract the downloaded file and set the SPARK_HOME environment variable to the directory where you extracted Spark.

Add Spark's bin directory to your system's PATH, and you're ready to proceed.

Running Spark on Hadoop

5 Free Websites to Learn Apache Spark in 2026
5 Free Websites to Learn Apache Spark in 2026

To run Spark on Hadoop, you'll need to set up a few environment variables. In your spark-env.sh file, add the following lines:

export HADOOP_HOME=/path/to/your/hadoop/installation export HADOOP_MAPRED_HOME=$HADOOP_HOME export HADOOP_COMMON_HOME=$HADOOP_HOME export YARN_HOME=$HADOOP_HOME

Replace "/path/to/your/hadoop/installation" with the actual path to your Hadoop installation.

Hadoop Tutorial: Introducing Apache Hadoop
Hadoop Tutorial: Introducing Apache Hadoop

Running Spark Jobs on Hadoop

Now that we have our environment set up, let's look at how to run Spark jobs on Hadoop.

How to Set Up Apache Hadoop and YARN: A Step-by-Step Guide
How to Set Up Apache Hadoop and YARN: A Step-by-Step Guide
About Spark – Databricks
About Spark – Databricks
Spark Tutorial | Spark Tutorial for Beginners | Apache Spark Full Course - Learn Apache Spark 2020
Spark Tutorial | Spark Tutorial for Beginners | Apache Spark Full Course - Learn Apache Spark 2020
Explore Hadoop architecture & it's components.
Explore Hadoop architecture & it's components.
a man standing in front of a large screen with the words, apache spark instruction
a man standing in front of a large screen with the words, apache spark instruction
Build Spark Machine Learning and Analytics (5 Projects)
Build Spark Machine Learning and Analytics (5 Projects)
Introduction on Apache Spark SQL DataFrame - TechVidvan
Introduction on Apache Spark SQL DataFrame - TechVidvan
Big Data Analytics using Hadoop & Apache Spark Tutorial | Big Data Tutorial for Beginners
Big Data Analytics using Hadoop & Apache Spark Tutorial | Big Data Tutorial for Beginners
What is Spark - Apache Spark Tutorial for Beginners - DataFlair
What is Spark - Apache Spark Tutorial for Beginners - DataFlair

First, you'll need to start your Hadoop daemons. Navigate to your Hadoop installation directory and run:

bin/start-dfs.sh bin/start-yarn.sh

Submitting Spark Jobs to Hadoop

Spark comes with a script called spark-class that can be used to submit Spark jobs to Hadoop's resource manager, YARN. Here's the command to submit a Spark job:

spark-class org.apache.spark.examples.Spark Pi --executor-memory 2G --num-executors 2 --master yarn --deploy-mode cluster --driver-memory 1g --conf spark.executor.extraJavaOptions=-XX\+UseG1GC --conf spark.driver.extraJavaOptions=-XX\+UseG1GC

This command runs the Spark Pi example application on YARN with two executors, each with 2GB of memory. The driver process has 1GB of memory, and both the driver and executors use the G1 garbage collector.

Monitoring Spark Jobs

You can monitor Spark jobs submitted to YARN using the YARN web interface. By default, it runs on http://localhost:8088.

Click on the "Applications" tab to see a list of all running and finished jobs. Select the Spark job you submitted to view its logs and resource usage.

Using Spark with Hadoop Data Sources

Spark can read data from and write data to Hadoop's data storage formats, such as HDFS and HBase. In this section, we'll look at how to use Spark with these data sources.

Reading Data from HDFS

HDFS is the distributed file system that comes with Hadoop. Spark can read data from HDFS using the hdfs source.

Here's an example of reading a text file from HDFS:

val conf = spark.sparkContext.hadoopConfiguration val rdd = spark.read.textFile("hdfs://namenode:9000/user/username/data.txt")

Replace "namenode:9000" with the port number your NameNode is running on, and "username/data.txt" with the path to your file in HDFS.

Writing Data to HDFS

Similarly, Spark can write data to HDFS using the parquet or sequenceFile formats. Here's an example of writing a DataFrame to HDFS in Parquet format:

df.write.format("parquet").save("hdfs://namenode:9000/user/username/output.parquet")

Again, replace "namenode:9000" with the port number of your NameNode, and "username/output.parquet" with the desired output path in HDFS.

With this guide, you should now have a solid understanding of how to use Apache Spark with Hadoop. Whether you're a seasoned data engineer or just starting your journey into big data, these tools will be invaluable in processing and analyzing large datasets.

Remember, the best way to learn is by doing. Try out the examples in this tutorial, and experiment with your own use cases. You'll be amazed at what you can achieve with Spark and Hadoop!