from_xml
Analisa uma coluna contendo strings XML e a converte em uma linha com o esquema especificado. Retorna null, no caso de strings não analisáveis.
Sintaxe
from pyspark.sql import functions as sf
sf.from_xml(col, schema, options=None)
Parâmetros
Parâmetro | Tipo | Descrição |
|---|---|---|
|
| Uma coluna ou nome de coluna em formato XML. |
|
| Um StructType, Column ou literal de strings Python com strings formatadas em DDL para usar ao analisar a coluna XML. |
| dicionário, opcional | Opções para controlar a análise sintática. Aceita as mesmas opções que a fonte de dados XML. |
Devoluções
pyspark.sql.Column: uma nova coluna de tipo complexo a partir de um objeto XML fornecido.
Exemplos
Exemplo 1 : Analisando XML com um esquema de strings formatadas em DDL
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))]
Exemplo 2 : Analisando XML com um esquema StructType
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}|
+---------------+
Exemplo 3 : Analisando XML com ArrayType no esquema
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]))]
Exemplo 4 : Analisando XML usando 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]))]