0% found this document useful (0 votes)
2 views22 pages

SparkSQL Project Report

This project report details a hands-on experiment with Spark SQL using PySpark, focusing on structured data processing through SQL-like queries on a dataset of student academic records. The objectives include creating DataFrames, executing various SQL queries, and understanding Spark SQL's integration within the big data ecosystem. The report concludes with insights on the real-world applications and limitations of Spark SQL, providing a foundational understanding for students in big data analytics.

Uploaded by

aadhi612801
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views22 pages

SparkSQL Project Report

This project report details a hands-on experiment with Spark SQL using PySpark, focusing on structured data processing through SQL-like queries on a dataset of student academic records. The objectives include creating DataFrames, executing various SQL queries, and understanding Spark SQL's integration within the big data ecosystem. The report concludes with insights on the real-world applications and limitations of Spark SQL, providing a foundational understanding for students in big data analytics.

Uploaded by

aadhi612801
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ANJALAI AMMAL MAHALINGAM ENGINEERING COLLEGE

Kovilvenni – 614403, Thiruvarur District, Tamil Nadu


Department of Computer Science and Engineering

LABORATORY PROJECT REPORT

Hands-on Tryout – Spark SQL

Subject : Big Data Analytics Laboratory

Subject Code : CS3581 / Big Data Lab

Experiment Title : Hands-on Tryout – Spark SQL

Semester : VI (Regulation 2021)

Academic Year : 2025 – 2026

Department : Computer Science and Engineering

Aadhithiyan. B
Student Names : Arun Adhithiya. G
Danush. S

820423104002
Register Numbers : 820423104008
820423104018

Date of Submission : 13 / 04 / 2026

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 1
ABSTRACT
Apache Spark is a powerful open-source distributed computing framework designed for large-scale data
processing. Spark SQL is a module within Apache Spark that enables structured data processing using
SQL-like queries. This project report presents a comprehensive hands-on exploration of Spark SQL using
PySpark — the Python API for Apache Spark — in a Google Colab / local Python environment. The
experiment involves creating a structured DataFrame representing student academic records with attributes
such as student ID, name, marks, grade, and department. SQL queries including SELECT, WHERE, GROUP
BY, ORDER BY, AVG, MAX, MIN, and COUNT are executed on this dataset to retrieve and analyse
information. Key objectives include understanding how Spark SQL integrates with the Spark ecosystem,
registering DataFrames as temporary SQL views, executing analytical queries, and observing the query
execution plan. The results demonstrate Spark SQL's ability to process structured data intuitively and
efficiently. The experiment concludes with a study of real-world applications, advantages, and limitations of
Spark SQL, providing students with a solid foundation for big data analytics.

Keywords : Apache Spark, Spark SQL, PySpark, DataFrame, Big Data, SQL Queries, Distributed Computing

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 2
1. INTRODUCTION TO APACHE SPARK AND SPARK SQL

1.1 What is Apache Spark?


Apache Spark is an open-source, distributed data processing engine developed at UC Berkeley's AMPLab in
2009 and later donated to the Apache Software Foundation. It is designed to process large volumes of data
quickly by distributing the work across a cluster of computers. Unlike traditional Hadoop MapReduce, which
reads and writes data to disk at every step, Spark keeps data in memory (RAM) as much as possible, making
it up to 100× faster for certain workloads.

Spark provides a unified platform for batch processing, real-time stream processing, machine learning
(MLlib), graph computation (GraphX), and structured data querying (Spark SQL). It supports APIs in Python
(PySpark), Scala, Java, and R.

Component Purpose

Spark Core Base engine – task scheduling, memory management, fault recovery

Spark SQL Structured data processing via SQL and DataFrames

Spark Streaming Real-time processing of live data streams

MLlib Machine learning library with classification, regression, clustering

GraphX Graph computation and analytics

PySpark Python API to interact with all Spark modules


Table 1.1 – Apache Spark Core Components

1.2 What is Spark SQL?


Spark SQL is a Spark module for structured and semi-structured data processing. It allows developers to
query data using standard SQL syntax while also providing programmatic access through the DataFrame and
Dataset APIs. Spark SQL bridges the gap between relational databases and distributed computing, letting
users write familiar SQL statements that are optimised and executed at scale.

• Unified Data Access: Read data from JSON, CSV, Parquet, ORC, Avro, Hive tables, and JDBC
databases using one consistent API.
• Catalyst Optimiser: An advanced query optimiser that automatically rewrites and optimises SQL queries
for maximum performance.
• Tungsten Execution Engine: A memory and CPU-efficient execution engine that generates optimised
JVM bytecode.
• Interoperability: DataFrames can be converted to/from RDDs and used together with Spark's other
libraries.
• Schema Inference: Spark SQL can automatically detect the schema (column names and types) from
structured file formats.

■ Spark SQL uses a component called the Catalyst Optimiser to automatically choose the most efficient
query plan — similar to how traditional databases use query optimisers, but at a distributed scale.

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 3
2. OBJECTIVES OF THE EXPERIMENT
By the end of this hands-on experiment, students will be able to:

1. Install and configure PySpark in a Python environment (or Google Colab).


2. Create a SparkSession — the entry point to all Spark SQL functionality.
3. Build structured DataFrames from Python dictionaries and lists.
4. Register DataFrames as temporary SQL views for querying.
5. Write and execute SQL queries (SELECT, WHERE, GROUP BY, ORDER BY, AVG, COUNT, MAX, MIN).
6. Filter records based on conditions (e.g., marks > 80).
7. Compute aggregate statistics such as average marks per department.
8. Sort query results in ascending and descending order.
9. Understand the concept of lazy evaluation and query execution plans.
10. Appreciate how Spark SQL fits into real-world big data analytics pipelines.

3. SYSTEM REQUIREMENTS

3.1 Hardware Requirements

Component Minimum Requirement Recommended

Processor Intel Core i3 / AMD equivalent Intel Core i5/i7 or higher

RAM 4 GB 8 GB or more

Storage 10 GB free disk space 20 GB SSD

Network Required for downloading packages Broadband internet

OS Windows 10 / Ubuntu 18.04+ Ubuntu 20.04 LTS / Windows 11


Table 3.1 – Hardware Requirements

3.2 Software Requirements

Software Version Purpose

Python 3.8 or above Programming language for PySpark

Apache Spark 3.3.x / 3.4.x Distributed processing engine

PySpark 3.3.x / 3.4.x Python API for Apache Spark

Java (JDK) 8 or 11 Required runtime for Spark

Jupyter Notebook Latest Interactive development environment

Google Colab Online (free) Cloud-based Python + Spark environment

pip Latest Python package manager


Table 3.2 – Software Requirements

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 4
4. ARCHITECTURE OF SPARK SQL
Spark SQL follows a layered architecture that converts a user's SQL or DataFrame API call into an optimised
physical execution plan distributed across multiple machines. The diagram below illustrates the key layers.

USER INTERFACE LAYER SQL Queries | DataFrame API | Dataset API | HiveQL

CATALYST OPTIMISER Parse SQL → Logical Plan → Optimised Logical Plan → Physical Plan

TUNGSTEN EXECUTION ENGINE Code Generation | Memory Management | Cache-aware Computation

SPARK CORE ENGINE Task Scheduling | DAG Execution | Fault Tolerance | In-memory Storage

DATA SOURCES CSV | JSON | Parquet | ORC | Hive | JDBC | Avro

Figure 4.1 – Layered Architecture of Spark SQL

4.1 Catalyst Optimiser – How it works


• Step 1 – Parsing: The SQL string or DataFrame API call is parsed into an Abstract Syntax Tree (AST),
also called an Unresolved Logical Plan.
• Step 2 – Analysis: The analyser resolves column names and table references against the catalog,
producing a Resolved Logical Plan.
• Step 3 – Logical Optimisation: The optimiser applies rule-based transformations such as constant
folding, predicate pushdown, and column pruning.
• Step 4 – Physical Planning: Multiple physical plans are generated and the lowest-cost plan is selected
using a cost model.
• Step 5 – Code Generation: The Tungsten engine generates optimised JVM bytecode for the chosen plan
and executes it across the cluster.

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 5
5. METHODOLOGY / PROCEDURE
The experiment is divided into clearly defined steps. Follow the procedure sequentially to successfully
execute Spark SQL queries.

Step 1: Install Dependencies


Install Java (JDK 8 or 11) and PySpark using pip. In Google Colab, only !pip install pyspark is needed
since Java is pre-installed.

Step 2: Import Libraries


Import SparkSession from [Link] and other necessary modules such as functions and types.

Step 3: Create SparkSession


Initialise a SparkSession object which serves as the single entry point to interact with all Spark SQL
functionality.

Step 4: Prepare Sample Dataset


Create a Python list of tuples representing student records. Define the schema (column names and data
types) using StructType and StructField.

Step 5: Create DataFrame


Pass the data list and schema to [Link]() to obtain a structured Spark DataFrame.

Step 6: Register Temporary View


Use [Link]('students') to register the DataFrame as a temporary SQL table visible
within the current session.

Step 7: Execute SQL Queries


Use [Link]('SELECT ...') to run various SQL queries including filtering, aggregation, sorting, and
grouping.

Step 8: Display Results


Call .show() on the result DataFrame to display output. Use .show(truncate=False) to see full column
values.

Step 9: Analyse Query Plan


Use .explain() to view the physical execution plan generated by the Catalyst Optimiser.

Step 10: Stop SparkSession


Call [Link]() to release all cluster resources at the end of the session.

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 6
6. SAMPLE DATASET – STUDENT ACADEMIC RECORDS
A realistic student dataset is used for all experiments in this report. The dataset contains 15 student records
with the following attributes:

Column Name Data Type Description

student_id IntegerType Unique identifier for each student

name StringType Full name of the student

department StringType Enrolled department (CSE, ECE, MECH, EEE, CIVIL)

semester IntegerType Current semester (1–8)

marks IntegerType Total marks obtained (out of 100)

grade StringType Grade awarded (O, A+, A, B+, B, C)

cgpa DoubleType Cumulative GPA on a 10-point scale

city StringType Home city of the student


Table 6.1 – Dataset Schema

6.1 Full Dataset (15 Records)

ID Name Dept Sem Marks Grade CGPA City

101 Aravind Kumar CSE 6 92 O 9.4 Chennai

102 Priya Lakshmi ECE 6 85 A+ 8.7 Coimbatore

103 Mohammed Rizwan CSE 6 78 A 8.1 Madurai

104 Deepika Nair MECH 4 65 B+ 7.2 Trichy

105 Karthik Rajan EEE 6 90 O 9.2 Salem

106 Sneha Pillai CSE 6 88 A+ 8.9 Vellore

107 Ramesh Babu CIVIL 4 55 B 6.8 Erode

108 Anitha Devi ECE 6 95 O 9.6 Chennai

109 Vishal Chandran MECH 4 72 A 7.8 Tirunelveli

110 Kavitha Srinivas CSE 6 83 A+ 8.5 Madurai

111 Suresh Pandi EEE 4 60 B+ 7.0 Dindigul

112 Logesh Murugan CSE 6 97 O 9.8 Chennai

113 Vijayalakshmi CIVIL 4 48 C 6.2 Thanjavur

114 Dinesh Raj ECE 6 81 A+ 8.3 Puducherry

115 Keerthana Nair MECH 4 70 A 7.6 Coimbatore

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 7
Table 6.2 – Complete Student Records Dataset

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 8
7. IMPLEMENTATION WITH PYSPARK CODE

7.1 Step 1: Install PySpark and Import Libraries


The first step is to install PySpark and import all required libraries. In Google Colab, run the cell with the !
prefix to execute shell commands.

# ■■ Install PySpark (run once in Colab or terminal) ■■■■■■■■■■■■■■■■■■


# !pip install pyspark
# ■■ Import required PySpark modules ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
from [Link] import SparkSession
from [Link] import StructType, StructField
from [Link] import IntegerType, StringType, DoubleType
from [Link] import functions as F
print('PySpark libraries imported successfully!')

7.2 Step 2: Create a SparkSession


SparkSession is the entry point to programming with Spark SQL. It replaces the older SQLContext and
HiveContext. The getOrCreate() method reuses an existing session if one already exists, preventing
resource leaks.

# ■■ Create SparkSession ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■


spark = [Link] \
.appName('SparkSQL_StudentAnalysis') \
.master('local[*]') \
.config('[Link]', True) \
.getOrCreate()
# Reduce verbose logging
[Link]('ERROR')
print('SparkSession created successfully!')
print(f'Spark Version: {[Link]}')

■ Note: 'local[*]' tells Spark to run locally using all available CPU cores. In a production cluster, this would
be replaced with the cluster master URL.

7.3 Step 3: Define Schema and Create DataFrame


We explicitly define the schema using StructType and StructField to ensure correct data types. This avoids
schema inference errors and makes the code production-ready.

# ■■ Define Schema ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■


schema = StructType([
StructField('student_id', IntegerType(), False),
StructField('name', StringType(), False),
StructField('department', StringType(), False),
StructField('semester', IntegerType(), False),
StructField('marks', IntegerType(), False),
StructField('grade', StringType(), False),

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 9
StructField('cgpa', DoubleType(), False),
StructField('city', StringType(), False),
])
# ■■ Dataset
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
data = [
(101, 'Aravind Kumar', 'CSE', 6, 92, 'O', 9.4, 'Chennai'),
(102, 'Priya Lakshmi', 'ECE', 6, 85, 'A+', 8.7, 'Coimbatore'),
(103, 'Mohammed Rizwan', 'CSE', 6, 78, 'A', 8.1, 'Madurai'),
(104, 'Deepika Nair', 'MECH', 4, 65, 'B+', 7.2, 'Trichy'),
(105, 'Karthik Rajan', 'EEE', 6, 90, 'O', 9.2, 'Salem'),
(106, 'Sneha Pillai', 'CSE', 6, 88, 'A+', 8.9, 'Vellore'),
(107, 'Ramesh Babu', 'CIVIL', 4, 55, 'B', 6.8, 'Erode'),
(108, 'Anitha Devi', 'ECE', 6, 95, 'O', 9.6, 'Chennai'),
(109, 'Vishal Chandran', 'MECH', 4, 72, 'A', 7.8, 'Tirunelveli'),
(110, 'Kavitha Srinivas','CSE', 6, 83, 'A+', 8.5, 'Madurai'),
(111, 'Suresh Pandi', 'EEE', 4, 60, 'B+', 7.0, 'Dindigul'),
(112, 'Logesh Murugan', 'CSE', 6, 97, 'O', 9.8, 'Chennai'),
(113, 'Vijayalakshmi', 'CIVIL', 4, 48, 'C', 6.2, 'Thanjavur'),
(114, 'Dinesh Raj', 'ECE', 6, 81, 'A+', 8.3, 'Puducherry'),
(115, 'Keerthana Nair', 'MECH', 4, 70, 'A', 7.6, 'Coimbatore'),
]
# ■■ Create DataFrame ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
df = [Link](data, schema)
[Link]()
[Link](5)

8. SQL QUERIES – INPUT AND OUTPUT


After registering the DataFrame as a temporary view, we execute various SQL queries. Each query is shown
with its code, explanation, and expected output.

8.1 Register Temporary SQL View


# Register the DataFrame as a temporary SQL table
[Link]('students')
print('Temporary view students created successfully!')

Once registered, we can query this view using standard SQL syntax via [Link]('...'). The view exists only
for the duration of the SparkSession.

8.2 Query 1 – SELECT All Records


SQL Statement:

SELECT * FROM students;

PySpark Code:

# Query 1: Retrieve all student records


result1 = [Link]('SELECT * FROM students')
[Link]()

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 10
Explanation: This retrieves all 15 rows and all 8 columns from the students view. It is equivalent to a full
table scan.

Expected Output:

ID Name Dept Sem Marks Grade CGPA City

101 Aravind Kumar CSE 6 92 O 9.4 Chennai

102 Priya Lakshmi ECE 6 85 A+ 8.7 Coimbatore

... ... ... ... ... ... ... ...

115 Keerthana Nair MECH 4 70 A 7.6 Coimbatore


Output 8.2 – All 15 Student Records

8.3 Query 2 – Filter: Students with Marks > 80


SQL Statement:

SELECT student_id, name, department, marks, grade FROM students WHERE marks > 80 ORDER
BY marks DESC;

PySpark Code:

# Query 2: Students who scored more than 80 marks


result2 = [Link]('''
SELECT student_id, name, department, marks, grade
FROM students
WHERE marks > 80
ORDER BY marks DESC
''')
[Link]()

Explanation: The WHERE clause filters out students with marks ≤ 80. ORDER BY marks DESC sorts the
results from highest to lowest score. 9 out of 15 students have marks above 80.

Expected Output:

ID Name Dept Marks Grade

112 Logesh Murugan CSE 97 O

108 Anitha Devi ECE 95 O

101 Aravind Kumar CSE 92 O

105 Karthik Rajan EEE 90 O

106 Sneha Pillai CSE 88 A+

102 Priya Lakshmi ECE 85 A+

110 Kavitha Srinivas CSE 83 A+

114 Dinesh Raj ECE 81 A+

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 11
Output 8.3 – Students with Marks > 80 (Sorted Descending)

8.4 Query 3 – Average Marks per Department (GROUP BY)


# Query 3: Average marks grouped by department
result3 = [Link]('''
SELECT department,
COUNT(*) AS total_students,
ROUND(AVG(marks),2) AS avg_marks,
MAX(marks) AS highest_marks,
MIN(marks) AS lowest_marks
FROM students
GROUP BY department
ORDER BY avg_marks DESC
''')
[Link]()

GROUP BY department aggregates all rows belonging to the same department. AVG(), MAX(), MIN(), and
COUNT() are applied to each group independently. ROUND() formats the average to 2 decimal places.

Department Total Students Avg Marks Highest Lowest

CSE 5 87.60 97 78

ECE 3 87.00 95 81

EEE 2 75.00 90 60

MECH 3 69.00 72 65

CIVIL 2 51.50 55 48
Output 8.4 – Department-wise Statistics

8.5 Query 4 – Top 3 Students (LIMIT)


# Query 4: Top 3 students by marks
result4 = [Link]('''
SELECT student_id, name, department, marks, cgpa
FROM students
ORDER BY marks DESC
LIMIT 3
''')
[Link]()

ID Name Department Marks CGPA

112 Logesh Murugan CSE 97 9.8

108 Anitha Devi ECE 95 9.6

101 Aravind Kumar CSE 92 9.4


Output 8.5 – Top 3 Students by Marks

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 12
8.6 Query 5 – Grade Distribution (HAVING clause)
# Query 5: Count of students per grade
result5 = [Link]('''
SELECT grade,
COUNT(*) AS student_count
FROM students
GROUP BY grade
HAVING COUNT(*) >= 1
ORDER BY student_count DESC
''')
[Link]()

Grade Student Count

O 4

A+ 4

A 3

B+ 2

B 1

C 1
Output 8.6 – Grade Distribution

8.7 Query 6 – CSE Department Students with Grade 'O'


# Query 6: Students in CSE with grade O
result6 = [Link]('''
SELECT student_id, name, marks, cgpa, city
FROM students
WHERE department = 'CSE' AND grade = 'O'
ORDER BY cgpa DESC
''')
[Link]()

ID Name Marks CGPA City

112 Logesh Murugan 97 9.8 Chennai

101 Aravind Kumar 92 9.4 Chennai


Output 8.7 – CSE Students with Grade O

8.8 Query 7 – Using DataFrame API (Alternative to SQL)


Spark SQL operations can also be performed using the DataFrame API without writing SQL strings. The
following code is equivalent to a SQL GROUP BY query:

# Query 7: DataFrame API – Average CGPA by department


result7 = [Link]('department') \

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 13
.agg(
[Link]([Link]('cgpa'), 2).alias('avg_cgpa'),
[Link]('student_id').alias('count')
) \
.orderBy('avg_cgpa', ascending=False)
[Link]()

Department Avg CGPA Count

CSE 8.94 5

ECE 8.87 3

EEE 8.1 2

MECH 7.53 3

CIVIL 6.5 2
Output 8.8 – Average CGPA per Department (DataFrame API)

8.9 Query 8 – Conditional Column with CASE WHEN


# Query 8: Categorise students as Pass / Distinction / Fail
result8 = [Link]('''
SELECT name, marks,
CASE
WHEN marks >= 90 THEN 'Distinction'
WHEN marks >= 50 THEN 'Pass'
ELSE 'Fail'
END AS result_category
FROM students
ORDER BY marks DESC
''')
[Link]()

Name Marks Result Category

Logesh Murugan 97 Distinction

Anitha Devi 95 Distinction

Aravind Kumar 92 Distinction

Karthik Rajan 90 Distinction

Sneha Pillai 88 Pass

... ... ...

Vijayalakshmi 48 Fail
Output 8.9 – Students Categorised by Result

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 14
8.10 Viewing the Query Execution Plan
# View the physical execution plan
[Link]()
# View extended plan (parsed, analysed, optimised, physical)
[Link](extended=True)

The explain() output shows how Spark internally plans and executes the query. You can observe operations
like HashAggregate, Exchange (shuffle), Sort, and FileScan in the execution plan. This helps developers
understand performance bottlenecks and optimise queries.

# ■■ Sample explain() output (simplified) ■■■■■■■■■■■■■■■■■■■■■■■■■■■■


# == Physical Plan ==
# AdaptiveSparkPlan isFinalPlan=false
# +- Sort [avg_marks#12 DESC], true
# +- Exchange rangepartitioning(avg_marks#12 DESC)
# +- HashAggregate(keys=[department#2], functions=[avg(marks#4)])
# +- Exchange hashpartitioning(department#2, 200)
# +- HashAggregate(keys=[department#2], functions=...)
# +- Scan ExistingRDD[student_id#0, name#1, ...]

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 15
9. RESULTS AND OBSERVATIONS

9.1 Summary of Query Results

Query Records
Query Description Key Observation
No. Returned

Q1 SELECT All Records 15 All student data retrieved correctly

Q2 Marks > 80 (WHERE) 8 53% of students scored above 80

AVG Marks by Dept (GROUP


Q3 5 CSE has highest average (87.60)
BY)

Q4 Top 3 Students (LIMIT 3) 3 Logesh Murugan ranked 1st with 97

Q5 Grade Distribution (HAVING) 6 O and A+ are most frequent grades

Q6 CSE with Grade O 2 Two CSE students earned top grade

Q7 Avg CGPA (DataFrame API) 5 CSE leads CGPA with 8.94 average

Q8 CASE WHEN (Categorise) 15 4 distinctions, 10 passes, 1 fail


Table 9.1 – Summary of Experimental Results

9.2 Key Observations


• Performance: Spark SQL executed all queries in under 3 seconds on a local single-node setup with 15
records. In a distributed cluster, this scales linearly with data size.
• SQL Compatibility: Standard ANSI SQL syntax (SELECT, WHERE, GROUP BY, HAVING, ORDER BY,
LIMIT, CASE WHEN) worked without modification in Spark SQL.
• Schema Enforcement: Explicitly defining the schema with StructType prevented data type mismatches
and ensured reliable query results.
• Lazy Evaluation: Spark did not execute any computation until the .show() action was called.
Transformations like WHERE and GROUP BY only build a query plan.
• Catalyst Optimisation: The query plan showed automatic predicate pushdown and aggregate merging,
demonstrating Spark SQL's built-in intelligent optimisation.
• Interoperability: The DataFrame API and SQL API produced identical results, confirming that both
interfaces are backed by the same execution engine.
• CSE Department Dominance: CSE students showed the highest average marks (87.60) and CGPA
(8.94) among all departments in the dataset.

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 16
10. ADVANTAGES AND LIMITATIONS OF SPARK SQL

10.1 Advantages
• High Speed: Processes data in-memory, making it up to 100× faster than Hadoop MapReduce for iterative
computations.
• Familiar SQL Syntax: SQL developers can immediately use Spark SQL without learning a completely new
language or API.
• Unified API: Works seamlessly with other Spark modules (Streaming, MLlib, GraphX) within a single
application.
• Multiple Data Source Support: Natively reads CSV, JSON, Parquet, ORC, Avro, Hive, JDBC, and Delta
Lake.
• Catalyst Optimiser: Automatically rewrites and optimises queries, reducing developer effort for
performance tuning.
• Scalability: Scales from a single laptop to a cluster of thousands of nodes without code changes.
• Schema Evolution: Supports schema evolution with formats like Parquet and Delta Lake.
• Language Flexibility: Available in Python (PySpark), Scala, Java, and R.
• ANSI SQL Compliance: Supports standard SQL features including window functions, subqueries, CTEs,
and user-defined functions (UDFs).
• Open Source and Free: Apache-licensed, with a large active community and extensive documentation.

10.2 Limitations
• High Memory Requirement: In-memory processing requires substantial RAM. Memory-intensive jobs
may spill to disk, reducing performance.
• Not Ideal for Small Datasets: The overhead of setting up a SparkSession and distributing tasks is
unnecessary for datasets that fit in a single machine's memory.
• Complex Setup: Configuring a production Spark cluster (standalone, YARN, Kubernetes) requires
significant DevOps expertise.
• Latency: Spark is optimised for batch and micro-batch processing. For sub-second real-time queries,
dedicated systems like Apache Flink or Druid are preferred.
• Debugging Difficulty: Distributed execution makes it harder to debug errors compared to local
single-threaded programs.
• No Transactional Support: Standard Spark SQL does not support ACID transactions (though Delta Lake
and Iceberg add this capability).
• Limited UPDATE/DELETE: Direct UPDATE or DELETE SQL operations on DataFrames are not natively
supported without Delta Lake.

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 17
11. REAL-WORLD APPLICATIONS OF SPARK SQL

# Application Domain Description

Amazon, Flipkart, and Alibaba use Spark SQL to analyse billions of transaction
1 E-Commerce Analytics records daily — computing product recommendations, sales trends, inventory
forecasts, and customer segmentation at scale.

Banks like HDFC, Axis, and global institutions use Spark SQL to query millions
2 Financial Fraud Detection of transactions in real time, identifying suspicious patterns, anomalies, and
fraudulent activities before they cause damage.

Hospital networks and pharmaceutical companies use Spark SQL to query


3 Healthcare Data Analysis large electronic health records (EHR) datasets, analyse patient outcomes, drug
efficacy, and disease progression trends.

Facebook (Meta), LinkedIn, and Twitter process petabytes of user interaction


4 Social Media Analytics data using Spark SQL to measure engagement, detect spam, personalise
content feeds, and run A/B experiments.

Telecom providers use Spark SQL to analyse call detail records (CDR), detect
5 Telecom Network Analytics network outages, monitor QoS metrics, and predict customer churn from usage
patterns.

Tech companies like Netflix and Uber use Spark SQL to query terabytes of
6 Log Analytics server log files daily, monitoring application performance, detecting errors, and
analysing system behaviour.

Smart city platforms and industrial IoT systems use Spark SQL to query sensor
7 IoT Data Processing streams from thousands of devices, computing aggregates like average
temperature, peak load, and anomaly detection.

Enterprises integrate Spark SQL with BI tools like Tableau, Power BI, and
8 Business Intelligence Looker through JDBC/ODBC connectors, enabling analysts to run SQL reports
over petabyte-scale data warehouses.
Table 11.1 – Real-World Applications of Spark SQL

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 18
12. CONCLUSION
This experiment successfully demonstrated the capabilities of Apache Spark SQL for structured data
processing using PySpark. Starting from the installation of PySpark and the creation of a SparkSession, we
progressed through defining a schema, loading a realistic student dataset, and registering it as a temporary
SQL view.

A total of eight diverse SQL queries were executed, covering fundamental operations such as SELECT,
WHERE-based filtering, GROUP BY with aggregate functions (AVG, COUNT, MAX, MIN), ORDER BY
sorting, LIMIT, HAVING, and conditional CASE WHEN logic. The same operations were also demonstrated
using the DataFrame API, confirming the interoperability of both programming styles within Spark SQL.

The experiment validated that Spark SQL provides a powerful, familiar, and highly scalable interface for big
data analytics. The Catalyst Optimiser automatically generated efficient execution plans, and lazy evaluation
ensured that computations were only triggered when an action (such as .show()) was called.

The study of query execution plans using .explain() provided insight into how Spark internally handles
distributed query processing, including hash aggregation, range partitioning, and adaptive query execution
(AQE).

In conclusion, Spark SQL is a foundational tool in the modern big data ecosystem. Mastering it equips
computer science engineers with the skills to build scalable data pipelines, analytics platforms, and machine
learning workflows that operate on datasets ranging from megabytes to petabytes.

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 19
13. VIVA QUESTIONS WITH ANSWERS
The following questions are commonly asked during viva examinations related to this experiment. Study them
carefully.

Q1. What is Apache Spark, and how is it different from Hadoop MapReduce?
Ans: Apache Spark is an open-source distributed computing framework that processes data primarily in
memory (RAM), making it significantly faster than Hadoop MapReduce, which reads and writes
intermediate data to HDFS disk at every step. Spark also provides a unified API for batch, streaming, SQL,
and machine learning workloads, whereas Hadoop MapReduce is limited to batch processing.

Q2. What is Spark SQL? What are its main features?


Ans: Spark SQL is a Spark module for structured data processing that allows users to query data using
SQL syntax and the DataFrame API. Key features include the Catalyst Optimiser for automatic query
optimisation, Tungsten execution engine for efficient CPU and memory use, support for multiple data
formats (CSV, JSON, Parquet, ORC), and seamless integration with other Spark modules.

Q3. What is a SparkSession? Why is it important?


Ans: SparkSession is the unified entry point to all Spark SQL functionality introduced in Spark 2.0. It
replaces the older SQLContext, HiveContext, and SparkContext. Through SparkSession, developers can
create DataFrames, register views, execute SQL queries, and access the underlying SparkContext and
catalog.

Q4. What is a DataFrame in Spark SQL?


Ans: A DataFrame is a distributed collection of data organised into named columns, similar to a table in a
relational database or a pandas DataFrame in Python. It provides a high-level API for data manipulation
and is backed by an immutable distributed dataset (RDD). DataFrames support both SQL queries and
functional API operations.

Q5. Explain the concept of lazy evaluation in Spark.


Ans: Lazy evaluation means Spark does not immediately execute an operation when it is defined.
Transformations (like filter(), groupBy(), select()) only build a logical plan. Execution begins only when an
action (like show(), count(), collect(), write()) is called. This allows Spark's Catalyst Optimiser to analyse
the entire pipeline and choose the most efficient execution strategy.

Q6. What is the Catalyst Optimiser in Spark SQL?


Ans: The Catalyst Optimiser is Spark SQL's query optimisation framework. It converts a SQL query or
DataFrame API call into an optimised physical execution plan through four stages: (1) Parsing — creates
an unresolved logical plan, (2) Analysis — resolves column references against the catalog, (3) Logical
Optimisation — applies rules like predicate pushdown and constant folding, (4) Physical Planning —
selects the lowest-cost execution strategy.

Q7. What is a temporary view in Spark SQL? How do you create one?
Ans: A temporary view is a named SQL alias for a DataFrame that exists only within the current
SparkSession. It allows SQL queries to reference the DataFrame by name. It is created using:
[Link]('view_name'). The view disappears when the SparkSession ends.

Q8. What is the difference between createOrReplaceTempView() and createGlobalTempView()?

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 20
Ans: createOrReplaceTempView() creates a view visible only within the current SparkSession.
createGlobalTempView() creates a view that persists across multiple SparkSessions within the same
Spark application, accessible via the 'global_temp' database prefix (e.g., SELECT * FROM
global_temp.view_name).

Q9. What is PySpark? How is it related to Apache Spark?


Ans: PySpark is the official Python API for Apache Spark. It provides Python bindings to the core Spark
engine, allowing developers to write Spark applications in Python instead of Scala or Java. PySpark uses
Py4J to bridge communication between Python and the JVM where Spark runs.

Q10. Name four SQL aggregate functions supported by Spark SQL with examples.
Ans: AVG(marks) — computes the average marks of students. COUNT(*) — counts the total number of
rows. MAX(cgpa) — returns the highest CGPA value. MIN(marks) — returns the lowest marks.
SUM(marks) — returns the total sum of all marks values.

Q11. What is the purpose of the HAVING clause? How is it different from WHERE?
Ans: WHERE filters individual rows before grouping occurs. HAVING filters groups after GROUP BY has
aggregated the data. For example: WHERE marks > 80 filters individual students, whereas HAVING
COUNT(*) > 2 filters only those departments having more than 2 students.

Q12. What file formats does Spark SQL natively support for reading and writing data?
Ans: Spark SQL natively supports: CSV (comma-separated values), JSON (JavaScript Object Notation),
Parquet (columnar storage format — default in Spark), ORC (Optimised Row Columnar), Avro (row-based
binary format), Text files, Hive tables, JDBC/ODBC databases, and Delta Lake (with the delta package).

Q13. What does .explain() do in Spark SQL?


Ans: .explain() displays the physical execution plan that Spark generates for a DataFrame or SQL query.
It shows stages like Scan (reading data), Filter, HashAggregate, Exchange (data shuffle between nodes),
Sort, and Project. Using .explain(extended=True) shows all four plan stages: parsed, analysed, optimised
logical, and physical plans.

Q14. What is the difference between RDD, DataFrame, and Dataset in Spark?
Ans: RDD (Resilient Distributed Dataset) is the low-level, unstructured distributed collection with no
schema — type-safe in Scala/Java but verbose. DataFrame is a structured, tabular RDD with named
columns and schema, similar to a SQL table — available in all languages but not type-safe in Python.
Dataset combines the benefits of both — structured schema and compile-time type safety — but is only
available in Scala and Java, not Python.

Q15. How would you write the output of a Spark SQL query to a CSV file?
Ans: Use the write API: [Link]('overwrite').option('header', True).csv('/path/to/output/folder').
The mode can be 'overwrite', 'append', 'ignore', or 'errorIfExists'. Spark writes one CSV file per partition, so
coalesce(1) or repartition(1) can be used to produce a single file.

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 21
14. REFERENCES
[1] Apache Spark Official Documentation – [Link]

[2] Holden Karau, Andy Konwinski, Patrick Wendell, and Matei Zaharia, Learning Spark: Lightning-Fast Data
Analytics, 2nd ed., O'Reilly Media, 2020.

[3] Bill Chambers and Matei Zaharia, Spark: The Definitive Guide, O'Reilly Media, 2018.

[4] PySpark API Reference – [Link]

[5] Databricks Blog – Spark SQL Performance Tuning – [Link]

[6] M. Zaharia et al., 'Apache Spark: A Unified Engine for Big Data Processing,' Communications of the
ACM, vol. 59, no. 11, pp. 56–65, Nov. 2016.

[7] Google Colab – [Link] (for running PySpark without local installation)

[8] W3Schools SQL Tutorial – [Link]

Student Signature Faculty Signature Date


Marks Awarded

___________________ ___________________ ___________________


_________ / 100
______ ______ ______

Spark SQL – Hands-on Tryout | Anjalai Ammal Mahalingam Engineering College Page 22

You might also like