This document shows how to do following:
- Get a csv from a local folder,
- Unmarshal it to a Java pojo object array
- Split the array to each object
- Handle each pojo object by a customized code
Following is the pojo definition with camel csv format annotations
package com.zzyan.domain;
import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
import org.apache.camel.dataformat.bindy.annotation.DataField;
@CsvRecord(separator = ",", skipFirstLine = true)
public class Item {
@DataField(pos=1)
private String transactionType;
@DataField(pos=2)
private String sku;
@DataField(pos=3)
private String itemDescrition;
@DataField(pos=4)
private String price;
/*
here ignores all the getter and setters
*/
@Override
public String toString() {
return "Item{" +
"transactionType='" + transactionType + '\'' +
", sku='" + sku + '\'' +
", itemDescrition='" + itemDescrition + '\'' +
", price='" + price + '\'' +
'}';
}
}
We also define a customized processor, the processor will take in 1 Item object, and do some handling. Here we do nothing but just print it out.
package com.zzyan.processor;
import com.zzyan.domain.Item;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.Exchange;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class ItemProcessor implements org.apache.camel.Processor {
@Override
public void process(Exchange exchange) throws Exception {
Item item = (Item)exchange.getIn().getBody();
System.out.println(item.getItemDescrition());
}
}
Finally we define the Camel Route to link these together.
package com.zzyan.route;
import com.zzyan.domain.Item;
import com.zzyan.processor.ItemProcessor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat;
import org.apache.camel.spi.DataFormat;
import org.springframework.stereotype.Component;
//@Component
public class UnmarshalCsvRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
DataFormat bindy = new BindyCsvDataFormat(Item.class);
from("timer:hello?period=10s")
.log("Timer Invoked and the body is ${body}")
.pollEnrich("file:c:/data/input?noop=true&readLock=none")
.log("body: ${body}")
.unmarshal(bindy)
.log("The unmarshaled object is ${body}")
.split(body())
.log("new Item ${body}")
.process(new ItemProcessor())
.end();
}
}
Put the csv file with the same header to c:/data/input folder, and run the spring boot project, the Route will pickup the file and do the transformation.




