...
Option | Type | Description |
---|---|---|
config | CSVConfig | Can be used to set a custom CSVConfig object. |
strategy | CSVStrategy | Camel uses by default |
autogenColumns | boolean | Camel 1.6.1/2.0: Is default true. By default, columns are autogenerated in the resulting CSV. Subsequent messages use the previously created columns with new fields being added at the end of the line. |
delimiter | String | Camel 2.4: Is default |
skipFirstLine | boolean | Camel 2.10: Is default false. While unmarshalling can be used to skip the first line of a CSV input which contains the CSV headers. |
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.
...
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>
|
Using skipFirstLine attribute while unmarshaling
Available as of Camel 2.10
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");
|
Unmarshaling with a pipe as delimiter
...