...
Option | Type | Description |
---|---|---|
config | CSVConfig | Can be used to set a custom |
strategy | CSVStrategy | Can be used to set a custom |
autogenColumns | boolean | Whether or not columns are auto-generated in the resulting CSV. The default value is |
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 |
lazyLoad | boolean | Camel 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 |
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.
...
Given a bean which can handle the incoming data...
Code Block | ||||
---|---|---|---|---|
| ||||
// Some comments here
public void doHandleCsvData(List<List<String>> csvData)
{
// do magic here
}
|
... your route then looks as follows
Code Block | ||
---|---|---|
| ||
<route>
<!-- poll every 10 seconds -->
<from uri="file:///some/path/to/pickup/csvfiles?delete=true&consumer.delay=10000" />
<unmarshal><csv /></unmarshal>
<to uri="bean:myCsvHandler?method=doHandleCsvData" />
</route>
|
...
Using the Spring/XML DSL:
Code Block | ||||
---|---|---|---|---|
| ||||
<route>
<from uri="direct:start" />
<marshal>
<csv delimiter="|" />
</marshal>
<to uri="bean:myCsvHandler?method=doHandleCsv" />
</route>
|
Or the Java DSL:
Code Block | ||
---|---|---|
| ||
CsvDataFormat csv = new CsvDataFormat();
CSVConfig config = new CSVConfig();
config.setDelimiter('|');
csv.setConfig(config);
from("direct:start")
.marshal(csv)
.convertBodyTo(String.class)
.to("bean:myCsvHandler?method=doHandleCsv");
|
Code Block | ||
---|---|---|
| ||
CsvDataFormat csv = new CsvDataFormat();
csv.setDelimiter("|");
from("direct:start")
.marshal(csv)
.convertBodyTo(String.class)
.to("bean:myCsvHandler?method=doHandleCsv");
|
...
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.
Code Block | ||||
---|---|---|---|---|
| ||||
<route>
<from uri="direct:start" />
<marshal>
<!-- make use of a strategy other than the default one which is 'org.apache.commons.csv.CSVStrategy.DEFAULT_STRATEGY' -->
<csv autogenColumns="false" delimiter="|" configRef="csvConfig" strategyRef="excelStrategy" />
</marshal>
<convertBodyTo type="java.lang.String" />
<to uri="mock:result" />
</route>
<bean id="csvConfig" class="org.apache.commons.csv.writer.CSVConfig">
<property name="fields">
<list>
<bean class="org.apache.commons.csv.writer.CSVField">
<property name="name" value="orderId" />
</bean>
<bean class="org.apache.commons.csv.writer.CSVField">
<property name="name" value="amount" />
</bean>
</list>
</property>
</bean>
<bean id="excelStrategy" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField" value="org.apache.commons.csv.CSVStrategy.EXCEL_STRATEGY" />
</bean>
|
...
You can instruct the CSV Data Format to skip the first line which contains the CSV headers. Using the Spring/XML DSL:
Code Block | ||||
---|---|---|---|---|
| ||||
<route>
<from uri="direct:start" />
<unmarshal>
<csv skipFirstLine="true" />
</unmarshal>
<to uri="bean:myCsvHandler?method=doHandleCsv" />
</route>
|
Or the Java DSL:
Code Block |
---|
CsvDataFormat csv = new CsvDataFormat();
csv.setSkipFirstLine(true);
from("direct:start")
.unmarshal(csv)
.to("bean:myCsvHandler?method=doHandleCsv");
|
...
Using the Spring/XML DSL:
Code Block | ||||
---|---|---|---|---|
| ||||
<route>
<from uri="direct:start" />
<unmarshal>
<csv delimiter="|" />
</unmarshal>
<to uri="bean:myCsvHandler?method=doHandleCsv" />
</route>
|
Or the Java DSL:
Code Block | ||
---|---|---|
| ||
CsvDataFormat csv = new CsvDataFormat();
CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY;
strategy.setDelimiter('|');
csv.setStrategy(strategy);
from("direct:start")
.unmarshal(csv)
.to("bean:myCsvHandler?method=doHandleCsv");
|
Code Block | ||
---|---|---|
| ||
CsvDataFormat csv = new CsvDataFormat();
csv.setDelimiter("|");
from("direct:start")
.unmarshal(csv)
.to("bean:myCsvHandler?method=doHandleCsv");
|
Code Block | ||
---|---|---|
| ||
CsvDataFormat csv = new CsvDataFormat();
CSVConfig csvConfig = new CSVConfig();
csvConfig.setDelimiter(";");
csv.setConfig(csvConfig);
from("direct:start")
.unmarshal(csv)
.to("bean:myCsvHandler?method=doHandleCsv");
|
Info | |||||
---|---|---|---|---|---|
| |||||
It looks like that
doesn't work. You have to set the delimiter as a String! |
...
If you use Maven you can just add the following to your pom.xml, substituting the version number for the latest and greatest release (see the download page for the latest versions).
Code Block | ||
---|---|---|
| ||
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-csv</artifactId>
<version>x.x.x</version>
</dependency>
|