【问题标题】:Mule 4 : Dataweave 2.0 : is there any way we can have a filter condition in groupBy method that works similar to SQL GroupBY and Having?Mule 4:Dataweave 2.0:有什么方法可以在 groupBy 方法中使用类似于 SQL GroupBY 和 Have 的过滤条件?
【发布时间】:2021-03-19 22:52:33
【问题描述】:

场景:给定如下输入 sampleArray ,我想将所有拥有特定老师的学生分组。

在 DataWeave 中,我们有一个方法 groupBy,它允许我们对指定字符串键的数组进行分组。 但是在这里,由于 item.studentsMarks.subjectTeacher 返回一个数组,我收到了下面指定的错误。

谁能帮忙。提前致谢。

下面附上预期的输出。

示例输入:

var sampleArray = [
    {
        "studentName" : "ABC",  
        "studentsMarks" : [
            {
                "subject" : "maths",
                "marks" : "50",
                "subjectTeacher" : "teacher_1"
            },
            {
                "subject" : "science",
                "marks" : "30",
                "subjectTeacher" : "teacher_3"
            }
        ]
    },
    {
        "studentName" : "XYZ",  
        "studentsMarks" : [
            {
                "subject" : "maths",
                "marks" : "90",
                "subjectTeacher" : "teacher_1"

            },
            {
                "subject" : "arts",
                "marks" : "50", 
                "subjectTeacher" : "teacher_2"
            }
        ]
    }
]


代码尝试:

payload groupBy ((item, index) -> item.studentsMarks.subjectTeacher)

错误:

Cannot coerce Array to String

预期输出:

[
    
    "teacher_1" : [{
            "studentName" : "ABC",  
            "studentsMarks" : [
                {
                    "subject" : "maths",
                    "marks" : "50",
                    "subjectTeacher" : "teacher_1"
                },
                {
                    "subject" : "science",
                    "marks" : "30",
                    "subjectTeacher" : "teacher_3"
                }
            ]
        },    
        {
                "studentName" : "XYZ",  
                "studentsMarks" : [
                    {
                        "subject" : "maths",
                        "marks" : "90",
                        "subjectTeacher" : "teacher_1"
        
                    },
                    {
                        "subject" : "arts",
                        "marks" : "50", 
                        "subjectTeacher" : "teacher_2"
                    }
                ]
           }
    ],
    "teacher_2" : [
        
                {
                "studentName" : "XYZ",  
                "studentsMarks" : [
                    {
                        "subject" : "maths",
                        "marks" : "90",
                        "subjectTeacher" : "teacher_1"
        
                    },
                    {
                        "subject" : "arts",
                        "marks" : "50", 
                        "subjectTeacher" : "teacher_2"
                    }
                ]
           }
    ],
    "teacher_3" : [
        
{
            "studentName" : "ABC",  
            "studentsMarks" : [
                {
                    "subject" : "maths",
                    "marks" : "50",
                    "subjectTeacher" : "teacher_1"
                },
                {
                    "subject" : "science",
                    "marks" : "30",
                    "subjectTeacher" : "teacher_3"
                }
            ]
        }
    ]
        
]

【问题讨论】:

  • 输出是什么样的?顺便说一句,问题出在item.studentsMarks.subjectTeacher 这个表达式上,因为item.studentsMarks 是一个数组——因此下一个.subjectTeacher 是一个与.*subjectTeacher 非常相似的选择器,它返回一个数组,因此你会得到你得到的错误。
  • @George 嗨,我附上了预期的输出。我理解了这个问题,但正在寻找一种使用 groupBy 实现解决方案的方法。如果您能找到合适的方法,请告诉我。非常感谢

标签: dataweave mule4


【解决方案1】:

这里有一个非常快速的方法来完成它。不过,它不仅仅需要groupBy

%dw 2.0
output application/json

var sampleArray = [
    {
        "studentName" : "ABC",  
        "studentsMarks" : [
            {
                "subject" : "maths",
                "marks" : "50",
                "subjectTeacher" : "teacher_1"
            },
            {
                "subject" : "science",
                "marks" : "30",
                "subjectTeacher" : "teacher_3"
            }
        ]
    },
    {
        "studentName" : "XYZ",  
        "studentsMarks" : [
            {
                "subject" : "maths",
                "marks" : "90",
                "subjectTeacher" : "teacher_1"

            },
            {
                "subject" : "arts",
                "marks" : "50", 
                "subjectTeacher" : "teacher_2"
            }
        ]
    }
]
---
// Get list of all teachers
sampleArray..*subjectTeacher
// Change the collection to a set 
distinctBy $ 
// Iterate over every single teacher and create an object where
// the field is the teacher while the value is a collection
// containing all students where they attend a class for the teacher.
map {
    ($): (sampleArray dw::core::Arrays::partition (e) -> e..*subjectTeacher contains $).success
}

【讨论】:

  • 你能解释一下你的方法吗?
  • 用 cmets 注释代码。如果算法是问题,那么 cmets 会有所帮助。如果问题是对我使用的任何功能缺乏了解,请告诉我,我会添加文档链接。
  • cmets 将帮助任何访问者快速了解解决方案并将其用于他们的用例。我经历了这些方法并理解了它。感谢您的努力。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 2023-03-16
  • 1970-01-01
  • 2018-07-22
  • 1970-01-01
相关资源
最近更新 更多