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 |
|---|---|---|
| StructType or str | A |
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)