json(DataFrameWriter)

將內容 DataFrame 以 JSON 格式(JSON 行數/換行分隔 JSON)儲存在指定路徑。

語法

json(path, mode=None, compression=None, dateFormat=None, timestampFormat=None,
     lineSep=None, encoding=None, ignoreNullFields=None)

參數

參數 類型 說明
path str 任何支援 Hadoop 的檔案系統中的路徑。
mode 力量,選用 當資料已經存在時的行為。 接受的值為 'append''overwrite''ignore''error''errorifexists' (預設值)。

退貨

沒有

Examples

把 DataFrame 寫進 JSON 檔案,然後讀回來。

import tempfile
with tempfile.TemporaryDirectory(prefix="json") as d:
    spark.createDataFrame(
        [{"age": 100, "name": "Alice"}]
    ).write.json(d, mode="overwrite")

    spark.read.format("json").load(d).show()
    # +---+------------+
    # |age|        name|
    # +---+------------+
    # |100|Alice|
    # +---+------------+