Search code examples
javaparsingdata-binding

How to get MappingIterator to not include empty rows?


Thanks for taking a look at this.

I'm parsing csv files and ran into an issue where if the file contains a blank line at the end of it, it breaks things. The easy work-around is to just delete the blank row at the end of the file but I would much rather know how to configure this to ignore the blank row at the end if it is present.

I've transcribed the code from my other system here to show how I'm creating the MappingIterator

For a valid row of data it runs fine but I'm running into an issue where if a csv file contains a blank line at the end of the file I end up with a null pointer exception.

The mappingIterator will contain for a blank line a key of col_a with a null value for it and nothing else.

How am I to get this to ignore the blank line?

Here is a code sample

private MappingIterator getMappingIteratorCsv(InputStream inputStream) {
    logger.info("Processing ");

    CsvMapper mapper = new CsvMapper();
    mapper.enable(CsvParser.Feature.SKIP_EMPTY_LINES);
    mapper.enable(CsvParser.Feature.WRAP_AS_ARRAY);

    CsvSchema.Builder schemaBuilder = CsvSchema.builder().setColumnSeparator(',');
    schemaBuilder.addColumn("col_a");
    schemaBuilder.addColumn("col_b");
    schemaBuilder.addColumn("col_c");

    CsvSchema schema = schemaBuilder.build();

    MappingIterator<Map<String, String>> mappingIterator;

    mappingIterator = mapper.readerFor(Map.class).with(schema).readValues(inputStream);

    return mappingIterator;
}

Solution

  • The feature you want to configure seems to be proposed, but not implemented yet:

    https://github.com/FasterXML/jackson-dataformats-text/issues/15

    So I think you'll have to go with the option to clean the input first, if you don't want to go ahead and contribute a fix to the project :-)

    Cheers, Anders