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.
surname,firstname,age Smith,John,35 Smith,Peter,25
Step 1: Add camel-csv dependency to the pom.xml file that enables reading csv files
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-csv</artifactId> <version>2.10.4</version> </dependency>
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="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> <routeContext id="simpleFileRoute" xmlns="http://camel.apache.org/schema/spring"> <route id="fileIn"> <from uri="file://c:/temp/simple/input?include=test.*csv&delay=600000&initialDelay=5000" /> <unmarshal> <csv skipFirstLine="true" /> </unmarshal> <to uri="bean:personMapper" /> <marshal> <csv delimiter="," /> </marshal> <convertBodyTo type="java.lang.String" /> <to uri="file://c:/temp/simple/output?fileName=${file:name.noext}_new.csv" /> </route> </routeContext> </beans>
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; @Component("personMapper") 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"); results.add(headerLine); 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)); results.add(resultLine); } 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.
Labels: Apache Camel
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home