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:
Snowparkスクリプト¶
これにより、XML ファイルの各 <book> 要素が独自の行にロードされ、子要素(例: <title> および <author>)が VARIANT タイプ の列として自動的に抽出されます。
出力¶
|
|
|
|
|
|
|---|---|---|---|---|---|
"2" |
"John Smith" |
|
"35.50" |
|
"XML for Data Engineers" |
"1" |
"Jane Doe" |
|
"29.99" |
|
"The Art of Snowflake" |
rowTagによって識別される各 XML 要素が1行になります。そのタグ内の各サブ要素は列になり、
VARIANTとして保存されます。ネストされた要素はネストされたVARIANTデータとしてキャプチャされます。The resulting DataFrame is flattened and columnized and behaves like any other Snowpark DataFrame.
はじめるにあたり¶
Snowpark Pythonパッケージをインストールします。
Snowflakeステージに XML ファイルをアップロードします。
Snowparkを使用して XML ファイルを読み取ります。
DataFrame メソッドを使用して変換または保存します。
サポート対象のオプション¶
rowTag(必須):行として抽出する XML 要素の名前。:code:`rowValidationXSDPath`(オプション):ロード中にrowTagフラグメントを検証するために使用される XSD へのステージパス。
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 をご参照ください。
Validate XML using XSD¶
To validate each
rowTagfragment against an XSD during load, set the XSD path and choose a validation mode:
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).
FAILFAST:読み取りは最初の行で停止し、エラーを返します。
制限事項¶
Snowpark XMLRowTag リーダーには次の制限があります:
スキーマを推測せず、出力列はすべて
VARIANTの型です。Only supports files stored in Snowflake stages; local files are not supported.
Snowpark Pythonライブラリでのみ利用可能です。