将多个 tXMLMap 输出值组合到一个雪花表中

时间:2021-04-03 19:02:52

标签: xml snowflake-cloud-data-platform talend

我有以下 XML 结构作为输入

<dataexport>
<header>
     <companyinfo>
     <companyid> 100 </companyid>
     <companyname>ABC Corp</companyname>
     </companyinfo>
</header>
<deptinfo>
     <electrical>
          <response>
               <deptid> 1 </deptid>
               <totalemp> 200 </totalemp>
               <totalunits> 20 </totalunits>
          </response>
     </electrical>
     <mechanical>
          <response>
               <deptid> 2 </deptid>
               <totalemp> 150 </totalemp>
               <totalunits> 40 </totalunits>
          </response>
     </mechanical>
     <chemical>
          <response>
               <deptid> 3 </deptid>
               <totalemp> 100 </totalemp>
               <totalunits> 20 </totalunits>
          </response>
     </chemical>

我期望的输出是

<头>
公司 ID 公司名称 部门 部门 ID 总emp
100 ABC Corp 电动 1 200
100 ABC Corp 机械 2 150
100 ABC Corp 化学 3 100

我采用的设计方法如下 -

enter image description here

tXMLMap 组件配置如下 -

enter image description here

通过这种方法,我只能获得一个输出,并且无法将 deptinfo_electrical、deptinfo_mechanical 和 deptinfo_chemistry 的结果合并到一个数据流中以加载到雪花表。

请告知这里可以使用什么设计方法。

1 个答案:

答案 0 :(得分:0)

所以使用 thia CTE 和你的数据,在雪花中:

with data as (
    select parse_xml('<dataexport>
    <header>
         <companyinfo>
         <companyid> 100 </companyid>
         <companyname>ABC Corp</companyname>
         </companyinfo>
    </header>
    <deptinfo>
         <electrical>
              <response>
                   <deptid> 1 </deptid>
                   <totalemp> 200 </totalemp>
                   <totalunits> 20 </totalunits>
              </response>
         </electrical>
         <mechanical>
              <response>
                   <deptid> 2 </deptid>
                   <totalemp> 150 </totalemp>
                   <totalunits> 40 </totalunits>
              </response>
         </mechanical>
         <chemical>
              <response>
                   <deptid> 3 </deptid>
                   <totalemp> 100 </totalemp>
                   <totalunits> 20 </totalunits>
              </response>
         </chemical>
    </deptinfo>
    </dataexport>') as xml
)

这个选择让你得到你想要的值:

select xml
    ,xmlget(xmlget(xml,'header',0), 'companyinfo', 0) as comp
    ,get(xmlget(comp, 'companyid'),'$') as companyid
    ,get(xmlget(comp, 'companyname'), '$') as companyname
    ,get(d.value, '@') as depm
    ,xmlget(d.value, 'response') as r
    ,get(xmlget(r, 'deptid'),'$') as deptid
    ,get(xmlget(r, 'totalemp'), '$') as totalemp
    --,d.*
from data,
 LATERAL FLATTEN(GET(xmlget(xml,'deptinfo',0), '$')) d
         

这就是您想要进行的转换形式。

可以重组为:

select 
    get(xmlget(xmlget(xmlget(xml,'header',0), 'companyinfo', 0), 'companyid'),'$') as companyid
    ,get(xmlget(xmlget(xmlget(xml,'header',0), 'companyinfo', 0), 'companyname'), '$') as companyname
    ,get(d.value, '@') as depm
    ,get(xmlget(xmlget(d.value, 'response'), 'deptid'),'$') as deptid
    ,get(xmlget(xmlget(d.value, 'response'), 'totalemp'), '$') as totalemp
from data,
 LATERAL FLATTEN(GET(xmlget(xml,'deptinfo',0), '$')) d
 

给出结果:

COMPANYID   COMPANYNAME DEPM            DEPTID  TOTALEMP
100         "ABC Corp"  "electrical"    1       200
100         "ABC Corp"  "mechanical"    2       150
100         "ABC Corp"  "chemical"      3       100

如何在 Talend 中实际执行此操作,我没有答案,但我假设您可以通过查看查询历史记录来了解当前模式如何在 Snowflake 上运行,以解决零件如何从 Talend 逻辑映射到雪花 SQL。