如何将CSV值插入postgres数据库

时间:2013-11-29 12:34:44

标签: mule mule-studio mule-el

我已完成此配置

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

<mule xmlns:jdbc-ee="http://www.mulesoft.org/schema/mule/ee/jdbc" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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.4.1" 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/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/ee/jdbc http://www.mulesoft.org/schema/mule/ee/jdbc/current/mule-jdbc-ee.xsd">
<configuration doc:name="Configuration">
    <expression-language autoResolveVariables="true">
        <import class="org.mule.util.StringUtils" />
        <import class="org.mule.util.ArrayUtils" />
    </expression-language>
</configuration>

    <jdbc-ee:postgresql-data-source name="PostgreSQL_Data_Source" user="youtilitydba" password="Youtility11" url="jdbc:postgresql://localhost:5432/sample" transactionIsolation="UNSPECIFIED" doc:name="PostgreSQL Data Source"/>
    <jdbc-ee:connector name="Database" dataSource-ref="PostgreSQL_Data_Source" validateConnections="true" queryTimeout="-1" pollingFrequency="0" doc:name="Database"/>
    <flow name="CSVtoDATABASEFlow" doc:name="CSVtoDATABASEFlow">
        <file:inbound-endpoint path="/home/youtility2/Desktop/ESB/infile" moveToDirectory="/home/youtility2/Desktop/ESB/out" pollingFrequency="100" responseTimeout="10000" doc:name="File">
            <file:filename-regex-filter pattern="data.csv" caseSensitive="false"/>
        </file:inbound-endpoint>
        <object-to-string-transformer doc:name="Object to String"/>
        <logger message="before split ***************** #[payload]" level="INFO" doc:name="Logger"/>
        <splitter expression="#[rows=StringUtils.split(message.payload, '\n\r');ArrayUtils.subarray(rows,1,rows.size())]" doc:name="Splitter"/>
        <logger message="after split ******************* #[payload]" level="INFO" doc:name="Logger"/>
        <logger message="before db  ***********************   #[message.payload]" level="INFO" doc:name="Logger"/>
        <jdbc-ee:outbound-endpoint exchange-pattern="one-way" queryKey="insert" queryTimeout="-1" connector-ref="Database" doc:name="Database">
            <jdbc-ee:query key="insert" value="insert into users(firstname,lastname) values(#[message.payload],#[message.payload])"/>
        </jdbc-ee:outbound-endpoint>
    </flow>
</mule>

但是插入了空值并且只有值的第一个字母我怎样才能将我的愿望值插入到db中 csv文件就像这样有2个cloumns 名字姓氏 ff kk dd gg

并且我记录了我的选择很好,但无法处理进入db甚至其按行吐出 插入为值时,某些问题是der 我的日志

INFO  2013-11-29 16:53:33,110 [[csvtodatabase].CSVtoDATABASEFlow.stage1.02] org.mule.api.processor.LoggerMessageProcessor: before split ***************** firstname lastname
suresh  chirra
prasahnt    daware
anil    kancheti

INFO  2013-11-29 16:53:33,126 [[csvtodatabase].CSVtoDATABASEFlow.stage1.02] org.mule.api.processor.LoggerMessageProcessor: after split ******************* suresh   chirra
INFO  2013-11-29 16:53:33,130 [[csvtodatabase].CSVtoDATABASEFlow.stage1.02] org.mule.api.processor.LoggerMessageProcessor: before db  ***********************   suresh  chirra
INFO  2013-11-29 16:53:33,134 [[csvtodatabase].Database.dispatcher.01] org.mule.lifecycle.AbstractLifecycleManager: Initialising: 'Database.dispatcher.2022023948'. Object is: EEJdbcMessageDispatcher
INFO  2013-11-29 16:53:33,135 [[csvtodatabase].Database.dispatcher.01] org.mule.lifecycle.AbstractLifecycleManager: Starting: 'Database.dispatcher.2022023948'. Object is: EEJdbcMessageDispatcher
INFO  2013-11-29 16:53:33,135 [[csvtodatabase].CSVtoDATABASEFlow.stage1.02] org.mule.api.processor.LoggerMessageProcessor: after split ******************* prasahnt daware
INFO  2013-11-29 16:53:33,137 [[csvtodatabase].CSVtoDATABASEFlow.stage1.02] org.mule.api.processor.LoggerMessageProcessor: before db  ***********************   prasahnt    daware
INFO  2013-11-29 16:53:33,139 [[csvtodatabase].Database.dispatcher.02] org.mule.lifecycle.AbstractLifecycleManager: Initialising: 'Database.dispatcher.1701338672'. Object is: EEJdbcMessageDispatcher
INFO  2013-11-29 16:53:33,139 [[csvtodatabase].Database.dispatcher.02] org.mule.lifecycle.AbstractLifecycleManager: Starting: 'Database.dispatcher.1701338672'. Object is: EEJdbcMessageDispatcher
INFO  2013-11-29 16:53:33,143 [[csvtodatabase].CSVtoDATABASEFlow.stage1.02] org.mule.api.processor.LoggerMessageProcessor: after split ******************* anil kancheti
INFO  2013-11-29 16:53:33,149 [[csvtodatabase].CSVtoDATABASEFlow.stage1.02] org.mule.api.processor.LoggerMessageProcessor: before db  ***********************   anil    kancheti
INFO  2013-11-29 16:53:33,152 [[csvtodatabase].Database.dispatcher.03] org.mule.lifecycle.AbstractLifecycleManager: Initialising: 'Database.dispatcher.660999458'. Object is: EEJdbcMessageDispatcher
INFO  2013-11-29 16:53:33,152 [[csvtodatabase].Database.dispatcher.03] org.mule.lifecycle.AbstractLifecycleManager: Starting: 'Database.dispatcher.660999458'. Object is: EEJdbcMessageDispatcher
INFO  2013-11-29 16:53:33,227 [[csvtodatabase].Database.dispatcher.02] com.mulesoft.mule.transport.jdbc.sqlstrategy.UpdateSqlStatementStrategy: Executing SQL statement: 1 row(s) updated
INFO  2013-11-29 16:53:33,227 [[csvtodatabase].Database.dispatcher.03] com.mulesoft.mule.transport.jdbc.sqlstrategy.UpdateSqlStatementStrategy: Executing SQL statement: 1 row(s) updated
INFO  2013-11-29 16:53:33,227 [[csvtodatabase].Database.dispatcher.01] com.mulesoft.mule.transport.jdbc.sqlstrategy.UpdateSqlStatementStrategy: Executing SQL statement: 1 row(s) updated
WARN  2013-11-29 17:51:30,468 [Finalizer] org.mule.transport.file.ReceiverFileInputStream: Failed to move file from /home/youtility2/Desktop/ESB/infile/data.csv to /home/youtility2/Desktop/ESB/out/data.cs

v  我的第3个日志无法打印值我怎么能实现这一点 比一吨

1 个答案:

答案 0 :(得分:0)

你忘了使用

 <expression-transformer expression="#[StringUtils.split(message.payload, ',')]" />

并在使用JDBC Component之前使用查询: - insert into my_table(col1, col2) values(#[message.payload[0]],#[message.payload[1]])


无论如何,您可以使用以下流程并根据您的要求对其进行修改...以下是您的完整XML流程: -

  <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>