Splitting a comma-separated string and saving to d

2019-09-10 04:23发布

问题:

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.

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?

回答1:

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(&quot;,&quot;)]" 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>

LOG OUTPUT

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



回答2:

Best way is
After all trials, i found that this dataweave transofrmation works fne without any data splitting or any ambiguity .

Make sure UTF-8 encoding is mentioned in JSON format. \n

The Above is using Mule 3.8 and anypoint 6.0 versions

%dw 1.0 
%output application/csv   quoteValues=true,separator="|~" ,header=true ,escape="\""

---
payload

This Works fine without any mapping you could directly map just by specifying payload