0

Current flow:

1.BatchItemReader implements ItemReader<List<SingleJsonRowInput>>
2.BatchItemProcessor implements ItemProcessor<List<SingleJsonRowInput>>
3.BatchItemWriter implements ItemWriter<List<String>>

The input is a text file with each row represent a Json file. currently the program runs well with a single file, i would like to implement MultiResourceItemReader but since my reader doesn't imlement this ResourceAwareItemReaderItemStream - it cannot be applied to MultiResourceItemReader. i tried:

1. Implementing ResourceAwareItemReaderItemStream 
2. Changing my reader to be FlatFileItemReader as advised here:

Spring Batch: How to setup a FlatFileItemReader to read a json file? but failed to do so.

Reader:

public class BatchItemReader implements ItemReader<List<SingleJsonRowInput>>{

    private int count = 0;
    private FileManager fileManager;
    private Gson gson = new Gson();

    public List<SingleJsonRowInput> read() {

        return readLine();
    }

    public BatchItemReader(FileManager fileManager) {
        this.fileManager = fileManager;
    }

    private List<SingleJsonRowInput> readLine() {
        List<String> result = fileManager.readTextJsonFile("C:\\Users\\orenl\\Desktop\\small.json");

        List<SingleJsonRowInput> singles = new LinkedList<>();
        SingleJsonRowInput singleJsonRowInput = null;
        for (String line : result) {
            System.out.println("#### Reading line: " + line);
            singleJsonRowInput = gson.fromJson(line, SingleJsonRowInput.class);
            singles.add(singleJsonRowInput);
        }
        if (count > 5) {
            return null;
        }
        count++;
        return singles;

    }

}

MultiResourceItemReader:

@Bean
public MultiResourceItemReader<SingleJsonRowInput> multiResourceItemReader(){
    Resource resources[]=new Resource[]{new FileSystemResource("small.json")};
    MultiResourceItemReader<SingleJsonRowInput>  multiResourceItemReader=new MultiResourceItemReader<>();
    multiResourceItemReader.setResources(resources);
    multiResourceItemReader.setDelegate(new FlatFileItemReader<>());
    return multiResourceItemReader;
}
Oren
  • 407
  • 1
  • 7
  • 19
  • Yes, the `MultiResourceItemReader` requires a delegate `ResourceAwareItemReaderItemStream`. For the second option, you probably failed to use the `FlatFileItemReader` because you want to return a list of items and not a single item per read. – Mahmoud Ben Hassine Nov 27 '18 at 11:55
  • Correct, i also tried to return a single item instead of a list, which approach do you think i should take here ? do you have an example of such a case ( MultiResourceItemReader with readers reading lines that represent Jsons ? – Oren Nov 27 '18 at 12:22
  • I don't have an example, but if each line represents a json object you can configure a `FlatFileItemReader` with a `JsonLineMapper` and use it as a delegate in the `MultiResourceItemReader `. – Mahmoud Ben Hassine Nov 27 '18 at 14:17

0 Answers0