如何将Mule Payload拆分为MySQL数据库?

时间:2016-06-20 09:54:23

标签: split mule payload

我希望用Mule流解决一个相对简单的问题。在学习如何操作和转换数据时,我正在研究一些简单的例子。以下代码设置以逗号分隔的字符串示例有效内容(一,二,三,四)。

split表达式为每个部分提供了自己的值,然后'for each'将它放入一个虚拟数据库中。

这样做是将每个有效负载放入数据库的colOne,直到有效负载的每个部分完成(colOne - row1 = 1,row2 = 2,依此类推)。

但是,是否有一种简单的方法可以使拆分有效负载的每个部分进入下一列(即colOne,row1 ='one'... colTwo,row1 ='Two'等等)?

我不能使用'for each'并将有效负载的每个部分设置为一个变量,但是我在一行中得到'one','two','three,'four'但重复超过四行。

谢谢你们, 我的代码已附上......

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:db="http://www.mulesoft.org/schema/mule/db" 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://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <db:mysql-config name="MySQL_Configuration" host="localhost" port="3306" user="Stuart" password="sqlsr3270" database="test" doc:name="MySQL Configuration"/>
    <flow name="sampleFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="Inbound HTTP" allowedMethods="GET"/>
        <set-payload value="five,six,seven,eight" doc:name="Set Sample Payload"/>
        <expression-transformer expression="#[message.payload.split(&quot;,&quot;)]" doc:name="Split String"/>
        <logger message="#['\n\n\nPayload before insert: ' + payload[0] + ', ' + payload[1] + ', ' + payload[2] + ', ' + payload[3]]" level="INFO" doc:name="Logger"/>
        <foreach collection="#[payload]" doc:name="For Each">
            <db:insert config-ref="MySQL_Configuration" doc:name="Database">
                <db:parameterized-query><![CDATA[INSERT INTO testtable (colOne) VALUES(#[payload])]]></db:parameterized-query>
            </db:insert>
        </foreach>
    </flow>
</mule>

0 个答案:

没有答案