schema (DataStreamReader)

Określa schemat wejściowy. Niektóre źródła danych (na przykład JSON) mogą automatycznie wnioskować schemat wejściowy z danych. Określenie schematu pozwala źródle danych pominąć wnioskowanie schematu i przyspieszyć ładowanie danych.

Składnia

schema(schema)

Parametry

Parameter Typ Opis
schema Typ struktury lub str Obiekt StructType lub ciąg w formacie DDL (na przykład col0 INT, col1 DOUBLE).

Zwroty

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 ...>

Określ inny schemat dla pliku 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)