Dataweave代码无法在Mule3中工作的Mule 4中工作

时间:2019-02-11 16:23:55

标签: mule anypoint-studio payload dataweave

以下代码段在3.8中可用,但在4.1中不可用:

output application/java
var csv = payload
---
(csv map $ reduce ((val,acc) -> ((acc) ++ ((val)) ))) map ($ replace ',' with '\t')

input payload: 

{"D01":{"AK":"D,01,AK,0,0,0,0,0,-2.89,0.00,0,0,0,0,0",
        "AL":"D,01,AL,829.23,18506.35,0,0.00,0,-6610.91,0.00,0,0,0,159.66,-1.94"},
"D02.1":{"AK":"D,02.1,AK,0,0,0,0,0,-6.76,0.00,0,0,0,0,0",
        "AL":"D,02.1,AL,7733.77,304148.90,0,0.00,0,-42791.15,0.00,0,0,0,1347.09,-8.88"}
} 

在此处输入代码

预期输出:     [     “ D \ t01 \ tAK \ t0 \ t0 \ t0 \ t0 \ t0 \ t-2.89 \ t0.00 \ t0 \ t0 \ t0 \ t0 \ t0 \ t0”,     “ D \ t01 \ tAL \ t829.23 \ t18506.35 \ t0 \ t0.00 \ t0 \ t-6610.91 \ t0.00 \ t0 \ t0 \ t0 \ t159.66 \ t-1.94”,     “ D \ t02.1 \ tAK \ t0 \ t0 \ t0 \ t0 \ t0 \ t-6.76 \ t0.00 \ t0 \ t0 \ t0 \ t0 \ t0 \ t0”,     “ D \ t02.1 \ tAL \ t7733.77 \ t304148.90 \ t0 \ t0.00 \ t0 \ t-42791.15 \ t0.00 \ t0 \ t0 \ t0 \ t1347.09 \ t-8.88”     ]

Am低于错误:

org.mule.runtime.core.internal.exception.OnErrorPropagateHandler:


消息:“您使用以下参数调用了函数'map':
1:字符串(“ {\” D01 \“:{\” AK \“:\” D,01,AK,0,0,0,0,0,-2.89,0.00,0,0,0,0, 0 \“,\” AL \“:\” D,01,A ...)
2:函数(($:Any,$$:Any)-> ???)

但是它期望使用以下类型的参数:
1:数组
2:功能

13 | (csv ma​​p $ reduce((val,acc)->((acc)++((val))))))map($用'\ t'替换',')      ^^^^^^^^^^

1 个答案:

答案 0 :(得分:1)

我认为存在问题,DataWeave 2中的mapObject上不起作用(请参阅changes)。 DataWeave 1允许这样做,因此您的代码对DataWeave 1有效。

基于DataWeave 1的输出,我认为您可以对DataWeave 2使用以下代码-

%dw 2.0
output application/java
var csv = payload
---
flatten ((csv pluck $) map ($ pluck $)) map ($ replace ',' with '\t')

pluck将对象分成两个数组-值($)和键($$)。