【问题标题】:MULE 4 : REDUCE method : Is there a limit to elements size in Reduce method if the accumulator is an Object?MULE 4:REDUCE 方法:如果累加器是对象,Reduce 方法中的元素大小是否有限制?
【发布时间】:2020-10-17 19:10:40
【问题描述】:

场景:使用 Mule 4 中的 Reduce 方法将 LIST 缩减为三个参数:

  1. 学生名单
  2. 教师名单
  3. 学生人数

在转换消息中使用以下 Dataweave 代码:

%dw 2.0
output application/java
---
payload reduce((value, acc = { 'totalStudents': 0 as Number,'studentList' : [], 'teachersList' : []}) -> 
    if(
        value.age > 18 and value.age < 25
    ){
        totalStudents : (acc.totalStudents default 0 as Number) + 1,
        studentList : (acc.studentList default [] ) << {
            'studentName' : value.Name ++ " is a Student"
        }
    }else{
        teachersList : acc.teachersList default [] << value.Name ++ " is a Teacher"
    }
)

问题陈述:转换消息已成功处理,但在有效负载中我只得到两个值:

  1. payload.totalStudents 和
  2. payload.studentList

谁能帮我理解为什么我的结果中没有得到 payload.teachersList

【问题讨论】:

  • 年龄大概在18到25之间
  • @SalimKhan 即使输入列表中所有人的年龄在 18 到 25 岁之间,因为我已经在 acc = { 'totalStudents': 0 as Number,'studentList' : [], 'teachersList' : []} 中定义了教师列表,我希望看到 payload.teachersList = [] 但我是在输出中根本没有得到这个 payload.teachersList
  • 那个是acc中初始化的变量。输出就是您在脚本中构建的内容。

标签: dataweave mule4


【解决方案1】:

最后这应该得到你正在寻找的东西:

%dw 2.0
output application/java
---
payload reduce((value, acc = { 'totalStudents': 0 as Number,'studentList' : [], 'teachersList' : []}) -> 
    if(
        value.age > 18 and value.age < 25
    ){
        totalStudents : (acc.totalStudents default 0 as Number) + 1,
        studentList : (acc.studentList default [] ) << {
            'studentName' : value.Name ++ " is a Student"
        },
        teachersList: acc.teachersList
    }else{
         totalStudents : acc.totalStudents,
         studentList : acc.studentList,
        teachersList : (acc.teachersList default [] ) <<  {'teacherName': value.Name ++ " is a Teacher"}
    }
)```

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/fcyNf.png

【讨论】:

  • 我试过了但是当我在 Studio 7 中本地测试应用程序时,我遇到了 MULE FATAL JVM 错误。输入列表大小为 40k 条记录。我不确定是什么导致了这个``` { description=java.lang.StackOverflowError detailDescription=java.lang.StackOverflowError errorType=MULE:FATAL_JVM_ERROR cause=org.mule.runtime.api.exception.MuleFatalException errorMessage=- childErrors=[ ] } ```
  • 这完全是一个不同的问题。我认为问题在于使用 reduce 以及为什么在 acc 中声明的某些变量没有显示在输出中。
  • Dataweave 代码在我的本地有一个错误,并且引发了 MULE_JVM 错误。似乎 MULE 中的错误未正确映射。另外,你的逻辑是正确的。 reduce 方法中“acc”中定义的值仅初始化它们。如果我们需要有效载荷中的“acc”变量,我们需要在 if 块和 else 块中为每个变量分配一些值。
  • 因此,受您对我有用的代码启发的实际工作代码附在下面的答案中。请更新您的答案,以便我接受它
【解决方案2】:
%dw 2.0
output application/java
---
payload reduce((value, acc = { 'totalStudents': 0 as Number,'studentList' : [], 'teachersList' : []}) -> 
    if(
        value.age > 18 and value.age < 25
    ){
        totalStudents : (acc.totalStudents default 0 as Number) + 1,
        studentList : (acc.studentList default [] ) << {
            'studentName' : value.Name ++ " is a Student"
        },
        teachersList: acc.teachersList
    }else{
        totalStudents : acc.totalStudents,
        studentList : acc.studentList,
        teachersList : (acc.teachersList default [] ) <<  {'teacherName': value.Name ++ " is a Teacher"}
    }
)

【讨论】:

    【解决方案3】:

    希望这会有所帮助。由于存在其他条件,因此它构建了教师列表并忽略了学生列表

    【讨论】:

      【解决方案4】:

      这也是……在下面的输入中,第二个元素满足 else 条件,因此最终输出仅包含输入中最后 4 个条目的学生。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-02
        • 2020-08-15
        • 2019-10-16
        • 2021-12-12
        • 2020-05-10
        • 2015-05-08
        • 2019-12-13
        • 1970-01-01
        相关资源
        最近更新 更多