載入

從資料來源載入資料並以 DataFrame.

語法

load(path=None, format=None, schema=None, **options)

參數

參數 類型 說明
path 力量或列表,選擇性 檔案系統支援資料來源中的一條或多條路徑。
format 力量,選用 資料來源格式。 預設為 'parquet'
schema 結構類型或力量,選用 輸入結構作為 StructType 物件或 DDL 格式的字串(例如 'col0 INT, col1 DOUBLE')。
**options dict 額外的弦線選擇。

退貨

DataFrame

Examples

載入一個格式、結構和選項的 CSV 檔案。

import tempfile
with tempfile.TemporaryDirectory(prefix="load") as d:
    df = spark.createDataFrame([{"age": 100, "name": "Alice"}])
    df.write.option("header", True).mode("overwrite").format("csv").save(d)

    df = spark.read.load(
        d, schema=df.schema, format="csv", nullValue="Alice", header=True)
    df.printSchema()
    # root
    #  |-- age: long (nullable = true)
    #  |-- name: string (nullable = true)
    df.show()
    # +---+----+
    # |age|name|
    # +---+----+
    # |100|NULL|
    # +---+----+