Вы должны хранить XML данные как тип варианта в Снежинке. В настоящее время Snowflake не поддерживает выражения xpath, поэтому вы можете использовать функцию XMLGET. Если вам нужно углубиться в n-уровни в XML, вы можете вложить такую функцию, как:
select xmlget(xmlget(xmlget(XML_FILE, 'Student', 1), 'course'), 'gym'):"$"::string as GRADE from XML_TEST;
Я написал краткое руководство для нескольких моих клиентов из источников, аннотированных в SQL рабочий лист. Он пытается охватить ключевые концепции быстрее, чем подробно. Если вам нужно больше подробностей, я рекомендую ссылки в комментариях SQL.
--https://community.snowflake.com/s/article/Querying-Nested-XML-in-Snowflake
--https://community.snowflake.com/s/article/How-To-Lateral-Join-Tutorial
--https://snowflakecommunity.force.com/s/article/introduction-to-loading-and-parsing-xml-data-using-sql
-- Set the context
use warehouse test;
use database test;
use schema public;
-- For convenience, we'll create a simple function to return a hard-coded XML document.
-- Note that the document is three layers deep from the root,
-- for example SchoolRoster/Student/Course/Math
create or replace function GetSampleXML ()
returns string
language javascript
as
$$
var s =
`<SchoolRoster>
<Student>
<name>John</name>
<age>14</age>
<course>
<math>A</math>
<english>B</english>
</course>
<course>
<government>A+</government>
</course>
</Student>
<Student>
<name>Tom</name>
<age>13</age>
<course>
<gym>A</gym>
<geography>incomplete</geography>
</course>
</Student>
</SchoolRoster>`;
return s;
$$
;
-- Confirm that we can select the user-defined function, and it returns the XML document.
select GetSampleXML() as XML_FILE;
-- Create a simple table that has just a single variant column to hold the XML.
create or replace table XML_TEST (XML_FILE variant);
-- Insert the sample XML document as a variant into the fist row.
insert into XML_TEST (XML_FILE) select PARSE_XML(GetSampleXML());
-- Confirm that the table looks right, one row containing the XML document
select * from XML_TEST;
-- Get the un-flattened student information for the first student (0 based).
select xmlget(XML_FILE, 'Student', 0):"$" from XML_TEST;
-- Break up the XML file into the top-level nodes, one row for "John" and one for "Tom"
select FLAT.VALUE
from XML_TEST X,
LATERAL FLATTEN( INPUT => XML_FILE:"$" ) FLAT;
-- Get a value three layers deep on the SECOND student (0 based)
select xmlget(xmlget(xmlget(XML_FILE, 'Student', 1), 'course'), 'gym'):"$"::string as GRADE from XML_TEST;