beginner

Defining Schemas

8 min readLast updated: 2026-07-08

Overview

Learn how to define schemas programmatically using Spark's StructType and StructField classes. This improves query performance and ensures data quality.

What You Will Learn

In this lesson, you will learn:
  • Programmatic Schemas: Using StructType and StructField.
  • Data Types: Working with StringType, IntegerType, and DoubleType.
  • Enforcing Quality: Restricting nulls and verifying schema boundaries.

Detailed Concept Explanation

Instead of letting Spark guess data types (via inferSchema), you can define your schema programmatically. This is called a User-Defined Schema.

Spark represents schemas using three core classes:

  1. StructType: Represents the overall table structure (a collection of fields).
  2. StructField: Represents a single column, specifying its name, data type, and whether it can contain null values.
  3. DataType: The specific type of the column (e.g. IntegerType, StringType, DoubleType).

By passing this schema to your reader (spark.read.schema(customSchema)), you save Spark from executing a pre-scan over your data.


Code Examples

Input Dataset Preview

Below is the strict schema dataset we want to load:

iditemprice
1Cup4.99
2Plate9.50

Python (PySpark) Implementation

python
from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StructField, IntegerType, StringType, DoubleType

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

# Define schema programmatically
schema = StructType([
    StructField("id", IntegerType(), False),
    StructField("item", StringType(), True),
    StructField("price", DoubleType(), True)
])

# Load data with the schema
data = [(1, "Cup", 4.99), (2, "Plate", 9.50)]
df = spark.createDataFrame(data, schema)
df.printSchema()

Expected Output

Spark builds the schema exactly as defined:

text
root
 |-- id: integer (nullable = false)
 |-- item: string (nullable = true)
 |-- price: double (nullable = true)

Execution Plan Diagram (Python & Scala)

Execution Plan Diagram
Define StructType Schema
StructField(id
IntegerType)
StructField(item
StringType)
createDataFrame
printSchema()

Scala Implementation

scala
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.types._

val spark = SparkSession.builder().appName("SchemaScala").getOrCreate()

val schema = StructType(Array(
  StructField("id", IntegerType, nullable = false),
  StructField("item", StringType, nullable = true),
  StructField("price", DoubleType, nullable = true)
))

val df = spark.createDataFrame(
  spark.sparkContext.parallelize(Seq(Row(1, "Cup", 4.99), Row(2, "Plate", 9.50))),
  schema
)
df.printSchema()

Expected Output

text
root
 |-- id: integer (nullable = false)
 |-- item: string (nullable = true)
 |-- price: double (nullable = true)

SQL Implementation

sql
-- Defining a schema inline inside SQL DDL
CREATE TABLE products_catalog (
    id INT,
    item STRING,
    price DOUBLE
) USING parquet;

Expected Output

Registers an SQL catalog table with specified schema types.


Common Mistakes

  • Data Type Mismatches: If you pass a programmatic schema specifying a column is an IntegerType but the raw file contains string text (e.g. "N/A"), Spark may fill that column with nulls.

Best Practices

  • Use Nullable Flag Safely: Mark the nullable flag inside StructField as true unless you are absolutely sure the source files will never contain missing values.

Interview Perspective

What are the benefits of defining schemas programmatically in Spark?
  1. Performance: Bypasses the file scanning cost of inferSchema.
  2. Data Quality: Enforces strict column data types, avoiding runtime failures.
  3. Schema Evolution: Helps handle missing or renamed fields gracefully.

Interactive Challenges

Challenge 1: Create StructField (Beginner)

Define a Python StructField named 'email' of type StringType that allows null values.

Related Topics