拆分逗号分隔的字符串并保存到数据库中,而无需在Mule中使用DataWeave [英] Splitting a comma-separated string and saving to database without DataWeave in Mule
问题描述
我对许多工具的关注是,困难的事情变得容易,但是容易的事情变得困难.我目前遇到这样的问题.
My one concern with many tools is that difficult things become easy, but easy things become difficult. I'm currently stuck with such a problem.
我正在使用Mule的社区版.此版本不包括DataWeave(以前是DataMapper)功能.
I'm using the community edition of Mule. This edition does not include the DataWeave (used to be DataMapper) function.
是否有一种简单的方法来编写将逗号分隔的字符串拆分为值并将其保存到数据库表中的流?
Is there a simple way to write a flow that splits a comma-separated string into values and save them to a table in a database?
推荐答案
尝试下面的流配置,基本上,您使用MEL和分割字符串,分割有效负载后将成为一个集合,然后仅使用集合分割器或在此示例中foreach,然后就放数据库出站连接器并构造您的插入sql语句,因为您没有可以使用数据感知的数据编织或数据映射器.
Try the flow config bellow, basically you use MEL and split string, after splitting the payload will be a collection, then just use collection splitter or in this example a foreach, then just put your database outbound connector and construct your insert sql statement since you don't have data weave or data mapper where you can utilize data sense.
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="sampleFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/inbound" doc:name="Inbound HTTP"/>
<set-payload value="one,two,three,four" doc:name="Set Sample Payload"/>
<expression-transformer expression="#[message.payload.split(",")]" doc:name="Split String"/>
<foreach collection="#[payload]" doc:name="For Each">
<logger message="INSERT INTO table(field_a) VALUES(#[payload]);" level="INFO" doc:name="SQL INSERT"/>
<logger message="INSERT TO DB" level="INFO" doc:name="YOUR DATABASE CONNECTOR"/>
</foreach>
</flow>
</mule>
日志输出
org.mule.api.processor.LoggerMessageProcessor:插入到 table(field_a)VALUES(一个); org.mule.api.processor.LoggerMessageProcessor:插入数据库 org.mule.api.processor.LoggerMessageProcessor:插入到 table(field_a)VALUES(两个); org.mule.api.processor.LoggerMessageProcessor:插入数据库 org.mule.api.processor.LoggerMessageProcessor:插入到 表(field_a)VALUES(三); org.mule.api.processor.LoggerMessageProcessor:插入数据库 org.mule.api.processor.LoggerMessageProcessor:插入到 table(field_a)VALUES(四个); org.mule.api.processor.LoggerMessageProcessor:插入数据库
org.mule.api.processor.LoggerMessageProcessor: INSERT INTO table(field_a) VALUES(one); org.mule.api.processor.LoggerMessageProcessor: INSERT TO DB org.mule.api.processor.LoggerMessageProcessor: INSERT INTO table(field_a) VALUES(two); org.mule.api.processor.LoggerMessageProcessor: INSERT TO DB org.mule.api.processor.LoggerMessageProcessor: INSERT INTO table(field_a) VALUES(three); org.mule.api.processor.LoggerMessageProcessor: INSERT TO DB org.mule.api.processor.LoggerMessageProcessor: INSERT INTO table(field_a) VALUES(four); org.mule.api.processor.LoggerMessageProcessor: INSERT TO DB
这篇关于拆分逗号分隔的字符串并保存到数据库中,而无需在Mule中使用DataWeave的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!