...
Option | Type | Description | |||||
---|---|---|---|---|---|---|---|
format | CSVFormat | The reference format to use, it will be updated with the other format options, the default value is CSVFormat.DEFAULT | |||||
formatName commentMarkerDisabled | String | Used in order to initialize the reference format using the name of one of the default formats. The possible values are: | boolean | Disables the comment marker of the reference format. This option is | |||
commentMarker | Character | Overrides the comment marker of the reference format. This option is | |||||
delimiter | Character | Overrides the delimiter of the reference format. This option is | |||||
escapeDisabled | boolean | Disables the escape character of the reference format. This option is | |||||
escape | Character | Overrides the escape character of the reference format. This option is | |||||
headerDisabled | boolean | Disables the header of the reference format. This option is | |||||
header | String[] | Overrides the header of the reference format. This option is In the XML DSL, this option is configured using children
| |||||
allowMissingColumnNames | Boolean | Overrides the missing column names behavior of the reference format. This option is | |||||
ignoreEmptyLines | Boolean | Overrides the empty line behavior of the reference format. This option is | |||||
ignoreSurroundingSpaces | Boolean | Overrides the surrounding spaces behavior of the reference format. This option is | |||||
nullStringDisabled | boolean | Disables the null string representation of the reference format. This option is | |||||
nullString | String | Overrides the null string representation of the reference format. This option is | |||||
quoteDisabled | boolean | Disables the quote of the reference format. This option is | |||||
quote | Character | Overrides the quote symbol of the reference format. This option is | |||||
quoteMode | QuoteMode | Overrides the quote mode of the reference format. This option is | |||||
recordSeparatorDisabled | boolean | Disables the record separator of the reference format. This option is | |||||
recordSeparator | String | Overrides the record separator of the reference format. This option is | |||||
skipHeaderRecord | Boolean | Overrides the header record behavior of the reference format. This option is | |||||
lazyLoad | boolean | 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 | |||||
useMaps | boolean | 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 | |||||
recordConverter | CsvRecordConverter | Sets the record converter to use. If defines the This option is |
...
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
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.
Considering the following body |
| |||||
and this Java route definition |
| |||||
or this XML route definition |
| |||||
then it will produce |
|
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 | ||
---|---|---|
| ||
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=text|url=camel/trunk/components/camel-csv/src/test/resources/daltons.csv} |
We can now use the CSV component to unmarshal this file:
Code Block | ||
---|---|---|
| ||
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 | ||
---|---|---|
| ||
List<List<String>> data = (List<List<String>>) exchange.getIn().getBody(); for (List<String> line : data) { LOG. |
...
Wiki Markup |
---|
{snippet:id=unmarshalResult|lang=java|url=camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteTest.javadebug(String.format("%s has an IQ of %s and is currently %s", line.get(0), line.get(1), line.get(2))); } |
Marshalling a List<Map> to CSV
...
Marshaling with a pipe as delimiter
...
Considering the following body |
|
...
|
...
|
...
|
...
|
...
|
...
| |
and this Java route definition |
...
|
...
|
...
|
...
|
...
| ||
or this XML route definition |
|
...
|
...
|
...
| ||||||
then it will produce |
|
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:
...