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.
Beispiel¶
This sample XML is an example:
Snowpark-Skript¶
Dieses lädt jedes <book>-Element aus der XML-Datei in die zugehörige eigene Zeile mit untergeordneten Elementen (z. B. <title> und <author>), die automatisch als Spalten vom Typ VARIANT extrahiert werden.
Ausgabe¶
|
|
|
|
|
|
|---|---|---|---|---|---|
„2“ |
„Joan Smith“ |
|
„35,50“ |
|
„XML für Dateningenieure“ |
„1“ |
„Jane Doe“ |
|
„29,99“ |
|
„The Art of Snowflake“ |
Jedes XML-Element, das durch
rowTagidentifiziert wird, wird zu einer Zeile.Jedes Unterelement innerhalb dieses Tags wird zu einer Spalte, die als
VARIANTgespeichert wird. Verschachtelte Elemente werden als verschachtelteVARIANT-Daten erfasst.The resulting DataFrame is flattened and columnized and behaves like any other Snowpark DataFrame.
Erste Schritte¶
Installieren der Snowpark Python-Bibliothek:
Laden Sie Ihre XML-Datei in einen Snowflake-Stagingbereich hoch:
Verwenden Sie Snowpark, um die XML-Datei zu lesen:
Verwenden Sie DataFrame-Methoden zum Transformieren oder Speichern:
Unterstützte Optionen¶
rowTag(Erforderlich): Der Name des XML-Elements, das als Zeile extrahiert werden soll.rowValidationXSDPath(Optional): Stagingbereichspfad zu einer XSD, die verwendet wird, um jedes rowTag-Fragment während des Ladens zu validieren.mode(Optional): Das Standardverhalten wird ohne Validierung geladen. WennrowValidationXSDPathfolgendermaßen eingestellt ist:PERMISSIVE: Quarantines invalid rows in_corrupt_record; loads the rest.FAILFAST: Stops at the first invalid row and raises an error.
Weitere Informationen zu XML-Optionen finden Sie unter 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: Der Lesevorgang wird bei der ersten fehlerhaften Zeile angehalten und ein Fehler zurückgegeben.
Einschränkungen¶
Snowpark XML RowTag Reader hat die folgenden Einschränkungen:
Leitet nicht das Schema ab, und die Ausgabespalten sind alle vom Typ
VARIANT.Only supports files stored in Snowflake stages; local files are not supported.
Ist nur in der Snowpark Python-Bibliothek verfügbar.