You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 11 Next »

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...

Error formatting macro: snippet: java.lang.NullPointerException

... through this route ...

Error formatting macro: snippet: java.lang.NullPointerException

... you will end up with a String containing this CSV message

abc,123

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.

Error formatting macro: snippet: java.lang.NullPointerException

We can now use the CSV component to unmarshal this file:

Error formatting macro: snippet: java.lang.NullPointerException

The resulting message will contain a List<List<String>> like...

Error formatting macro: snippet: java.lang.NullPointerException

File Poller of CSV, then unmarshaling

Given a bean which can handle the incoming data...

MyCsvHandler.java
// Some comments here
public void doHandleCsvData(List<List<String>> csvData)
{
    // do magic here
}

... your route then looks as follows

<route>
        <!-- poll every 10 seconds -->
        <from uri="file:///some/path/to/pickup/csvfiles?delete=true&amp;consumer.delay=10000" />
        <unmarshal><csv /></unmarshal>
        <to uri="bean:myCsvHandler?method=doHandleCsvData" />
</route>

Dependencies

To use CSV in your camel routes you need to add the a dependency on camel-csv which implements this data format.

If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions).

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-csv</artifactId>
  <version>1.5.0</version>
</dependency>
  • No labels