Skip to main content

schema

Specifies the input schema. Some data sources (such as JSON) can infer the input schema automatically from data. By specifying the schema here, the underlying data source can skip the schema inference step, which speeds up data loading.

Syntax

schema(schema)

Parameters

Parameter

Type

Description

schema

StructType or str

A StructType object or a DDL-formatted string (for example, 'col0 INT, col1 DOUBLE').

Returns

DataFrameReader

Examples

Specify the schema when reading a CSV file.

Python
import tempfile
with tempfile.TemporaryDirectory(prefix="schema") as d:
spark.read.schema("col0 INT, col1 DOUBLE").format("csv").load(d).printSchema()
# root
# |-- col0: integer (nullable = true)
# |-- col1: double (nullable = true)