【问题标题】:DataWeave groupBy with maxByDataWeave groupBy 和 maxBy
【发布时间】:2019-12-06 08:04:28
【问题描述】:

我有点像 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",
            }
        ]
    }

我尝试使用发布在以下位置的类似解决方案:Dataweave 2.0 maxBy and filter 但这似乎不起作用,因为我得到了一个空有效负载。

最终结果应该是这样的:

    "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

}

但这也不起作用。

【问题讨论】:

  • 我试图了解取消扣除的标准是什么——看来你只是保留最新的(按日期计算);我说的对吗?
  • 我正在尝试获取该用户的用户和扣除记录,该用户按 deductionType 分组,开始日期最晚

标签: dataweave mulesoft mule4


【解决方案1】:

试试这个

{ 
    users: payload.users map {
        ($ - "deductions"),
        deductions: (($.deductions groupBy $.deductionType) pluck $) map {
            ($ maxBy $.StartDate)
        }
    }
}

【讨论】:

  • 它让我更接近一点。现在我只是得到一个空用户而不是一个完全空的负载。
  • 使用您的示例输入,我能够通过上述转换获得预期的最终结果。你能发布你现在得到的结果吗?
  • 我只得到:输出应用程序/java --- { users: null } 当我在调试中单步执行时。
  • 我也尝试了@shortstackstevens 解决方案,它有效。
【解决方案2】:

这是一种不同的解决方案,请使用性能更好的解决方案:)。 算法的注释可以在 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 ---的主体内声明var,并再次使用---多次。我可以查看与此示例相关的 mule 文档吗?我很喜欢它。谢谢
  • 在 DW 2.0 @star 中,有一种新方法可以创建具有有限范围的本地化声明(也称为闭包)。它的do {<declarations> --- <expression> }。这个功能应该解释的页面在这个页面:docs.mulesoft.com/mule-runtime/4.2/…。不幸的是,它没有解释,而是我们只有using () () 方式,其中 (1) 是遗留的,(2) 不应该使用,并且 (3) 是错误的,因为它不限制声明的范围。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-16
  • 1970-01-01
  • 2021-03-19
  • 2020-10-12
相关资源
最近更新 更多