Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

The CSV Data Format uses Apache Commons CSV to handle CSV payloads (Comma Separated Values) such as those exported/imported by Excel.

...

As of Camel 2.15.0, it now uses the Apache Commons CSV 1.1 which is based on a completely different set of options.

Available options until Camel 2.15

Option

Type

Description

config

CSVConfig

Can be used to set a custom CSVConfig object.

strategy

CSVStrategy

Can be used to set a custom CSVStrategy; the default is CSVStrategy.DEFAULT_STRATEGY.

autogenColumns

boolean

Whether or not columns are auto-generated in the resulting CSV. The default value is true; subsequent messages use the previously created columns with new fields being added at the end of the line.

delimiter

String

Camel 2.4: The column delimiter to use; the default value is ",".

skipFirstLine

boolean

Camel 2.10: Whether or not to skip the first line of CSV input when unmarshalling (e.g. if the content has headers on the first line); the default value is false.

lazyLoadbooleanCamel 2.12.2: Whether or not to Sequential access CSV input through an iterator which could avoid OOM exception when processing huge CSV file; the default value is false
useMapsbooleanCamel 2.13: Whether to use List<Map> when unmarshalling instead of List<List>.

Available options as of Camel 2.15

OptionTypeDescription
formatCSVFormatThe reference format to use, it will be updated with the other format options, the default value is CSVFormat.DEFAULT
commentMarkerDisabledboolean

Disables the comment marker of the reference format.

This option is false by default.

commentMarkerCharacter

Overrides the comment marker of the reference format.

This option is null by default. When null it keeps the value of the reference format which is null for CSVFormat.DEFAULT.

delimiterCharacter

Overrides the delimiter of the reference format.

This option is null by defaut. When null it keeps the value of the reference format which is ',' for CSVFormat.DEFAULT.

escapeDisabledboolean

Disables the escape character of the reference format.

This option is false by default.

escapeCharacter

Overrides the escape character of the reference format.

This option is null by default. When null it keeps the value of the reference format which is null for CSVFormat.DEFAULT.

headerDisabledboolean

Disables the header of the reference format.

This option is false by default.

headerString[]

Overrides the header of the reference format.

This option is null by default. When null it keeps the value of the reference format which is null for CSVFormat.DEFAULT.

In the XML DSL, this option is configured using children <header> tags:

Code Block
languagexml
<csv >
    <header>orderId</header>
    <header>amount</header>
</csv>
allowMissingColumnNamesBoolean

Overrides the missing column names behavior of the reference format.

This option is null by default. When null it keeps the value of the reference format which is false for CSVFormat.DEFAULT.

ignoreEmptyLinesBoolean

Overrides the empty line behavior of the reference format.

This option is null by default. When null it keeps the value of the reference format which is true for CSVFormat.DEFAULT.

ignoreSurroundingSpacesBoolean

Overrides the surrounding spaces behavior of the reference format.

This option is null by default. When null it keeps the value of the reference format which is false for CSVFormat.DEFAULT.

nullStringDisabledboolean

Disables the null string representation of the reference format.

This option is false by default.

nullStringString

Overrides the null string representation of the reference format.

This option is null by default. When null it keeps the value of the reference format which is null for CSVFormat.DEFAULT.

quoteDisabledboolean

Disables the quote of the reference format.

This option is false by default.

quoteCharacter

Overrides the quote symbol of the reference format.

This option is null by default. When null it keeps the value of the reference format which is '"' (double quote) for CSVFormat.DEFAULT.

quoteModeQuoteMode

Overrides the quote mode of the reference format.

This option is null by default. When null it keeps the value of the reference format which is null for CSVFormat.DEFAULT.

recordSeparatorDisabledboolean

Disables the record separator of the reference format.

This option is false by default.

recordSeparatorString

Overrides the record separator of the reference format.

This option is null by default. When null it keeps the value of the reference format which is \r\n (CRLF) for CSVFormat.DEFAULT.

skipHeaderRecordBoolean

Overrides the header record behavior of the reference format.

This option is null by default. When null it keeps the value of the reference format which is false for CSVFormat.DEFAULT.

lazyLoadboolean

Whether the unmarshalling should produce an iterator that reads the lines on the fly or if all the lines must be read at one.

This option is false by default.

useMapsboolean

Whether the unmarshalling should produce maps for the lines values instead of lists. It requires to have header (either defined or collected).

This options is false by default.

recordConverterCsvRecordConverter

Sets the record converter to use. If defines the useMaps options is disabled.

This option is null by default.

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

Wiki Markup
{snippet:id=marshalInput|lang=java|url=camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteTest.java}

... through this route ...

Wiki Markup
{snippet:id=marshalRoute|lang=java|url=camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteTest.java}

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

No Format
abc,123

Sending the Map below through this route will result in a CSV message that looks like foo,bar

Considering the following body
Code Block
languagejava
Map<String, Object> body = new LinkedHashMap<>();
body.put("foo", "abc");
body.put("bar", 123);
and this Java route definition
Code Block
languagejava
from("direct:start")
    .marshal().csv()
    .to("mock:result");
or this XML route definition
Code Block
languagexml
<route>
    <from uri="direct:start" />
    <marshal>
        <csv />
    </marshal>
    <to uri="mock:result" />
</route>
then it will produce
Code Block
languagetext
abc,123

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.

Code Block
languagetext
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
Wiki Markup
{snippet:lang=sql|url=camel/trunk/components/camel-csv/src/test/resources/daltons.csv}

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

Code Block
languagejava
from("file:
Wiki Markup
{snippet:id=unmarshalRoute|lang=java|url=camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteTest.java}resources/?fileName=daltons.csv&noop=true")
    .unmarshal().csv()
    .to("mock:daltons");

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

Code Block
languagejava
List<List<String>> data = (List<List<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)));
Wiki Markup
{snippet:id=unmarshalResult|lang=java|url=camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteTest.java}

Marshalling a List<Map> to CSV

...

Marshaling with a pipe as delimiter

 Using the Spring/XML DSL:

 

Considering the following body
Code Block

...

language

...

java

...

Map<String, Object> 

...

body 

...

=

...

 new LinkedHashMap<>();
body.put("foo", "abc");
body.put("bar", 123);
and this Java route definition

...

Code Block
languagejava

...

// 

...

Camel 

...

version 

...

< 

...

2.15
CsvDataFormat oldCSV = new 

...

CsvDataFormat();

...

oldCSV.setDelimiter(

...

"|

...

");

...

from("direct:start")
    .marshal(

...

oldCSV)
    .

...

languagejava

...

to("mock:result")
 
// Camel version >= 2.15
from("direct:start")
    .marshal(new CsvDataFormat()

...

.setDelimiter('|'))
    .to("mock:result")
or this XML route definition
Code Block
xml
xml
<route>
  <from uri="direct:start"

...

 />
  <marshal>
    <csv delimiter="|" />
  </marshal>
  <to uri="mock:result" />
</route>
then it will produce
Code Block
languagetext
abc|123

Using autogenColumns, configRef and strategyRef attributes inside XML DSL

Available as of Camel 2.9.2 / 2.10 and deleted for Camel 2.15

You can customize the CSV Data Format to make use of your own CSVConfig and/or CSVStrategy. Also note that the default value of the autogenColumns option is true. The following example should illustrate this customization.

...

Available as of Camel 2.10 and deleted for Camel 2.15

You can instruct the CSV Data Format to skip the first line which contains the CSV headers. Using the Spring/XML DSL:

...