Aracılığıyla paylaş


from_xml

XML dizesi içeren bir sütunu belirtilen şemaya sahip bir satıra ayrıştırıyor. nullAyrıştırılamayan bir dize durumunda döndürür.

Sözdizimi

from pyspark.sql import functions as sf

sf.from_xml(col, schema, options=None)

Parametreler

Parametre Türü Description
col pyspark.sql.Column veya str XML biçiminde bir sütun veya sütun adı.
schema StructType, pyspark.sql.Column veya str Xml sütununu ayrıştırırken kullanılacak DDL biçimli dizeye sahip StructType, Sütun veya Python dizesi değişmez değeri.
options dict, isteğe bağlı Ayrıştırma denetimi seçenekleri. Xml veri kaynağıyla aynı seçenekleri kabul eder.

İade

pyspark.sql.Column: verilen XML nesnesinden karmaşık türde yeni bir sütun.

Örnekler

Örnek 1: DDL biçimli dize şemasıyla XML ayrıştırma

import pyspark.sql.functions as sf
data = [(1, '''<p><a>1</a></p>''')]
df = spark.createDataFrame(data, ("key", "value"))
# Define the schema using a DDL-formatted string
schema = "STRUCT<a: BIGINT>"
# Parse the XML column using the DDL-formatted schema
df.select(sf.from_xml(df.value, schema).alias("xml")).collect()
[Row(xml=Row(a=1))]

Örnek 2: XML'yi şemayla StructType ayrıştırma

import pyspark.sql.functions as sf
from pyspark.sql.types import StructType, LongType
data = [(1, '''<p><a>1</a></p>''')]
df = spark.createDataFrame(data, ("key", "value"))
schema = StructType().add("a", LongType())
df.select(sf.from_xml(df.value, schema)).show()
+---------------+
|from_xml(value)|
+---------------+
|            {1}|
+---------------+

Örnek 3: XML'i şemada ile ArrayType ayrıştırma

import pyspark.sql.functions as sf
data = [(1, '<p><a>1</a><a>2</a></p>')]
df = spark.createDataFrame(data, ("key", "value"))
# Define the schema with an Array type
schema = "STRUCT<a: ARRAY<BIGINT>>"
# Parse the XML column using the schema with an Array
df.select(sf.from_xml(df.value, schema).alias("xml")).collect()
[Row(xml=Row(a=[1, 2]))]

Örnek 4: KULLANARAK XML ayrıştırma schema_of_xml

import pyspark.sql.functions as sf
# Sample data with an XML column
data = [(1, '<p><a>1</a><a>2</a></p>')]
df = spark.createDataFrame(data, ("key", "value"))
# Generate the schema from an example XML value
schema = sf.schema_of_xml(sf.lit(data[0][1]))
# Parse the XML column using the generated schema
df.select(sf.from_xml(df.value, schema).alias("xml")).collect()
[Row(xml=Row(a=[1, 2]))]