Using the Snowpark XML RowTag Reader¶
You can activate the Snowpark XML RowTag Reader by specifying .option("rowTag", "<rowtag>") in session.read.option("rowTag", "<rowtag>").xml(). Instead of loading the entire document as a single object, this mode splits the file based on the specified rowTag, loads each matching element as a separate row, and splits each row into multiple columns in a Snowpark DataFrame. The Reader is especially useful for processing only selective elements in XML files or ingesting large XML files in a scalable, Snowpark-native way.
예¶
This sample XML is an example:
<library>
<book id="1">
<title>The Art of Snowflake</title>
<author>Jane Doe</author>
<price>29.99</price>
<reviews>
<review>
<user>tech_guru_87</user>
<rating>5</rating>
<comment>Very insightful and practical.</comment>
</review>
<review>
<user>datawizard</user>
<rating>4</rating>
<comment>Great read for data engineers.</comment>
</review>
</reviews>
<editions>
<edition year="2023" format="Hardcover"/>
<edition year="2024" format="eBook"/>
</editions>
</book>
<book id="2">
<title>XML for Data Engineers</title>
<author>John Smith</author>
<price>35.50</price>
<reviews>
<review>
<user>xml_master</user>
<rating>5</rating>
<comment>Perfect for mastering XML parsing.</comment>
</review>
</reviews>
<editions>
<edition year="2022" format="Paperback"/>
</editions>
</book>
</library>
Snowpark 스크립트¶
df = session.read.option("rowTag", "book").xml("@mystage/books.xml")
이를 통해 XML 파일의 각 <book> 요소가 자체 행으로 로드되고 하위 요소(예: <title> 및 <author>)가 열 유형 :code:`VARIANT`로 자동 추출됩니다.
출력¶
|
|
|
|
|
|
|---|---|---|---|---|---|
“2” |
“John Smith” |
|
“35.50” |
|
“XML for Data Engineers” |
“1” |
“Jane Doe” |
|
“29.99” |
|
“The Art of Snowflake” |
``rowTag``로 식별되는 각 XML 요소가 한 행이 됩니다.
해당 태그 내의 각 하위 요소는
VARIANT`로 저장되는 열이 됩니다. 중첩된 요소는 중첩된 :code:`VARIANT데이터로 캡처됩니다.The resulting DataFrame is flattened and columnized and behaves like any other Snowpark DataFrame.
시작하기¶
Snowpark Python 패키지 설치:
pip install snowflake-snowpark-python
XML 파일을 Snowflake 스테이지에 업로드합니다.
PUT file:///path/to/books.xml @mystage;
Snowpark를 사용하여 XML 파일 읽기:
df = session.read.option("rowTag", "book").xml("@mystage/books.xml")
DataFrame 메서드를 사용하여 변환 또는 저장:
df.select(col("`title`"), col("`author`")).show() df.write.save_as_table("books_table")
지원되는 옵션¶
:code:`rowTag`(필수): 행으로 추출할 XML 요소의 이름입니다.
:code:`rowValidationXSDPath`(선택 사항): 로드 중에 각 rowTag 조각의 유효성을 검사하는 데 사용되는 XSD에 대한 스테이지 경로입니다.
:code:`mode`(선택 사항): 기본 동작은 유효성 검사 없이 로드됩니다. :code:`rowValidationXSDPath`가 설정된 경우:
PERMISSIVE: Quarantines invalid rows in_corrupt_record; loads the rest.FAILFAST: Stops at the first invalid row and raises an error.
XML 옵션에 대한 자세한 내용은 `Snowflake.snowpark.DataFrameReader.xml</developer-guide/snowpark/reference/python/latest/snowpark/api/snowflake.snowpark.DataFrameReader.xml>`_을 참조하세요.
Validate XML using XSD¶
To validate each
rowTagfragment against an XSD during load, set the XSD path and choose a validation mode:df = ( session.read .option("rowTag", "book") .option("rowValidationXSDPath", "@mystage/schema.xsd") # validates each row element .option("mode", "PERMISSIVE") # or "FAILFAST" .xml("@mystage/books.xml") )
PERMISSIVE: Invalid rows are quarantined in a special _corrupt_record column; valid rows load normally.
To persist the result, write the DataFrame to a table with
df.write.save_as_table("<table_name>"). The table will include all parsed columns plus an extra_corrupt_recordcolumn: it isNULLfor valid rows and contains the full XML records for invalid rows (with the other columns showingNULL).+-------------------+ | _corrupt_record | | <book id="1"> ... | | <book id="2"> ... | +-------------------+
FAILFAST: 읽기는 문제가 되는 첫 번째 행에서 중지되고 오류를 반환합니다.
제한 사항¶
Snowpark XML RowTag Reader에는 다음과 같은 제한 사항이 있습니다.
스키마를 유추하지 않으며 출력 열이 모두
VARIANT유형입니다.Only supports files stored in Snowflake stages; local files are not supported.
Snowpark Python 라이브러리에서만 사용할 수 있습니다.