【问题标题】:Combining multiple tXMLMap output values to a single snowflake table将多个 tXMLMap 输出值组合到单个雪花表
【发布时间】:2021-04-03 19:02:52
【问题描述】:

我有以下 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>

我期待的输出是

Company ID Company name Dept Dept ID Total emp
100 ABC Corp Electrical 1 200
100 ABC Corp Mechanical 2 150
100 ABC Corp Chemical 3 100

我采用的设计方法如下 -

tXMLMap 组件已配置如下 -

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

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

【问题讨论】:

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


    【解决方案1】:

    所以在雪花中使用 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。

    【讨论】:

    • 感谢您的回复,西蒙!我实际上希望使用 Talend 组件而不是横向扁平化功能来实现。主要原因是横向展平功能的文档不清楚或难以理解:(希望您能帮助理解如何更好地使用该功能:)
    • @vvazza 我对 Talend 的经验为零,因此无法提供帮助。鉴于您编写问题的方式,很明显您希望在工具中找到解决方案。我向您展示了我将如何在 SQL 中执行此操作,因此也许它可以帮助您探索 Talend 中的选项。这就是为什么我在最后一段中这么说的原因。 FLATTEN 还没有准备好那么难的命令,给你一个例子。我觉得 XMLGET 是更粗暴的命令。
    • Simeon,如果电子标签中还有一组数据,我将如何设置循环?如果我必须单独循环 ,我应该 LATERAL FLATTEN 这样的东西 - LATERAL FLATTEN(GET(xmlget(xmlget(xml,'deptinfo',0), 'electrical', '$')) d
    • Flatten 有一个参数,如果输入为 null,则返回一行,如果 mechchem 没有子循环,这很有用。 OUTER=&gt;true 是您想要的,然后您可以使用 from data, LATERAL FLATTEN(GET(xmlget(xml,'deptinfo',0), '$')) d, LATERAL FLATTEN(input=&gt; d.&lt;the_thing_it_has&gt; outer=&gt;true)) e 并且对于 elec 行,e.value 不会为空,因此您可以选择这些子对象。 @vvazza
    猜你喜欢
    • 2021-09-07
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 2022-08-09
    • 2019-11-02
    • 2015-01-05
    • 2022-11-22
    • 1970-01-01
    相关资源
    最近更新 更多