Lets see how to read a file with multiple rows using CAMEL.
Create a java project in eclipse.
Create a folder by name testFileFolder and place the below given file.
The file that we are going to read, lets say demo.txt
data in file,
line one data
line two data
line three data
line four data
line five data
line six data
Next create a java file by name SplitData.java as given below,
package com.techexample.processor;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
public class SplitData implements Processor {
public static int count = 0;
@Override
public void process(Exchange exchange) throws Exception {
String data=(String)exchange.getIn().getBody();
count++;
System.out.println("Line "+count+" info is: "+data);
System.out.println("=============================");
}
}
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
public class SplitData implements Processor {
public static int count = 0;
@Override
public void process(Exchange exchange) throws Exception {
String data=(String)exchange.getIn().getBody();
count++;
System.out.println("Line "+count+" info is: "+data);
System.out.println("=============================");
}
}
Now the main part, we are going to create the camel context file.
Create a file with name camelContext.xml and place it inside a folder say Resource
The file context is,
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<bean id="splitdata" class="com.techexample.processor.SplitData" />
<camel:camelContext id="readfile" trace="true">
<camel:route id="CsvSplit" trace="true">
<camel:from uri="file:testFileFolder?noop=true;" />
<camel:split>
<camel:tokenize token="\n" />
<camel:to uri="bean:splitdata" />
</camel:split>
</camel:route>
</camel:camelContext>
</beans>
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">
<bean id="splitdata" class="com.techexample.processor.SplitData" />
<camel:camelContext id="readfile" trace="true">
<camel:route id="CsvSplit" trace="true">
<camel:from uri="file:testFileFolder?noop=true;" />
<camel:split>
<camel:tokenize token="\n" />
<camel:to uri="bean:splitdata" />
</camel:split>
</camel:route>
</camel:camelContext>
</beans>
Now, we have to write a java file to bring up the camel context file.
Steps to run the camel project
Go to project properties, under java buildpath -> source , add the resource and testfileFolder.
Now right click in our MyClient.java file and click runAs -> java application
OUTPUT:
Line 1 info is: line one data
=============================
Line 2 info is: line two data
=============================
Line 3 info is: line three data
=============================
Line 4 info is: line four data
=============================
Line 5 info is: line five data
=============================
Line 6 info is: line six data
=============================
No comments:
Post a Comment