Created
June 23, 2011 18:58
-
-
Save MoriTanosuke/1043310 to your computer and use it in GitHub Desktop.
Unmarshal CSV with Apache Camel and Bindy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.kopis.camel.model; | |
import org.apache.camel.dataformat.bindy.annotation.CsvRecord; | |
import org.apache.camel.dataformat.bindy.annotation.DataField; | |
@CsvRecord(separator = ",") | |
public class CsvBean { | |
@DataField(pos = 1) | |
private String first; | |
@DataField(pos = 2) | |
private String second; | |
@DataField(pos = 3) | |
private String third; | |
@DataField(pos = 4) | |
private String fourth; | |
public String getFirst() { | |
return first; | |
} | |
public void setFirst(String first) { | |
this.first = first; | |
} | |
public String getSecond() { | |
return second; | |
} | |
public void setSecond(String second) { | |
this.second = second; | |
} | |
public String getThird() { | |
return third; | |
} | |
public void setThird(String third) { | |
this.third = third; | |
} | |
public String getFourth() { | |
return fourth; | |
} | |
public void setFourth(String fourth) { | |
this.fourth = fourth; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.kopis.camel; | |
import static org.junit.Assert.*; | |
import java.util.List; | |
import org.apache.camel.builder.RouteBuilder; | |
import org.apache.camel.component.mock.MockEndpoint; | |
import org.apache.camel.test.junit4.CamelTestSupport; | |
import org.junit.Test; | |
public class CsvToBeanTest extends CamelTestSupport { | |
@Test | |
public void testCsv() throws Exception { | |
MockEndpoint mock = getMockEndpoint("mock:queue.csv"); | |
mock.expectedMessageCount(2); | |
assertMockEndpointsSatisfied(); | |
List line1 = mock.getReceivedExchanges().get(0).getIn().getBody(List.class); | |
assertEquals("row 01", line1.get(0)); | |
assertEquals("row 02", line1.get(1)); | |
assertEquals("", line1.get(2)); | |
assertEquals("row 04", line1.get(3)); | |
List line2 = mock.getReceivedExchanges().get(1).getIn().getBody(List.class); | |
assertEquals("row 11", line2.get(0)); | |
assertEquals("row 12", line2.get(1)); | |
assertEquals("row 13", line2.get(2)); | |
assertEquals("row 14", line2.get(3)); | |
} | |
@Override | |
protected RouteBuilder createRouteBuilder() throws Exception { | |
return new RouteBuilder() { | |
@Override | |
public void configure() throws Exception { | |
context.setTracing(true); | |
from("file://src/test/resources?noop=true&fileName=test.csv") | |
.unmarshal().csv() | |
.split(body()) | |
.to("mock:queue.csv"); | |
} | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.kopis.camel; | |
import java.util.List; | |
import java.util.Map; | |
import org.apache.camel.builder.RouteBuilder; | |
import org.apache.camel.component.mock.MockEndpoint; | |
import org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat; | |
import org.apache.camel.test.junit4.CamelTestSupport; | |
import org.junit.Test; | |
import de.kopis.camel.model.CsvBean; | |
public class CsvToBeanWithBindyTest extends CamelTestSupport { | |
@Test | |
public void testCsv() throws Exception { | |
MockEndpoint mock = getMockEndpoint("mock:queue.csv"); | |
mock.expectedMessageCount(1); | |
assertMockEndpointsSatisfied(); | |
List csv = (List) mock.getReceivedExchanges().get(0).getIn().getBody(); | |
Map map1 = (Map) csv.get(0); | |
CsvBean csv1 = (CsvBean) map1.get("de.kopis.camel.model.CsvBean"); | |
assertEquals("row 01", csv1.getFirst()); | |
Map map2 = (Map) csv.get(1); | |
CsvBean csv2 = (CsvBean) map2.get("de.kopis.camel.model.CsvBean"); | |
assertEquals("row 11", csv2.getFirst()); | |
} | |
@Override | |
protected RouteBuilder createRouteBuilder() throws Exception { | |
return new RouteBuilder() { | |
@Override | |
public void configure() throws Exception { | |
context.setTracing(true); | |
from("file://src/test/resources?noop=true&fileName=test.csv") | |
.unmarshal(new BindyCsvDataFormat("de.kopis.camel.model")) | |
.to("mock:queue.csv"); | |
} | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>de.kopis.camel</groupId> | |
<artifactId>camel-example</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.8.2</version> | |
<type>jar</type> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.camel</groupId> | |
<artifactId>camel</artifactId> | |
<version>2.7.2</version> | |
<type>pom</type> | |
<scope>compile</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.camel</groupId> | |
<artifactId>camel-bindy</artifactId> | |
<version>2.7.2</version> | |
<scope>compile</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.camel</groupId> | |
<artifactId>camel-csv</artifactId> | |
<version>2.7.2</version> | |
<scope>compile</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.slf4j</groupId> | |
<artifactId>slf4j-simple</artifactId> | |
<version>1.6.1</version> | |
<type>jar</type> | |
<scope>compile</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.camel</groupId> | |
<artifactId>camel-test</artifactId> | |
<version>2.7.2</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
definately late to the party, just want to say Excellent example! thank you