DataWeave groupBy 和 maxBy
DataWeave groupBy with maxBy
我有点像 DataWeave 新手。我正在尝试获取用户的最新扣款记录,但也按数组中的类型进行分组。这是我必须处理的数据:
"users": [
{
"employeeId": "123456",
"lastName": "smith",
"firstName": "joe ",
"deductions": [
{
"deductionType": "ABC",
"Amt": 1000,
"StartDate": "2001-01-02T00:00:00"
},
{
"deductionType": "ABC",
"Amt": 1000,
"StartDate": "2019-01-02T00:00:00"
},
{
"deductionType": "ABC",
"Amt": 1000,
"StartDate": "2016-01-02T00:00:00"
},
{
"deductionType": "DCA",
"Amt": 4000,
"StartDate": "2019-11-02T00:00:00",
}
]
}
我尝试使用发布在以下位置的类似解决方案:
但这似乎不起作用,因为我得到的是空负载。
最终结果应该是这样的:
"users": [
{
"employeeId": "123456",
"lastName": "smith",
"firstName": "joe ",
"deductions": [
{
"deductionType": "ABC",
"Amt": 1000,
"StartDate": "2019-01-02T00:00:00"
},
{
"deductionType": "DCA",
"Amt": 4000,
"StartDate": "2019-11-02T00:00:00",
}
]
我目前尝试的解决方案是:
payload.users map {($),
deductions: (($.deductions groupBy $.deductionType) mapObject (value, key) ->
{(key) : (value maxBy $.benefitStartDate)}) pluck (value) -> value
}
但这也不起作用。
试试这个
{
users: payload.users map {
($ - "deductions"),
deductions: (($.deductions groupBy $.deductionType) pluck $) map {
($ maxBy $.StartDate)
}
}
}
这里有一个不同的解决方案,使用性能更好的那个 :)。
算法的注释可以在 DW 表达式中看到。
此外,我在代码中对您的示例数据进行了硬编码,因此您只需复制和粘贴即可确信 DW 表达式有效。
%dw 2.0
output application/dw
var data = "users": [
{
"employeeId": "123456",
"lastName": "smith",
"firstName": "joe ",
"deductions": [
{
"deductionType": "ABC",
"Amt": 1000,
"StartDate": "2001-01-02T00:00:00"
},
{
"deductionType": "ABC",
"Amt": 1000,
"StartDate": "2019-01-02T00:00:00"
},
{
"deductionType": "ABC",
"Amt": 1000,
"StartDate": "2016-01-02T00:00:00"
},
{
"deductionType": "DCA",
"Amt": 4000,
"StartDate": "2019-11-02T00:00:00",
}
]
}
]
---
users: data.users map do {
// Sort by StartDate, I type casted to a `DateTime` instead of comparing strings
// Reverse the sorted list such that the latest dates are at the top of the list
// Finally, get a set out the list where uniqueness is the deductionType
// since distinctBy maintains the first element that matches and removes the rest
// you know have a list of distinct deductionType with the latest date.
var ds = ($.deductions orderBy ($.StartDate as DateTime))[-1 to 0]
distinctBy (d) -> d.deductionType
---
{
($ - "deductions"),
deductions: ds
}
}
我有点像 DataWeave 新手。我正在尝试获取用户的最新扣款记录,但也按数组中的类型进行分组。这是我必须处理的数据:
"users": [
{
"employeeId": "123456",
"lastName": "smith",
"firstName": "joe ",
"deductions": [
{
"deductionType": "ABC",
"Amt": 1000,
"StartDate": "2001-01-02T00:00:00"
},
{
"deductionType": "ABC",
"Amt": 1000,
"StartDate": "2019-01-02T00:00:00"
},
{
"deductionType": "ABC",
"Amt": 1000,
"StartDate": "2016-01-02T00:00:00"
},
{
"deductionType": "DCA",
"Amt": 4000,
"StartDate": "2019-11-02T00:00:00",
}
]
}
我尝试使用发布在以下位置的类似解决方案:
最终结果应该是这样的:
"users": [
{
"employeeId": "123456",
"lastName": "smith",
"firstName": "joe ",
"deductions": [
{
"deductionType": "ABC",
"Amt": 1000,
"StartDate": "2019-01-02T00:00:00"
},
{
"deductionType": "DCA",
"Amt": 4000,
"StartDate": "2019-11-02T00:00:00",
}
]
我目前尝试的解决方案是:
payload.users map {($),
deductions: (($.deductions groupBy $.deductionType) mapObject (value, key) ->
{(key) : (value maxBy $.benefitStartDate)}) pluck (value) -> value
}
但这也不起作用。
试试这个
{
users: payload.users map {
($ - "deductions"),
deductions: (($.deductions groupBy $.deductionType) pluck $) map {
($ maxBy $.StartDate)
}
}
}
这里有一个不同的解决方案,使用性能更好的那个 :)。 算法的注释可以在 DW 表达式中看到。 此外,我在代码中对您的示例数据进行了硬编码,因此您只需复制和粘贴即可确信 DW 表达式有效。
%dw 2.0
output application/dw
var data = "users": [
{
"employeeId": "123456",
"lastName": "smith",
"firstName": "joe ",
"deductions": [
{
"deductionType": "ABC",
"Amt": 1000,
"StartDate": "2001-01-02T00:00:00"
},
{
"deductionType": "ABC",
"Amt": 1000,
"StartDate": "2019-01-02T00:00:00"
},
{
"deductionType": "ABC",
"Amt": 1000,
"StartDate": "2016-01-02T00:00:00"
},
{
"deductionType": "DCA",
"Amt": 4000,
"StartDate": "2019-11-02T00:00:00",
}
]
}
]
---
users: data.users map do {
// Sort by StartDate, I type casted to a `DateTime` instead of comparing strings
// Reverse the sorted list such that the latest dates are at the top of the list
// Finally, get a set out the list where uniqueness is the deductionType
// since distinctBy maintains the first element that matches and removes the rest
// you know have a list of distinct deductionType with the latest date.
var ds = ($.deductions orderBy ($.StartDate as DateTime))[-1 to 0]
distinctBy (d) -> d.deductionType
---
{
($ - "deductions"),
deductions: ds
}
}