I have generated CSV file with camel Bindy using @csvRecord, but unable generate footer for this csv to show total count of records in file. I could able to generate with @FixedLenght record but not with @csvRecord. Please provide any suggestions
Asked
Active
Viewed 1,688 times
1 Answers
0
You cannot generate a footer in CSV in a similar way as in FixedLength using camel-bindy.
Depending on the data, you could maybe add an additional record with the information about total count etc.
EDIT:
Example CSV file:
FIELD1;FIELD2
test1;4
test3;6
CsvFilePojo class:
import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
import org.apache.camel.dataformat.bindy.annotation.DataField;
@CsvRecord(separator = ";", skipFirstLine = true)
public class CsvFilePojo {
@DataField(pos = 1)
private String field1;
@DataField(pos = 2)
private Integer field2;
public CsvFilePojo(String field1, Integer field2) {
this.field1 = field1;
this.field2 = field2;
}
public CsvFilePojo() {
}
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public Integer getField2() {
return field2;
}
public void setField2(Integer field2) {
this.field2 = field2;
}
@Override
public String toString() {
return String.format("field1: %s; field2: %s", field1, field2);
}
}
CountTotalProcessor class:
import java.util.List;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
public class CountTotalProcessor implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
List<CsvFilePojo> csvlist = exchange.getIn().getBody(List.class);
// count total
Integer total = 0;
for (CsvFilePojo c : csvlist) {
total += c.getField2();
}
// add total as last record in csv data
csvlist.add(new CsvFilePojo("total", total));
exchange.getIn().setBody(csvlist);
}
}
MyRouteBuilder class:
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat;
public class MyRouteBuilder extends RouteBuilder {
public void configure() {
//@formatter:off
from("file:csvFiles")
.log("gotFile ${file:name}")
.to("direct:processFile")
.to("file:processedCsvFiles");
from("direct:processFile")
.log("processing CSV file")
.unmarshal(new BindyCsvDataFormat(CsvFilePojo.class))
.process(new CountTotalProcessor())
.split(simple("${body}"))
.log("Body ${body}")
.end()
;
}
}
With that example csv file this route would produce a CSV file like this:
FIELD1;FIELD2
test1;4
test3;6
total;10

StefanR
- 676
- 10
- 20
-
Hi StefanR, thanks for the reply. can you please provide an example to handle this requirement using Apache Camel and Bindy component. – redyabkees Sep 25 '18 at 13:50
-
I added a simple example for you. – StefanR Sep 26 '18 at 04:49