Introduction to Apache Spark for Data Engineering

Sahil Sharma
5 min readMay 5, 2023

--

Apache Spark is a popular open-source large data processing platform among data engineers due to its speed, scalability, and ease of use. Spark is intended to operate with enormous datasets in a distributed computing environment, allowing developers to create high-performance data pipelines capable of processing massive volumes of data quickly.

In this post we will talk about what Apache Spark is and how it may help with data engineering. We will also present an overview of the Spark architecture and its components, discuss the various Spark deployment modes, and provide coding examples for some popular Spark use cases in data engineering.

What is Apache Spark and how can it help you with data engineering?

Apache Spark is a general-purpose distributed computing solution for processing huge datasets. It was created at the University of California, Berkeley, and has since become one of the industry’s most prominent large data processing frameworks. Spark is intended to deal with a wide range of data sources, such as Hadoop Distributed File System (HDFS), Apache Cassandra, Apache HBase, and Amazon S3.

The following are the primary advantages of utilising Spark for data engineering:

Speed

Spark can analyse big datasets at breakneck speeds by leveraging in-memory computation and data partitioning techniques.

Scalability

Because Spark can grow horizontally over a cluster of nodes, it can handle enormous datasets without sacrificing performance.

Ease of Use

Spark offers an intuitive and user-friendly interface for building data pipelines, allowing developers to easily create complex data processing workflows.

Flexibility

Spark offers a wide range of data sources and data processing activities, allowing developers to create unique data pipelines that meet their individual requirements.

Spark Architecture and its Components

The master node in Spark’s master-worker architecture is in charge of managing and directing the whole Spark cluster. It distributes resources to various applications and splits data across worker nodes. The master node also manages the fault tolerance mechanism and keeps track of the state of the worker nodes.

Worker nodes, on the other hand, are in charge of carrying out the tasks delegated to them by the master node. Each worker node has its own set of resources, including as CPU, memory, and storage, and can handle one or more tasks at the same time. When the master node assigns a task to a worker node, it also provides the relevant data to that node for processing.

The cluster manager supervises the allocation of resources to the various applications operating on the cluster and communicates with the master node and worker nodes.

Spark Core, Spark SQL, Spark Streaming, and MLlib are the four primary components of the Spark architecture.

Spark Core

Spark Core is the Spark framework’s basis and provides the essential capability for distributed data processing. It provides an RDD (Resilient Distributed Dataset) API for distributed data processing, a task scheduler for parallelism, and an in-memory memory management system.

Spark SQL

Spark SQL is a package that gives you a SQL-like interface for working with structured and semi-structured data. It allows developers to run SQL queries on data stored in a variety of data sources such as HDFS, Apache Cassandra, and Apache HBase.

Spark Streaming

Spark Streaming is a module that allows for real-time processing of streaming data. It enables developers to process real-time data streams in small batches, which makes it ideal for processing data from sensors, social media, and other real-time sources.

MLlib

MLlib is a data processing package that supports machine learning methods. It encompasses many machine learning algorithms, such as clustering, classification, regression, and collaborative filtering.

Spark Deployment Modes

Spark can be deployed in three main modes:

Local Mode

Spark runs on a single system in local mode, utilising all available cores. When working with small datasets, this mode is ideal for testing and development.

Standalone Mode

In standalone mode, Spark runs on a cluster of machines, with one serving as the master node and the others as worker nodes. This mode is appropriate for processing medium to large amounts of data.

Cluster Mode

Spark runs in cluster mode on a distributed computing environment such as Hadoop or Mesos. This mode is appropriate for large-scale data processing involving data distribution over several nodes.

The best Spark deployment mode for your use case is determined by the size of your data and the resources available in your environment.

Common Spark Use Cases in Data Engineering

Batch Processing

Spark is frequently used for batch processing of huge datasets. Spark reads data from multiple data sources, performs data transformations, and writes the results to a target data storage in this use case. The batch processing features of Spark make it perfect for jobs like ETL (Extract, Transform, Load), data warehousing, and data analytics.

Here is an example of a batch processing workflow using Spark:

# Import the necessary modules
from pyspark.sql import SparkSession

# Create a Spark session
spark = SparkSession.builder.appName("Batch Processing").getOrCreate()

# Read data from a CSV file
data = spark.read.csv("data.csv", header=True, inferSchema=True)

# Apply transformations
data_transformed = data.filter("age > 18").groupBy("gender").count()

# Write results to a target data store
data_transformed.write.mode("overwrite").csv("output")

In this example, we read data from a CSV file, apply some transformations to filter out individuals under the age of 18 and group the remaining data by gender. Finally, we write the results to a CSV file.

Real-time Data Streaming

Spark may also be used to stream data in real time. Spark collects data from a real-time data source, such as sensors or social media, and performs real-time processing on the data stream in this use case.

Here is an example of a real-time data streaming workflow using Spark:

# Import the necessary modules
from pyspark.sql import SparkSession
from pyspark.streaming import StreamingContext

# Create a Spark session
spark = SparkSession.builder.appName("Real-time Streaming").getOrCreate()

# Create a streaming context
ssc = StreamingContext(spark.sparkContext, batchDuration=10)

# Read data from a Kafka topic
data_stream = ssc.kafkaStream("localhost:9092", "data_topic")

# Apply transformations
data_transformed = data_stream.filter(lambda x: x["age"] > 18).map(lambda x: (x["gender"], 1)).reduceByKey(lambda x, y: x + y)

# Print results to console
data_transformed.pprint()

# Start the streaming context
ssc.start()
ssc.awaitTermination()

In this example, we read data from a Kafka topic and apply some transformations to filter out individuals under the age of 18 and group the remaining data by gender. Finally, we print the results to the console.

Wrapping Up

In this post, we learned what is Apache Spark, including its benefits for data engineering, its architecture and components, different deployment modes, and common use cases for batch processing and real-time data streaming.

Let me know in the comments, If you found this post useful, Follow me as I go on my content journey!

--

--

Sahil Sharma

|| Data Engineer || - || Big Data || Technology || AI & ML || CDE ||