Mulesoft:通过使用dataweave检查dateField来删除重复记录

时间:2017-05-29 20:47:11

标签: duplicates mule esb dataweave

我有一个包含以下数据的csv文件:

Id,Name,Type,date
1,name1,employee,25/04/2017
2,name2,contrator,26/04/2017
3,name3,employee,25/04/2017
4,name4,contrator,26/04/2017
5,name5,employee,24/04/2017
6,name6,employee,27/04/2017
7,name7,employee,25/04/2017
8,name8,contrator,24/04/2017
9,name9,employee,24/04/2017
10,name10,contrator,26/04/2017
11,name11,employee,27/04/2017
12,name12,contrator,27/04/2017

如果它有两行具有相同的Id号。应通过检查最新日期删除其中一行。应删除具有较早日期的行。 例如,上面的输入有两行ID为6的数据。应该删除日期为24/04/2017的行。输出应该是这样的

@RequestMapping(value = "/registration", method = RequestMethod.POST)
public ModelAndView createNewUser(@Valid User user, BindingResult bindingResult) {
    ModelAndView modelAndView = new ModelAndView();
    try{
        userService.saveUser(user);
        modelAndView.addObject("successMessage", "User has been registered successfully");
        modelAndView.addObject("user", user);
        modelAndView.setViewName("registration");
    }catch(DataIntegrityViolationException e){
        bindingResult
        .rejectValue("email", "error.user",
                "There is already a user registered with the email provided");
    }
    return modelAndView;
}

我需要使用Dataweave实现这一目标。请给我一个解决方案或建议

1 个答案:

答案 0 :(得分:4)

这是您要查找的数据编辑:

%dw 1.0
%output application/csv
%var toDate = (str) -> str as :date { format: "dd/MM/yyyy" }
%var maxDate = (a, b) -> a when toDate(a.date) > toDate(b.date) otherwise b
---
payload groupBy $.Id 
    pluck $ map ($ reduce ((val, acc) -> maxDate(val, acc)))

enter image description here