2016-07-24 30 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我以前的一段代碼。 –