0

I would like to do a CSV normalization with the help of the Super-CSV-Dozer API.

I have a CSV file in the form of:

ObjectA_ID,ObjectA_Name,ObjectB_ID,ObjectB_Name
1,A_Name1,10,B_Name1
1,A_Name1,11,B_Name2
1,A_Name1,12,B_Name3
...
2,A_Name2,20,B_Name11
2,A_Name2,21,B_Name12
2,A_Name2,22,B_Name13
...

and after reading this file (for instance via CsvDozerBeanReader) I want to have a normalized representation of this data in the form of beans like:

Class ObjectA {
    private long id;
    private String name;
    private Collection objectBs;
    ...
}

Class ObjectB {
    private long id;
    private String name;
    ...
}

Is there a built-in possibility within Super-CSV-Dozer?

1 Answers1

1

If each row represented a different ObjectA then I'd recommend this answer (I initially thought your question was similar to this).

After reading your comments though, that's not going to help. The ObjectA columns are repeated for every ObjectB belonging to it.

Super CSV can't combine these for you, so you'll need to store each ObjectA in a map, e.g.

  • For each a row read as an ObjectA bean (using CsvDozerBeanMapper)

    • Check if there's an ObjectA in the Map for the id

      • If yes, then update it by adding the current ObjectB to it

      • If no, then just add it to the Map

  • Return the Map's values (the normalized ObjectA's).

The downside to having to normalize is that you have to store every ObjectA in memory.

Community
  • 1
  • 1
James Bassett
  • 9,458
  • 4
  • 35
  • 68
  • Thank you for the reply. I red your [answer](http://stackoverflow.com/a/12504722/1068649) but as far as I understand it it does not solve my problem. – user2463924 Jun 10 '13 at 09:18
  • My question is for normalization of CSV-rows, not columns. So the question is, whether the Super-CSV/Dozer functionality can help out of the box to eliminate duplicates and do a "row-based" normalization of one "super-table" (the CVS-File) to several normalizes tables (the resulting Beans). In you answer and in the samples the collection of dependent Beans (collection of answers) is originally modeled as set of columns within the csv file. But in my case there are several rows in the csv file, where for instance the data of ObjectA stays the same and the data of assigned ObjectB changes. – user2463924 Jun 10 '13 at 09:30
  • Thank you for the update! Thats just what I wanted to know: whether I have to do it on my own or with some help from supercsv/dozer. – user2463924 Jun 12 '13 at 06:15