mule-user
mule-user

Reputation: 223

Processing files line by line in mule

I have a requirement in my project to process a csv file line by line. Each line of csv is going t call a rest api, get data from the api and build output payload and finally write a file.

So suppose I have 10 lines in the csv file, each line will call api, get data from api and at the end write new xml output file with 10 segments.

So could you please suggest which component would be best to achive the splitting of csv file into each line. I assume splitter or foreach can serve the purpose. But I am not 100% sure. Any help or pointer is appreciated.

TIA

Upvotes: 0

Views: 10236

Answers (3)

Manik Magar
Manik Magar

Reputation: 1401

You can use file and for loop -

   <flow name="muleutilFlow6">
    <file:inbound-endpoint path="input" connector-ref="File" responseTimeout="10000" doc:name="File"/>
    <foreach collection="#[payload.split('\n')]]" doc:name="For Each">
        <logger message="Line - #[payload]" level="INFO" doc:name="Logger"/>
    </foreach>
</flow>

Or you can even use batch -

<flow name="muleutilFlow7">
    <file:inbound-endpoint path="input" connector-ref="File" responseTimeout="10000" doc:name="File"/>
    <file:file-to-string-transformer name="file-to-string" />
    <batch:execute name="muleutilBatch" doc:name="muleutilBatch"/>
</flow>
<batch:job name="muleutilBatch">
    <batch:input>
        <set-payload value="#[payload.split('\n')]" doc:name="Set Payload"/>
    </batch:input>
    <batch:process-records>
        <batch:step name="Batch_Step">
            <logger message="Line - #[payload]" level="INFO" doc:name="Logger"/>
        </batch:step>
    </batch:process-records>
</batch:job>

Upvotes: 2

chethan
chethan

Reputation: 36

The below code works for me

<configuration doc:name="Configuration">
<expression-language autoResolveVariables="true">
    <import class="org.mule.util.StringUtils" />
</expression-language>
</configuration>

<flow name="muleutilFlow6">
<file:inbound-endpoint path="C:\path\input sample" connector-ref="File" responseTimeout="10000" doc:name="File"/>
<object-to-string-transformer />
<splitter expression="#[StringUtils.split(message.payload, '\n\r')]" doc:name="Splitter" />
    <logger message="Line - #[payload]" level="INFO" doc:name="Logger"/>

Upvotes: 2

sulthony h
sulthony h

Reputation: 1277

You can do the following configuration:

  1. File connector (read CSV)
  2. DataWeave (transform CSV to Java Map and XML)
  3. Collection Splitter (iterate the Map)
  4. Call API and further data processing
  5. File connector (write XML)

Or refer to this example: Using Mule Studio to read CSV.... However You need further modification to achieve the requirement.

Upvotes: -1

Related Questions