Jun 4, 2013

Standalone Apache Camel with Spring tutorial -- file transformer

In the last Apache Camel with Spring tutorial we went through polling a folder for a file pattern, and then zip and copy the file to the archive subfolder. This tutorial extends that to show how we can read a file say test1.csv in the format shown below

firstname,surname, age
John, Smith, 35
Peter, Smith, 25

and then convert it to a format shown below where fields firstname and surname are swapped.


Step 1: Add camel-csv dependency to the pom.xml file that enables reading csv files


Step 2: The next step is to define the routes in the simple_file_route.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
 xmlns:p="" xmlns:c=""
 xmlns:xsi="" xmlns:camel=""

 <routeContext id="simpleFileRoute" xmlns="">
  <route id="fileIn">
    uri="file://c:/temp/simple/input?include=test.*csv&delay=600000&initialDelay=5000" />

    <csv skipFirstLine="true" />
   <to uri="bean:personMapper" />
    <csv delimiter="," />
   <convertBodyTo type="java.lang.String" />
   <to uri="file://c:/temp/simple/output?fileName=${file:name.noext}_new.csv" />

Step 3: The above route uses the "PersonMapper" bean class to transform the fields. This is basically translating the file by rearranging "surname" and "firstname" columns.

package com.mycompany.app3;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

public class PersonMapper
    public Object service(List<List<String>> data)
        List<Map<String, String>> results = new ArrayList<Map<String, String>>();
        Map<String, String> headerLine = new LinkedHashMap<String, String>();
        headerLine.put("surname", "surname");
        headerLine.put("firstname", "firstname");
        headerLine.put("age", "age");
        for (List<String> line : data)
            Map<String, String> resultLine = new LinkedHashMap<String, String>();
            resultLine.put("surname", line.get(1));
            resultLine.put("firstname", line.get(0));
            resultLine.put("age", line.get(2));
        return results;

That's all to it. This is the power of the integration framework Apache Camel. It supports 20+ protocols like ftp, smtp, etc.



Post a Comment

Subscribe to Post Comments [Atom]

<< Home