2016-07-24 35 views
0

我试图提取类别-ID时初级=真通过产品ID的分组骡Dataweave对象到阵列误差

%input payload application/xml 
%output application/java 
--- 
using (c=payload.catalog.*category-assignment default []) 

((c default []) filter ([email protected] != "delete") groupBy [email protected] map ({ 

    (($ default []) filter ($.primary == 'true') map ({ 
      field3:[email protected] 
     }) when $.primary != null otherwise field3:""), 

     cat-id: [email protected] joinBy "||" , 
     primary-flag:$.primary 

// (($ default []) filter ($.primary matches /true/) map ({ 
//   //ur code here when equals to shipping charges 
//   field3:[email protected] 
//  }) when $.primary != null otherwise []) , 

})) 

我与多个组合和过滤器尝试。过滤器通常使用XML属性的作品,但这里给人异常

cast to com.mulesoft.weave.model.structure.ObjectSeq (java.lang.ClassCastException). Message payload is of type: ReceiverFileInputStream 

样品输入 -

<?xml version="1.0" encoding="UTF-8"?> 
<catalog xmlns="http://www.example.com/xml/impex/catalog/2006-10-31"> 
<category-assignment product-id="D711069" category-id="4160"> 
    <primary>true</primary> 
    </category-assignment> 
<category-assignment product-id="D711069" category-id="DANIEL_4160"/> 
</catalog> 

任何这里建议?

我尝试另一套改造只是为了演示,不工作 -

%input payload application/xml 
%output application/xml 
--- 

Test:{((payload.catalog.*category-assignment default []) groupBy [email protected] pluck { 

     product-id:$$, 
     cat-id: [email protected] joinBy "||" , 
     primary-flag:$[0].primary, 

     field3:[email protected][?($.primary == "true")] 

    })} 

回答

0

尝试这个表情。你能详细说明你的预期产出吗?

using (c=payload.catalog.*category-assignment default []) 
(c[?(($.primary == "true") and ([email protected] != "delete"))] groupBy [email protected] map { 
    field3:[email protected], 
    primary-flag:$.primary 
}) 

更新dataweave按您的意见:

%dw 1.0 
%output application/csv 
--- 

payload.catalog.*category-assignment[?(($.primary == "true") and ([email protected] != "delete"))] groupBy [email protected] map { 
productid:[email protected][0], 
categoryid: arrayToString([email protected], sizeOf [email protected]) 

添加全局配置元素,如下图所示:

<configuration doc:name="Configuration"> 
    <expression-language> 
     <global-functions> 
def arrayToString(arrayObj,length){ 
String r=null; 
    if(arrayObj==null){ 
     return "null"; 
    }else if(arrayObj==""){ 
     return "Empty"; 
    }else{ 
     for (String s:arrayObj) { 
      if(r != null) 
       r = r + '||' + s; 
      else { 
       r = s; 
      } 
     } 
     return r; 
    } 
} 
     </global-functions> 
    </expression-language> 
</configuration> 
+0

我想创建CSV,FIELD1 =类别-ID,字段2 =产品-id(产品分组),field3 =当primary = true时的category-id –

+0

按照上面的xml,csv应该看起来像D711069,4160 || DANIEL_4160,4160我们需要使用||加入类别 –

+0

上面的代码适用于单个节点 true ,但每当我传递多个节点时,其给出的错误 –

0

试试这个表达,它会产生CSV按您的规定要求(S)。

%dw 1.0 
%output application/csv header=true 
--- 
using (ca = payload.catalog.*category-assignment) 
(
payload.catalog map (catVal, idx) -> ({ 
    ( 
     ca groupBy [email protected] pluck { 
      product-id:$$, 
      cat-id: [email protected] joinBy "||" 
      ,primary-flag:$.primary[0] default "false" 
      ,(field3:[email protected][0]) when ($.primary[0] contains "true") 
     } 
    ) 
}) distinctBy $ 
) 

它产生的输出为:

product-id,cat-id,primary,field3 
D711069,DANIEL_4160||4160||DANIEL_4160,true,DANIEL_4160 

输入用于:

<?xml version="1.0" encoding="UTF-8"?> 
<catalog> 
<category-assignment product-id="D711069" category-id="DANIEL_4160"/> 
<category-assignment product-id="D711069" category-id="4160"> 
    <primary>true</primary> 
</category-assignment> 
<category-assignment product-id="D711069" category-id="DANIEL_4160"/> 
</catalog> 

HTH

+0

早些时候我实现了同样的功能,但是这个代码在输入时失败了,比如'<?xml version =“1.0”encoding =“UTF-8”?> true' –

+0

@RohanShinde,用正确的代码更新,意外地粘贴了sa我以前的一段代码。 –