Mule 根据类别压缩数据
下面的例子。我有一组帐号和帐户属性。对于每个 account_number 都有三个类别,我希望根据 DataWeave 中的每个余额计算每个帐号的总和。
数据输入
[
{
Account_Number: 1,
Account: 5,
Category: "A",
Balance: 500
},
{
Account_Number: 1,
Account: 5,
Category: "A",
Balance: 700
},
{
Account_Number: 1,
Account: 5,
Category: "B",
Balance: 300
},
{
Account_Number: 1,
Account: 5,
Category: "C",
Balance: 100
},
{
Account_Number: 2,
Account: 10,
Category: "B",
Balance: 300
},
{
Account_Number: 2,
Account: 10,
Category: "B",
Balance: 800
}
]
数据输出
[
{
Account_Number: 1,
Account: 5,
CategoryA_Balance: 1200,
CategoryB_Balance: 300,
CategoryC_Balance: 100
}
{
Account_Number: 2,
Account: 10,
CategoryA_Balance: 0,
CategoryB_Balance: 1100,
CategoryC_Balance: 0
}
]```
Example below. I've got a set of account numbers, with an account attribute. For each account_number there are three categories, and I would like the sum for each account number based on each balance in DataWeave.
Data input
[
{
Account_Number: 1,
Account: 5,
Category: "A",
Balance: 500
},
{
Account_Number: 1,
Account: 5,
Category: "A",
Balance: 700
},
{
Account_Number: 1,
Account: 5,
Category: "B",
Balance: 300
},
{
Account_Number: 1,
Account: 5,
Category: "C",
Balance: 100
},
{
Account_Number: 2,
Account: 10,
Category: "B",
Balance: 300
},
{
Account_Number: 2,
Account: 10,
Category: "B",
Balance: 800
}
]
Data Output
[
{
Account_Number: 1,
Account: 5,
CategoryA_Balance: 1200,
CategoryB_Balance: 300,
CategoryC_Balance: 100
}
{
Account_Number: 2,
Account: 10,
CategoryA_Balance: 0,
CategoryB_Balance: 1100,
CategoryC_Balance: 0
}
]```
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为类别是动态的。如果没有,您可以用静态数组替换类别变量。
I assume Categories are dynamic. If not, you can replace the Categories variable with a static array.
与 sudhish 类似的解决方案。分解解决方案以更好地理解
distinctBy
因为..
将为您提供输入中存在的所有类别。 DistinctBy 将删除重复项,您将有 [A,B,C]groupBy
根据每个帐号的详细信息进行分组(item[0] - "Balance" - "Category")< /code> 由于我们只需要 AccountNumber 和 Account 一次,因此使用 item[0] 和“-”来消除 Balance 和 Category,因为我们需要进一步执行一些基于条件的逻辑
pluck
来转换带有 account 的对象数字作为数组的键map
迭代每个帐号的详细信息map
遍历类别将产生两个帐号的 [A,B,C]filter
检查顶级映射中存在的类别是否与变量中存在的类别匹配。 if (true) then sum(Balance) else 0sum
根据使用过滤器匹配的类别进行添加A Similar solution to sudhish. Breaking down the solution for better understanding
distinctBy
Since..
will give you all the categories present in the input. DistinctBy will remove duplicates and you will have [A,B,C]groupBy
to group based over details of each account number(item[0] - "Balance" - "Category")
Since we require AccountNumber and Account only once so used item[0] and "-" to eliminate Balance and Category since we need to perform some conditional based logic furtherpluck
to convert the object with account number as key to arraymap
iterates over the details of each account numbermap
over the categories will yield you [A,B,C] for both the account numbersfilter
to check if the Category present in the top level map matches the categories present in the variable. if (true) then sum(Balance) else 0sum
to add based on the categories matched using filter