XML 문자열이 포함된 열을 지정된 스키마에 따라 행으로 변환합니다.
null분리할 수 없는 문자열의 경우를 반환합니다.
문법
from pyspark.sql import functions as sf
sf.from_xml(col, schema, options=None)
매개 변수
| 매개 변수 | 유형 | Description |
|---|---|---|
col |
pyspark.sql.Column 또는 str |
XML 형식의 열 또는 열 이름입니다. |
schema |
StructType또는 pyspark.sql.Column str |
Xml 열을 구문 분석할 때 사용할 DDL 형식 문자열이 있는 StructType, Column 또는 Python 문자열 리터럴입니다. |
options |
dict, 선택 사항 | 구문 분석을 제어하는 옵션입니다. Xml 데이터 원본과 동일한 옵션을 허용합니다. |
Returns
pyspark.sql.Column: 지정된 XML 개체에서 복합 형식의 새 열입니다.
예시
예제 1: DDL 형식 문자열 스키마를 사용하여 XML 구문 분석
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))]
예제 2: 스키마를 사용하여 StructType XML 구문 분석
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}|
+---------------+
예제 3: XML을 스키마로 ArrayType 구문 분석
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]))]
예제 4: 다음을 사용하여 XML 구문 분석 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]))]