I have the following requirements:
- For each record in a CSV file, read the value contained in three of its fields named
startValue
,endValue
andchange
. - Calculate the
change
as (endValue-startValue)/startValue - Compare the calculated
change
to thechange
provided in the CSV file for the record. Log a warning if different to the same number of decimal places. Continue to step 4 in any case. - Replace the
change
value in the CSV file with thechange
calculated in step 3. Leave the CSV file unaltered otherwise. Blank lines, line terminators, headers and all other field values should remain unchanged.
I'm aware of two approaches.
Approach 1
from("{{route.getCsvFile}}")
.unmarshal(new BindyCsvDataFormat(MyValueChange.class))`
.process(myProcessor) // calculates `change` and replaces CSV `change` value
.to("bean-validator:changeValidator") // does step 3
.marshal(new BindyCsvDataFormat(MyValueChange.class))
.to("{{route.putCsvFile}}");
The problem: I do not want to have to unmarshall all CSV fields into a Java object. I only need the three fields to work with change
. The CSV file contains 75 other fields.
Approach 2
from("{{route.getCsvFile}}")
.unmarshal(new CsvDataFormat())
.process(myProcessor)
.marshal(new CsvDataFormat())
.to("{{route.putCsvFile}}");
The problem: myProcessor
must now contain code to parse through a List
of String
s and must contain code to validate the bean (instead of using Camel's call to bean validation).
Is there a way to use bindy for just three fields and then use that bean to replace only the change
values within the original CSV data?