Search code examples
javamuleesb

MULE ESB Import CSV, if no CSV column data (blank data) , errors out


I am using this MULE ESB flow to import a CSV file into a MYSQL database. I have determined that if there is a CSV column that is 'empty' The MULE flow will error out with the following error:

Message               : Execution of the expression "message.payload[4]" failed. (org.mule.api.expression.ExpressionRuntimeException). Message payload is of type: String[]

Here is the CSV that causes the error:

 Gone,2691,,13-Jul,8/10/2010

However if I change the CSV file to this (notice the addition of white space) this will work as intended.

Gone,2691, ,13-Jul,8/10/2010

Do I need to run an additional step in my CSV (before Mule processing) to change all 'blank' values to say NULL, or is there a method within Mule to import these blank value in the CSV file?

Here is the config file I am using:

<context:property-placeholder location="classpath:mysql.properties,classpath:smtp.properties" />
<configuration doc:name="Configuration"> 
    <expression-language autoResolveVariables="true"> 
        <import class="org.mule.util.StringUtils"></import>  
    </expression-language>  
</configuration>

<db:mysql-config name="MySQL_Configuration" host="${mysql.host}" port="${mysql.port}" user="${mysql.user}" database="${mysql.database}" password="${mysql.password}" doc:name="MySQL Configuration"/>

<flow name="ImportCSVFileToDatabase" > 
    <file:inbound-endpoint path="\\192.168.10.6\pfw" pollingFrequency="5000" doc:name="Source" responseTimeout="10000" moveToDirectory="C:\Users\IEUser\Desktop\Post Processed">
        <file:filename-regex-filter pattern="Tractor_Status.csv" caseSensitive="true"/>
    </file:inbound-endpoint>  
    <object-to-string-transformer doc:name="Object to String"></object-to-string-transformer>  
    <splitter expression="#[StringUtils.split(message.payload, '\n\r')]" doc:name="Splitter"></splitter>  
    <expression-transformer expression="#[StringUtils.split(message.payload, ',')]" doc:name="Expression" ></expression-transformer> 
            <db:insert config-ref="MySQL_Configuration" doc:name="Database">
        <db:parameterized-query><![CDATA[INSERT INTO Tractor_Sheet(
 Status,
 Build_Number,
 Ship_Date,
 Production_Completion_Date
 ) VALUES (
 #[message.payload[0]],
 #[message.payload[1]],
 #[message.payload[2]],
 #[message.payload[4]]
 )]]></db:parameterized-query>
            </db:insert>    
  </flow>

Solution

  • If anyone stumbles across this is in the future, I added an additional expression

    #[StringUtils.replace(message.payload, ",,", ", ,")]