我在Mule 4上(dataweave 2.0),我的代码如下。
%dw 2.0
import * from dw::core::Arrays
output application/json
var var1 = [
{"ID": 1000, "ID_2": "ABC-1000"},
{"ID": 2000, "ID_2": "DEF-2000"},
{"ID": 3000, "ID_2": "GHI-3000"}
]
var var2 = [
{"EID": "ABC-1000", "GUID": "A0F4960AE5E54459A1B1FB40297EF893"},
{"EID": "DEF-2000", "GUID": "63F6A192988A4C0696B117E9AC080611"},
{"EID": "GHI-3000", "GUID": "A0FB600FA73E4E3BBF1CB59AA6E92AEF"}
]
---
outerJoin(var1, var2, (obj) -> (obj.ID_2), (obj) -> (obj.EID))
我得到的输出是:
[
{
"l": {"ID": 1000, "ID_2": "ABC-1000"},
"r": {"EID": "ABC-1000", "GUID": "A0F4960AE5E54459A1B1FB40297EF893"}
},
{
"l": {"ID": 2000, "ID_2": "DEF-2000"},
"r": {"EID": "DEF-2000", "GUID": "63F6A192988A4C0696B117E9AC080611"}
},
{
"l": {"ID": 3000, "ID_2": "GHI-3000"},
"r": {"EID": "GHI-3000", "GUID": "A0FB600FA73E4E3BBF1CB59AA6E92AEF"}
}
]
预期的输出如下图所示。我需要将左右两边的输出都扁平化,同时引入一个静态属性。
[
{"Key1": "1000", "Key2": "ABC-1000", "Name": "Test", "GUID": "A0F4960AE5E54459A1B1FB40297EF893"},
{"Key2": "2000", "Key2": "DEF-2000", "Name": "Test", "GUID": "63F6A192988A4C0696B117E9AC080611"},
{"Key3": "3000", "Key2": "GHI-3000", "Name": "Test", "GUID": "A0FB600FA73E4E3BBF1CB59AA6E92AEF"}
]
请赐教。
试试这个,但注意我把你的 outerJoin
到 join
:
%dw 2.0
import * from dw::core::Arrays
output application/json
var var1 = [
{"ID": 1000, "ID_2": "ABC-1000"},
{"ID": 2000, "ID_2": "DEF-2000"},
{"ID": 3000, "ID_2": "GHI-3000"}
]
var var2 = [
{"EID": "ABC-1000", "GUID": "A0F4960AE5E54459A1B1FB40297EF893"},
{"EID": "DEF-2000", "GUID": "63F6A192988A4C0696B117E9AC080611"},
{"EID": "GHI-3000", "GUID": "A0FB600FA73E4E3BBF1CB59AA6E92AEF"}
]
---
join(var1, var2, (obj) -> (obj.ID_2), (obj) -> (obj.EID))
reduce (e, acc=[]) -> (
acc + {
Key1: e.l.ID,
Key2: e.l.ID_2,
Name: "Test",
GUID: e.r.GUID
}
)
说明在左边或右边没有匹配记录的情况下,输出数据是什么样子的,然后我就可以切换回。outerJoin