Nota
O acesso a esta página requer autorização. Podes tentar iniciar sessão ou mudar de diretório.
O acesso a esta página requer autorização. Podes tentar mudar de diretório.
Analisa uma coluna contendo uma string XML para uma linha conforme o esquema especificado. Retorna null, no caso de uma cadeia não analisável.
Sintaxe
from pyspark.sql import functions as sf
sf.from_xml(col, schema, options=None)
Parâmetros
| Parâmetro | Tipo | Description |
|---|---|---|
col |
pyspark.sql.Column ou str |
Uma coluna ou nome de coluna em formato XML. |
schema |
StructType, pyspark.sql.Column ou str |
Um literal StructType, Column ou string Python com uma string formatada em DDL para usar ao analisar a coluna Xml. |
options |
Dit, 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 do dado objeto XML.
Examples
Exemplo 1: Análise de XML com um esquema de strings formatado 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: Análise de XML com um StructType esquema
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: Analisar XML com ArrayType in schema
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: Análise de 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]))]