指定輸入結構。 有些資料來源(例如 JSON)可以自動從資料推斷出輸入結構。 在此指定結構,資料來源可跳過結構推論並加速資料載入。
語法
schema(schema)
參數
| 參數 | 類型 | 說明 |
|---|---|---|
schema |
結構類型或力量 | 一個 StructType 物件或一個 DDL 格式的字串(例如, col0 INT, col1 DOUBLE)。 |
退貨
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 ...>
為 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)