I'm using univocity parsers with BeanListProcessor to map to my Java Bean which is good provided the column names don't change. However, I also need to parse a csv file which comes with different columns for each type of user. I have the mappings stored for each user to my standard column names, but how do I parse it dynamically to my pojo without having to modify the file. I couldnt use HeaderTransformer since its still not dynamic. Please let me know if you need additional info. The version I'm using is 2.6.3
For example:
BeanListProcessor<MyPojo> rowProcessor = new BeanListProcessor<MyPojo>(MyPojo.class);
CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.setProcessor(rowProcessor);
parserSettings.setHeaderExtractionEnabled(true);
CsvParser parser = new CsvParser(parserSettings);
parser.parse(getReader(file));
List<MyPojo> pojos= rowProcessor.getBeans();
pojos.forEach(v -> System.out.println(v.toString()));
public class MyPojo{
@Trim
@Parsed
private String myColumn1;
@Trim
@Parsed
private String myColumn2;
....
User1 file:
user1Column,user2Column\n
data1,data2
User 1 mappings
user1Column -> myColumn1
user2Column -> myColumn2
As the header names are not relevant you could map your attributes by position:
public class MyPojo{
@Trim
@Parsed(index = 0)
private String myColumn1;
@Trim
@Parsed(index = 1)
private String myColumn2;
}
This way no matter what headers you get, the attributes will be populated accordingly. Use selectIndexes
to alter the order of the columns extracted from the input so they match with the positions in your class.
Alternatively, if the headers can appear in any random sequence, you can keep the code you originally posted and add a call to the setHeaders
method:
If your input is:
user1Column,user2Column\n data1,data2
Then use:
parserSettings.setHeaders("myColumn1", "myColumn2");
If your input is:
user2Column,user1Column\n data1,data2
Then go with:
parserSettings.setHeaders("myColumn2", "myColumn1");
Notice that here the names match with the attributes in your bean. The headers are still extracted from the input but will be ignored.
Hope this helps