I have a batch that read from data base using JdbcPagingItemReader, process each record from the database in a java class then write it to a file using FlatFileItemWriter. It appends also a header and footer for this file using FlatFileFooterCallback and FlatFileHeaderCallback
The Job work well and it gives as output, a single JSON file in this format :
{"informations":[
{
"name" : "xxx",
"adress" : "xxx"
//a very complex json object (1000 lines)
},
{
"name" : "xxx",
"adress" : "xxx"
//a very complex json object (1000 lines )
},
// Many objects
]}
Kindly note that the header is just this :
{"informations":[
And the footer is just
]}
now the file is too big and I want to split it to multiple file using MultiResourceItemWriter
Each file has at maximum 1000 row read from the database.
So I configured the step and I used the first FlatFileItemWriter above and this work well.
As result, I have many files that have 1000 records from a data base but without the header and the footer
{"informations":[ and ]}
All files generated by MultiResourceItemWriter have this format :
{
"name" : "xxx",
"adress" : "xxx"
a very complex json object (1000 lines)
},
{
"name" : "xxx",
"adress" : "xxx"
a very complex json object (1000 lines )
},
{
// many objects
}
How could I add the header and the footer for all files created by MultiResourceItemWriter When writing.
I found an answer that say that we can't combine MultiResourceItemWriter with FlatFileItemWriter that have FlatFileFooterCallback and FlatFileHeaderCallback with spring batch version < 2.1 .
Stream closed exception when combining MultiResourceItemWriter and FlatFileItemWriter with footer callback and stack overflow post stack-overflow-post-about-my-problem
I have this exception java.lang.IllegalStateException: JsonWriter is closed when trying to wrap FlatFileItemWriter with FlatFileHeaderCallback in my final MultiResourceItemWriter writer.
So is there a way to specify a header and footer for all file created by MultiResourceItemWriter when writing in a new file (splitting to multiple file) ? Is there a way to define a template of the file in which the MultiResourceItemWriter write in?
If no, could you please guide me to do so?
I would like to have in final, all file that created by MultiResourceItemWriter have at the beginning the
{"informations":[ and at the end ]} , all the file content is wrapped in the json array informations.