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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top