0

I'm fairly new to Apache Camel.

I've been writing routes that process a csv file using the BindyCsvformat class. So far, the CSV files I'm processing are fairly straight-forward, in the sense that they have a fixed number of records. So using the @DataField annotation to define positions of the columns is not a problem.

However I've come across a CSV file, where number of columns are dynamic. This CSV file only contains 1 row.

The column numbers are dynamic based on the value of a column called 'number of periods'.

If the value in the 'number of periods' column is '5', then there are 5 extra columns added to the file.

Here is an example of the headings in this particular CSV file:

sample-csv.txt

row_id, number_of_periods, result_period, result_period, result_period 0,3,0.00,10.00,10.00.

As you can see from the sample data above, because the 'number_of_periods' column shows '3', there are three 'result_period' columns. If the 'number_of_periods' field was 5, then there would be 5 'result_period' columns.

Can anyone assist (or point me to any references) where BindyCsvFormat (via Camel) can handle this dynamic column mapping? Unless I'm mistaken, I think the @DataField annotation can only be applied to fixed fields (assuming the total number of columns is known before hand.

Thanks in advance.

rm12345
  • 1,089
  • 3
  • 18
  • 32
  • 1
    What does the target bean(s) look like? If you're attempting to create some dynamic object graph from this csv based upon some business rules, perhaps Camel's CSV component might be a better option, then have a processor create what you need from the list of lists of strings. – Gerry Mantha Oct 12 '17 at 15:20
  • Gerry, yeah that is also a good idea the CSV component just generates a List or Map structure and would support dynamic columns. Then if you really need to map to a POJO you can do that afterwards yourself from a processor/bean etc. – Claus Ibsen Oct 13 '17 at 06:40

1 Answers1

0

Yes its for fixed fields, however there is an option to let the last field overflow and just grab the remainder of the line.

In your use-case you are better not using Bindy and just write your own little bit of CSV parser code. It will not be hard to do that yourself. You can then still use your CSV parser in Apache Camel, and do a message transformation for each line with your parser. The parser can just be implemented as a java method.

Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65