last day (15 days later) » 

2:48 PM
0
A: How to use FlatFileItemReader in spring batch for items separated by space (multiple records on single line)?

Braj You can use your custom LineMapper to implement the logic as per your requirement to separate the items from each line. Here is the sample code that will return the list of Ids either separated by comma or space or both in each line. import java.util.ArrayList; import java.util.List; impor...

 
Thanks, but how it would work in case of restart ? It will start every time from beginning ?
When one line is passed, LineMapper is supposed to return one record ( domain object) but in my case, in one line I have multiple records separated by command, I dont have fields (or I can say I have only one field).
Is there any way to configure LINE_SEPARATOR for flatfileitemreader so that it considers each comma separated item as each line ?
 
FlatFileItemReader uses RecordSeparatorPolicy. Try with your custom RecordSeparatorPolicy.
My CustomLineMapper is returning multiple records formed by a line. Isn't it?
LINE_SEPARATOR is a property of FlatFileItemWriter.
Look at the source code of different impl of RecordSeparatorPolicy. you will get an idea to impl of your custom RecordSeparatorPolicy.
 
It works but to myItemWriter, spring batch doesn't send items separately. It sends string of command separated items (ids). It should ideally send List of number of items equal to commit-interval.
 
Great. Please can you share what have you tell now.
 
Thanks a lot for your help. Currently my item writer is being called only once and if I print inside it it displays all ids separated by comma
 
2:51 PM
Good. :)
 
This is my job configuration
<beans:bean id="exchangeFileItemReader"
class="org.springframework.batch.item.file.FlatFileItemReader"
p:resource="file:/tmp/eu-#{jobParameters[date]}/exchanges.txt"
p:lineMapper-ref="exchangeLineMapper" scope="step"/>
This is my item reader which I implemented the same way you mentioned and it works perfectly. It prepares a list of items. But if I print in my item writer, it is called only once and it displays all ids separated by comma
 
Is there any issue if its called once but you got all the IDs?
 
yeah, ideally it should send list of items equal to commit interval . I want to do some processing for each id. Later on, I am planning to process those in parallel and remotely on different servers, may be using rabbitmq
Ideally it should send separate items and not the whole string, so i think I am doing something wrong
 
OK let me try it. I'll come back with some more inputs.
 
Thanks a lot. You have been very helpful :)
 

last day (15 days later) »