beginner

CSV

8 min readLast updated: 2026-07-08

Overview

CSV (Comma-Separated Values) is a common text-based file format. Learn how to read and write CSV files in Spark, configure separators, and handle headers.

What You Will Learn

In this lesson, you will learn:
  • CSV Reader: Loading CSV files with specific configurations.
  • Separator Options: Setting custom delimiters (e.g. tabs or pipes).
  • Schema Options: Toggling headers and escaping quotes.

Detailed Concept Explanation

CSV is a plain-text format where each line represents a row and columns are separated by a delimiter (default is a comma ,).

Because CSV is a flat text format, it does not store schema metadata. When reading CSVs, Spark must either guess the column types (inferSchema) or consume an explicit StructType schema.

Key reader options:

  • header: Set true if the first row contains column names.
  • sep or delimiter: Configures column separators (e.g. \t for tab, | for pipe).
  • multiLine: Set true if rows span multiple lines.

Code Examples

Input Dataset Preview

Below is the users file users.csv:

text
id,name,city
1,Alice,London
2,Bob,New York

Python (PySpark) Implementation

python
from pyspark.sql import SparkSession

spark = SparkSession.builder.appName("CSVRead").getOrCreate()

# Read CSV with headers and commas
df = spark.read.format("csv") \
    .option("header", "true") \
    .option("sep", ",") \
    .load("users.csv")
df.show()

Expected Output

text
+---+-----+--------+
| id| name|    city|
+---+-----+--------+
|  1|Alice|  London|
|  2|  Bob|New York|
+---+-----+--------+

Execution Plan Diagram (Python & Scala)

Execution Plan Diagram
SparkSession.builder
read.format(csv)
option(header
true)
load()
show()

Scala Implementation

scala
import org.apache.spark.sql.SparkSession

val spark = SparkSession.builder().appName("CSVScala").getOrCreate()
val df = spark.read.format("csv")
  .option("header", "true")
  .option("sep", ",")
  .load("users.csv")
df.show()

Expected Output

text
+---+-----+--------+
| id| name|    city|
+---+-----+--------+
|  1|Alice|  London|
|  2|  Bob|New York|
+---+-----+--------+

SQL Implementation

sql
SELECT id, name, city 
FROM csv.`users.csv`;

Expected Output

Executing this SQL query returns:

idnamecity
1AliceLondon
2BobNew York

Common Mistakes

  • Forgetting multiLine for Line Breaks: Attempting to read CSV files where columns contain raw line breaks without setting multiLine to true. This causes Spark to break rows incorrectly, corrupting the dataset.

Best Practices

  • Explicit Delimiters: Always specify the sep option explicitly, even for commas, to ensure compatibility.

Interview Perspective

What are the performance limitations of using CSV files in Spark?

CSV is a row-oriented, text-based format. Spark cannot skip rows or columns when reading CSVs; it must read and parse the entire file line-by-line. Additionally, because CSV contains no metadata, Spark must execute expensive schema inference or parse custom schemas.


Interactive Challenges

Challenge 1: Read Tab-Separated File (Beginner)

Which option key and value should you set to read a file where columns are separated by tabs?

Related Topics