Condividi tramite


schema (DataStreamReader)

Specifica lo schema di input. Alcune origini dati , ad esempio JSON, possono dedurre automaticamente lo schema di input dai dati. La specifica dello schema consente all'origine dati di ignorare l'inferenza dello schema e velocizzare il caricamento dei dati.

Sintassi

schema(schema)

Parametri

Parametro Tipo Descrizione
schema StructType o str Oggetto StructType o stringa in formato DDL , ad esempio col0 INT, col1 DOUBLE.

Restituzioni

DataStreamReader

Examples

from pyspark.sql.types import StructField, StructType, StringType
spark.readStream.schema(StructType([StructField("data", StringType(), True)]))
# <...streaming.readwriter.DataStreamReader object ...>
spark.readStream.schema("col0 INT, col1 DOUBLE")
# <...streaming.readwriter.DataStreamReader object ...>

Specificare uno schema diverso per un file CSV:

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