Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Jackson CSV: Empty Header field

Tags:

java

csv

jackson

I'm trying to map a large csv file to a class with jackson csv. That csv file is not normalized at all which leads to several problems.

To give you an impression, I created a representive file:

,B,C,D,E,F�F,G,H H,I (I)
0,AAA,20. Jan.,10,A,5,AA: AAAA,1,A�AAA
1,BBB,05-Dez-14,12,BBB,1,BB: BBBB,32,BBB
2,CCCC,28. Jun.,80,CCCC,2,CCCC: CCCC,1,CCC / CCC

The first row represents the header and the first column the ids. The Jackson CsvMapper fails on mapping the first column because of it's empty name.

CsvMapper mapper = new CsvMapper();
CsvSchema bootstrapSchema = CsvSchema.builder()
                .setColumnSeparator(',')
                .setUseHeader(true)
                .setStrictHeaders(true)
                .setSkipFirstDataRow(true)
                .addColumn("", CsvSchema.ColumnType.NUMBER)
                .addColumn("B", CsvSchema.ColumnType.STRING)
                .addColumn("C", CsvSchema.ColumnType.STRING)
                .addColumn("D", CsvSchema.ColumnType.NUMBER)
                .addColumn("E", CsvSchema.ColumnType.STRING)
                .addColumn("F�F", CsvSchema.ColumnType.NUMBER)
                .addColumn("G", CsvSchema.ColumnType.STRING)
                .addColumn("H H", CsvSchema.ColumnType.NUMBER)
                .addColumn("I (I)", CsvSchema.ColumnType.STRING)
                .build();

        //File file = new ClassPathResource(fileName).getFile();
        MappingIterator<POJO> readValues =
                mapper.reader(POJO.class).with(bootstrapSchema).readValues(file);

        while (readValues.hasNext()){
            logger.info(mapper.writeValueAsString(readValues.nextValue()));
        }

And the POJO Class:

@JsonPropertyOrder({"","B","C","D","E","F�F", "G","H H","I (I)"})
public class POJO {

@JsonProperty
private Integer id;

@JsonProperty("B")
private String b;

@JsonProperty("C")
private String c;

@JsonProperty("D")
private Integer d;

@JsonProperty("E")
private String e;

@JsonProperty("F�F")
private Integer f;

@JsonProperty("G")
private String g;

@JsonProperty("HH")
private Integer h;

@JsonProperty("I (I)")
private String i;
}

So, how to map this mess correctly?

Thanks in advance

like image 431
Florian Bieck Avatar asked Nov 08 '22 02:11

Florian Bieck


1 Answers

Try changing your CsvMapper like this.

List<Object> objList = new CsvMapper()
                       .enable(CsvParser.Feature.SKIP_EMPTY_LINES)
                       .enable(CsvParser.Feature.TRIM_SPACES)
                       .enable(CsvParser.Feature.WRAP_AS_ARRAY)
                       .enable(CsvParser.Feature.INSERT_NULLS_FOR_MISSING_COLUMNS)
                       .readerFor(Map.class)
                       .with(CsvSchema.emptySchema().withHeader())
                       .readValues(file)
                       .readAll();
like image 84
Phenomenal One Avatar answered Nov 15 '22 05:11

Phenomenal One