我有mule flow,它从数据库表中选择几条记录,如下所示。
StudentID Subject Mark
1 Maths 98
2 Literature 62
1 Science 56
1 Anatomy 63
3 Zoology 38
2 Algebra 63
这里我需要根据studentID对记录进行分组,并需要发送以进行进一步处理。因此我在数据库节点之后立即放置了拆分器组件。但在MEL表达式中,我们如何对记录进行分组?
还有其他最好的方法来做这个ESB骡子吗?
更新 - 我需要根据StudentID(分组依据)拆分消息。我找到了groovy can do grouping。但是我们是否可以使用groovy分割消息。
答案 0 :(得分:0)
我会创建一个Java对象并实现可调用。使用此方法可更改有效负载。然后我将使用第二个数据库组件并在第二个查询中使用有效负载对象(MEL)。
此对象将数据从数据库组件输出流转换为混合对象,该对象用于显示具有嵌入式子数据阵列的JSON。
查看可调用方法的回报,看看你如何能够"转换"你自己的数据。
这是mule配置中用于实例化所需Java组件的片段。
<spring:beans>
<spring:bean id="ordersTransform" name="OrdersTransformSingleton"
class="org.dlw.transport.OrdersTransformSingleton" scope="singleton">
</spring:bean>
<spring:bean id="jdbcDataSource" class="org.enhydra.jdbc.standard.StandardDataSource" destroy-method="shutdown">
<spring:property name="driverName" value="com.mysql.jdbc.Driver"/>
<spring:property name="url" value="${database.url}"/>
</spring:bean>
</spring:beans>
该对象在此流程中使用...
<flow name="get:/orders:api-config">
<set-payload value="[
 {
 "orderId": 1233,
 "placementDate": "2016-06-02",
 "customerName": "Sally Hansen",
 "orderItems":[
 {
 "orderItemId": 1323,
 "orderId": 438577,
 "itemId": 23058,
 "itemName": "Salt",
 "itemCount": 3,
 "qtyItemCost": "$2.76"
 },
 {
 "orderItemId": 1323,
 "orderId": 438577,
 "itemId": 23058,
 "itemName": "Pepper",
 "itemCount": 3,
 "qtyItemCost": "$8.79"
 }
 ] 
 },
 {
 "orderId": 1233,
 "placementDate": "2016-06-02",
 "customerName": "Billy Wilson",
 "orderItems":[
 {
 "orderItemId": 1323,
 "orderId": 438577,
 "itemId": 23058,
 "itemName": "Wheat Flour",
 "itemCount": 3,
 "qtyItemCost": "$10.12"
 },
 {
 "orderItemId": 1323,
 "orderId": 438577,
 "itemId": 23058,
 "itemName": "Tomato Paste",
 "itemCount": 3,
 "qtyItemCost": "$9.21"
 }
 ] 
 }
]" doc:name="Set Payload"/>
<db:select config-ref="MySQL_Configuration" doc:name="Database">
<db:parameterized-query><![CDATA[SELECT a.orderId, a.customerName, a.placementDate, b.orderItemId, b.itemId, c.itemName, b.itemCount, c.itemCost FROM modusbox.orders a, modusbox.orderitems b, modusbox.items c WHERE a.orderId = b.orderId AND b.itemId = c.itemId]]></db:parameterized-query>
</db:select>
<component doc:name="Java">
<spring-object bean="OrdersTransformSingleton" />
</component>
<json:object-to-json-transformer doc:name="Object to JSON"/>
<logger level="INFO" doc:name="Logger"/>
</flow>
答案 1 :(得分:0)
更好地放置dataweave组件并使用'groupBy'逻辑。
https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-reference-documentation#group-by
答案 2 :(得分:0)
DataWeave是groupBy的正确选项。如果没有DataWeave作为选择(使用Community Edition),则可以通过脚本引擎实现快速获胜。
列表应为groupBy属性mail
[
{
"mail": "smith@example.com",
"name": "lastname",
"value": "Smith"
},
{
"mail": "smith@example.com",
"name": "firstname",
"value": "John"
},
{
"mail": "doe@example.com",
"name": "lastname",
"value": "Doe"
},
{
"mail": "doe@example.com",
"name": "firstname",
"value": "Lisa"
}
]
M子脚本组件
<scripting:component>
<scripting:script engine="groovy">
<![CDATA[flowVars['recipients'].groupBy{it.mail}]]>
</scripting:script>
</scripting:component>
groupBy mail
{
"smith@example.com": [
{
"mail": "smith@example.com",
"name": "lastname",
"value": "Smith"
},
{
"mail": "smith@example.com",
"name": "firstname",
"value": "John"
}
],
"doe@example.com": [
{
"mail": "doe@example.com",
"name": "lastname",
"value": "Doe"
},
{
"mail": "doe@example.com",
"name": "firstname",
"value": "Lisa"
}
]
}
使用Mule 3.8.1 CE正常工作。