本文介绍了如何使用Mule Studio的Mule ESB读取CSV文件并将数据插入PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Mule Studio的新手.

I am very new to Mule Studio.

我遇到了问题.我有一个要求,我需要使用Mule Studio将数据从CSV文件插入PostgreSQL数据库.

I am facing a problem. I have a requirement where I need to insert data from a CSV file to PostgreSQL Database using Mule Studio.

我正在使用Mule Studio CE(版本:1.3.1).我在Google中查看了ed,发现我们可以使用Data-mapper这样做.但这仅适用于EE.因此我无法使用它.

I am using Mule Studio CE (version: 1.3.1). I check ed in the Google and find that we can use Data-mapper for doing so. But it works only for EE .So I cannot use it.

我也在网上检查,发现了一篇文章.

Also I am checking in the net and found an article Using Mule Studio to read Data from PostgreSQL(Inbound) and write it to File (Outbound) - Step by Step approach.

这似乎可行,但我的要求与所给文章相反.我需要文件"作为入站数据,而数据库"作为出站"组件.

That seems feasible but my requirement is just the opposite of the article given. I need File as Inbound data while Databse as Outbound component.

这是怎么做的?

任何逐步的帮助(如使用哪些组件)和指导将不胜感激.

Any step by step help (like what components to use) and guidance will be greatly appreciated.

推荐答案

下面是插入两列CSV文件的示例:

Here is an example that inserts a two columns CSV file:

<configuration>
    <expression-language autoResolveVariables="true">
        <import class="org.mule.util.StringUtils" />
        <import class="org.mule.util.ArrayUtils" />
    </expression-language>
</configuration>

<spring:beans>
    <spring:bean id="jdbcDataSource" class=" ... your data source ... " />
</spring:beans>

<jdbc:connector name="jdbcConnector" dataSource-ref="jdbcDataSource">
    <jdbc:query key="insertRow"
        value="insert into my_table(col1, col2) values(#[message.payload[0]],#[message.payload[1]])" />
</jdbc:connector>

<flow name="csvFileToDatabase">
    <file:inbound-endpoint path="/tmp/mule/inbox"
        pollingFrequency="5000" moveToDirectory="/tmp/mule/processed">
         <file:filename-wildcard-filter pattern="*.csv" />
    </file:inbound-endpoint>

    <!-- Load all file in RAM - won't work for big files! -->
    <file:file-to-string-transformer />
    <!-- Split each row, dropping the first one (header) -->
    <splitter
        expression="#[rows=StringUtils.split(message.payload, '\n\r');ArrayUtils.subarray(rows,1,rows.size())]" />
    <!-- Transform CSV row in array -->
    <expression-transformer expression="#[StringUtils.split(message.payload, ',')]" />
    <jdbc:outbound-endpoint queryKey="insertRow" />
</flow>

这篇关于如何使用Mule Studio的Mule ESB读取CSV文件并将数据插入PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 09:46