CSV
The CSV Data Format uses Apache Commons CSV
to handle CSV payloads (Comma Separated Values) such as those exported/imported by Excel.
Marshalling a Map to CSV
The component allows you to marshal a Java Map (or any other message type that can be converted in a Map) into a CSV payload.
An example: if you send a message with this map...
Map body = new HashMap();
body.put("foo", "abc");
body.put("bar", 123);
... through this route ...
from("direct:start").
marshal().csv().
to("mock:result");
... you will end up with a String containing this CSV message
Sending the Map below through this route will result in a CSV message that looks like foo,bar
Unmarshalling a CSV message into a Java List
Unmarshalling will transform a CSV messsage into a Java List with CSV file lines (containing another List with all the field values).
An example: we have a CSV file with names of persons, their IQ and their current activity.
Jack Dalton, 115, mad at Averell
Joe Dalton, 105, calming Joe
William Dalton, 105, keeping Joe from killing Averell
Averell Dalton, 80, playing with Rantanplan
Lucky Luke, 120, capturing the Daltons
We can now use the CSV component to unmarshal this file:
from("file:src/test/resources/daltons.csv?noop=true").
unmarshal().csv().
to("mock:daltons");
The resulting message will contain a List<List<String>> like...
List<List<String>> data = "" class="code-object">String>>) exchange.getIn().getBody();
for (List<String> line : data) {
LOG.debug(String.format("%s has an IQ of %s and is currently %s",
line.get(0), line.get(1), line.get(2)));
}
File Poller of CSV, then unmarshaling
Given a bean which can handle the incoming data...
public void doHandleCsvData(List<List<String>> csvData)
{
}
... your route then looks as follows
<route>
<from uri="file:///some/path/to/pickup/csvfiles?delete=true&consumer.delay=10000" />
<unmarshal><csv /></unmarshal>
<to uri="bean:myCsvHandler?method=doHandleCsvData" />
</route>