Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the early version of the new website
https://camel.apache.org/staging/
We would very much like to receive any feedback on the new site, please join the discussion on the Camel user mailing list.
CSVThe 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
Available options as of Camel 2.15
Marshalling a Map to CSVThe component allows you to marshal a Java Map (or any other message type that can be converted in a Map) into a CSV payload.
Unmarshalling a CSV message into a Java ListUnmarshalling 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. 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 We can now use the CSV component to unmarshal this file: from("file:src/test/resources/?fileName=daltons.csv&noop=true") .unmarshal().csv() .to("mock:daltons"); The resulting message will contain a 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))); } Marshalling a List<Map> to CSVAvailable as of Camel 2.1 If you have multiple rows of data you want to be marshalled into CSV format you can now store the message payload as a File Poller of CSV, then unmarshalingGiven 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&consumer.delay=10000" /> <unmarshal><csv /></unmarshal> <to uri="bean:myCsvHandler?method=doHandleCsvData" /> </route> Marshaling with a pipe as delimiter
Using autogenColumns, configRef and strategyRef attributes inside XML DSLAvailable 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 <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 option while unmarshalingAvailable 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: <route> <from uri="direct:start" /> <unmarshal> <csv skipFirstLine="true" /> </unmarshal> <to uri="bean:myCsvHandler?method=doHandleCsv" /> </route> Or the Java DSL: CsvDataFormat csv = new CsvDataFormat(); csv.setSkipFirstLine(true); from("direct:start") .unmarshal(csv) .to("bean:myCsvHandler?method=doHandleCsv"); Unmarshaling with a pipe as delimiterUsing the Spring/XML DSL: <route> <from uri="direct:start" /> <unmarshal> <csv delimiter="|" /> </unmarshal> <to uri="bean:myCsvHandler?method=doHandleCsv" /> </route> Or the Java DSL: CsvDataFormat csv = new CsvDataFormat(); CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY; strategy.setDelimiter('|'); csv.setStrategy(strategy); from("direct:start") .unmarshal(csv) .to("bean:myCsvHandler?method=doHandleCsv"); CsvDataFormat csv = new CsvDataFormat(); csv.setDelimiter("|"); from("direct:start") .unmarshal(csv) .to("bean:myCsvHandler?method=doHandleCsv"); CsvDataFormat csv = new CsvDataFormat(); CSVConfig csvConfig = new CSVConfig(); csvConfig.setDelimiter(";"); csv.setConfig(csvConfig); from("direct:start") .unmarshal(csv) .to("bean:myCsvHandler?method=doHandleCsv"); Issue in CSVConfig It looks like that CSVConfig csvConfig = new CSVConfig(); csvConfig.setDelimiter(';'); doesn't work. You have to set the delimiter as a String! DependenciesTo use CSV in your Camel routes you need to add a dependency on camel-csv, which implements this data format. 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). <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-csv</artifactId> <version>x.x.x</version> </dependency> |