使用Mulesoft将CSV文件从FTP插入MySQL

时间:2014-07-02 09:13:44

标签: mysql ftp mule mule-studio

我还是Mulesoft EE的新人。我需要从FTP服务器(Filezilla)获取CSV文件并将其插入MySQL数据库(Wamp)。

我想运行这个测试代码(我用HTTP替换了FTP以便测试)但是postman插件显示没有结果!

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

<mule xmlns:...>
    <db:mysql-config name="MySQL_Configuration" host="localhost"
        port="3306"   database="information"
        doc:name="MySQL Configuration" />
    <data-mapper:config name="CSV_To_XML" transformationGraphPath="csv_to_xml.grf" doc:name="CSV_To_XML"/>
    <jdbc-ee:mysql-data-source name="MySQL_Data_Source" user="User" password="Pass" url="jdbc:mysql://localhost:3306/information" transactionIsolation="UNSPECIFIED" doc:name="MySQL Data Source"/>
    <jdbc-ee:connector name="Database" dataSource-ref="MySQL_Data_Source" validateConnections="true" queryTimeout="-1" pollingFrequency="0" doc:name="Database"/>
    <flow name="dbFlow1" doc:name="dbFlow1">
 <http:inbound-endpoint exchange-pattern="one-way" host="localhost" port="8084" path="csv" doc:name="HTTP"/>
        <data-mapper:transform config-ref="CSV_To_XML" doc:name="CSV To XML"/>
        <byte-array-to-string-transformer doc:name="Byte Array to String"/>
        <foreach collection="#[xpath('//info')]" doc:name="For Each">
            <mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
            <db:insert config-ref="MySQL_Configuration" doc:name="Database">
                <db:parameterized-query><![CDATA[INSERT INTO `information`.`user_table`
(`current_date`,
`serialnumber`,
`gender`,
`fullname`,
`birthdate`,
`email`,
`mobilnumber`,
`address`)
VALUES
(#[xpath://date],
#[xpath://serialnumber],
#[xpath://gender],
#[xpath://fullname],
#[xpath://birthdate],
#[xpath://email],
#[xpath://mobilenumber],
#[xpath://address]
);]]></db:parameterized-query>
            </db:insert>
            <message-properties-transformer scope="invocation" doc:name="Message Properties">
                <add-message-property key="count" value="#[flowVars.counter]"/>
            </message-properties-transformer>
        </foreach>
        <logger message="#[payload] Number of records #[flowVars.count]" level="INFO" doc:name="Logger"/>
        <smtp:outbound-endpoint host="localhost" responseTimeout="10000" doc:name="SMTP"/>
    </flow>
</mule>

1 个答案:

答案 0 :(得分:3)

让我们看看以下内容是否对您有所帮助。只需将您的文件连接器替换为您的FTP连接器即可。

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

<mule xmlns:smtp="http://www.mulesoft.org/schema/mule/smtp" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:file="http://www.mulesoft.org/schema/mule/file"
    xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper"
    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" version="EE-3.5.0"
    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/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.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/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.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/smtp http://www.mulesoft.org/schema/mule/smtp/current/mule-smtp.xsd">
    <db:mysql-config name="MySQL_Configuration" host="localhost"
        port="3306" user="" password="" database="information"
        doc:name="MySQL Configuration" />
    <data-mapper:config name="CSV_To_XML" transformationGraphPath="csv_to_xml.grf" doc:name="CSV_To_XML"/>
    <flow name="stackoverflow-ftp-dbFlow1" doc:name="stackoverflow-ftp-dbFlow1">
        <file:inbound-endpoint path="" responseTimeout="10000" doc:name="File" />
        <data-mapper:transform config-ref="CSV_To_XML" doc:name="CSV To XML"/>
        <byte-array-to-string-transformer doc:name="Byte Array to String"/>
        <foreach collection="#[xpath('//info')]" doc:name="For Each">
            <mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
            <db:insert config-ref="MySQL_Configuration" doc:name="Database">
                <db:parameterized-query><![CDATA[INSERT INTO `information`.`user_table`
(`current_date`,
`serialnumber`,
`gender`,
`fullname`,
`birthdate`,
`email`,
`mobilnumber`,
`address`)
VALUES
(#[xpath://date],
#[xpath://serialnumber],
#[xpath://gender],
#[xpath://fullname],
#[xpath://birthdate],
#[xpath://email],
#[xpath://mobilenumber],
#[xpath://address]
);]]></db:parameterized-query>
            </db:insert>
            <message-properties-transformer scope="invocation" doc:name="Message Properties">
                <add-message-property key="count" value="#[flowVars.counter]"/>
            </message-properties-transformer>
        </foreach>
        <logger message="#[payload] Number of records #[flowVars.count]" level="INFO" doc:name="Logger"/>
        <set-payload value="#[flowVars.count]" doc:name="Set Payload"/>
        <smtp:outbound-endpoint host="localhost" responseTimeout="10000" doc:name="SMTP"/>
    </flow>
</mule>

我使用Anypoint Studio 3.5 of MuleSoft中的DataMapper功能来实际地将CSV中的映射到我在流中使用的XML。 在那里,您可以将csv的示例文件设置为输入示例,将xml设置为输出示例。

假设您提供的信息很少,我使用了以下文件......

用户csv作为DataMapper的输入格式示例:

date|serialnumber|gender|fullname|birthdate|email|mobilenumber|address
03-07-2014|129329|m|your name|12-03-1970|my@mail.com|219239|my address
04-07-2014|129329|w|her name|12-03-1975|her@mail.com|219229|her address

User XML I as DataMapper的输出格式示例注意:流变量和xpath表达式取决于它!!

<infos>
    <info>
        <date></date>
        <serialnumber></serialnumber>
        <gender></gender>
        <fullname></fullname>
        <birthdate></birthdate>
        <email></email>
        <mobilenumber></mobilenumber>
        <address></address>
    </info>
    <info>
        <date></date>
        <serialnumber></serialnumber>
        <gender></gender>
        <fullname></fullname>
        <birthdate></birthdate>
        <email></email>
        <mobilenumber></mobilenumber>
        <address></address>
    </info>
</infos>
祝你好运!

PS:不要忘记将mysql lib添加到类路径或maven。

相关问题