xml(DataStreamReader)

載入 XML 檔案串流,並以資料幀形式回傳結果。 若 schema 未指定,則從資料推斷輸入結構。

語法

xml(path, schema=None, **options)

參數

參數 類型 說明
path str XML 輸入的路徑。
schema 結構類型或力量,選用 Schema 作為 StructType 或 DDL 格式的字串(例如 col0 INT, col1 DOUBLE)。

退貨

DataFrame

Examples

將資料框架寫入 XML,並以串流形式讀取:

import tempfile
import time
with tempfile.TemporaryDirectory(prefix="xml") as d:
    spark.createDataFrame(
        [{"age": 100, "name": "Hyukjin Kwon"}]
    ).write.mode("overwrite").option("rowTag", "person").xml(d)
    q = spark.readStream.schema(
        "age INT, name STRING"
    ).xml(d, rowTag="person").writeStream.format("console").start()
    time.sleep(3)
    q.stop()