Niko
Niko

Reputation: 4448

Spring FlatFileReader Jagged CSV file

I'm reading data via spring batch and I'm going to dump it into a database table.

My csv file of musical facts is formatted like this:

question; valid answer; potentially another valid answer; unlikely, but another;

Where all rows have a question and at least one valid answer, but there can be more. The simple way to hold this data is to in the data in a pojo is with one field for a String and another for a List<String>.

Below is a simple line mapper to read a CSV file, but I don't know how to make the necessary changes to accommodate a jagged CSV file in this manner.

  @Bean
  public LineMapper<MusicalFactoid> musicalFactoidLineMapper() {
    DefaultLineMapper<MusicalFactoid> musicalFactoidDefaultLineMapper = new DefaultLineMapper<>();
    musicalFactoidDefaultLineMapper.setLineTokenizer(new DelimitedLineTokenizer() {{
      setDelimiter(";");
      setNames(new String[]{"question", "answer"}); // <- this will not work!
    }});
    musicalFactoidDefaultLineMapper.setFieldSetMapper(new BeanWrapperFieldSetMapper<MusicalFactoid>() {{
      setTargetType(MusicalFactoid.class);
    }});

    return musicalFactoidDefaultLineMapper;
  }

What do I need to do?

Upvotes: 0

Views: 363

Answers (1)

Hansjoerg Wingeier
Hansjoerg Wingeier

Reputation: 4444

Write your own Line Mapper. As far as I see, you don't have any complex logic.

Something like this:

public MyLineMapper implements LineMapper<MusicalFactoid> {
  public MusicalFactoid mapLine(String line, int lineNumber) {
     MusicalFactoid dto = new MusicalFactoid();
     String[] splitted = line.split(";");

     dto.setQuestion(splitted[0]); 
     for (int idx = 1; idx < splitted.length; idx++) {
        dto.addAnswer(splitted[idx]); 
     }

      return dto;
   }
}

Upvotes: 1

Related Questions