ewom2468
ewom2468

Reputation: 841

Reading Multiple Json Files And Converting To POJO Springboot batch

I have a folder with thousands of text files with JSON content that I need to read, convert into a POJO and then save into a MySQL database. I intend to use a Spring Batch application.

Here is the issue so far, the research I have done only shows reading multiple CSV files or XML files and no JSON data. Specifically, I need to convert this method for parsing a CSV file into a JSON parser.

@Bean
public FlatFileItemReader<Person> reader() {
    FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
    reader.setLineMapper(new DefaultLineMapper<Person>() {{
        setLineTokenizer(new DelimitedLineTokenizer() {{
            setNames(new String[] {"firstname", "lastname", "email", "age"});
        }});
        setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
            setTargetType(Person.class);
        }});
    }});
    return reader;
}

This code parses a JSON file:

JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("C:\\path\\sample.json"));

The method might be something like this

@Bean
Public FileReader<Person> reader() {
    FileReader<Person> reader = new FileReader<Person>();

    /**** need help on what to do here ****/

    return reader;
}

Also seeing that I am reading all the files in a directory, I am passing the value of that directory in this format

@Value(value="C:\\path\\*.json")
private Resource[] resources;

So I need help on how to use this value (directory for all files) instead of what I showed earlier (single file location)

Object obj = parser.parse(new FileReader("C:\\path\\sample.json"));

Upvotes: 0

Views: 2556

Answers (2)

Mahmoud Ben Hassine
Mahmoud Ben Hassine

Reputation: 31620

You can use the MultiResourceItemReader with a JsonItemReader as delegate. Here is a quick example:

@Bean
public JsonItemReader<Person> jsonItemReader(Resource[] resources) {
   JsonItemReader<Person> delegate = new JsonItemReaderBuilder<Person>()
      .jsonObjectReader(new JacksonJsonObjectReader<>(Person.class))
      .name("personItemReader")
      .build();

   MultiResourceItemReader<Person> reader = new MultiResourceItemReader<Person>();
   reader.setDelegate(delegate);
   reader.setResources(resources);
   return reader;
}

You can find more details about the JsonItemReader in the reference documentation.

Hope this helps.

Upvotes: 2

Chris
Chris

Reputation: 389

https://docs.spring.io/spring-batch/4.0.x/reference/html/readersAndWriters.html#multiFileInput - I only found this after I found this answer

Upvotes: 0

Related Questions