Line number in Batch Writer for Spring Batch

952 views
Skip to first unread message

Niyant

unread,
Oct 22, 2013, 4:39:57 PM10/22/13
to bea...@googlegroups.com
Hi Kevin, 

We have a requirement to load data along with the row number where the record was found in the original flat-file. 

It's pretty easy to get this info in the SkipListener with InvalidRecException.getRecordContext().getLineNumber(). 

However, I didn't find a way to get this in the BatchWriter. 

Do you have any thoughts on this? This is rather important and I would hate to employ some convoluted mechanism to get this information. 

Thanks, 
Niyant

Kevin

unread,
Oct 23, 2013, 10:16:14 PM10/23/13
to bea...@googlegroups.com
Hi Niyant,

I'm not sure what you are referring to with "SkipListener" or "BatchWriter".  The line number is exposed via beanReader.getLineNumber().

Thanks,
Kevin

Niyant Dalal

unread,
Oct 28, 2013, 10:44:57 AM10/28/13
to bea...@googlegroups.com
Hi Kevin, 

I was using some terms loosely in my pervious post so things may not have been very clear. I'll make this post more specific by including code snippets.

I'm using Spring Batch and so I'm the reading is abstracted into org.beanio.spring.BeanIOFlatFileItemReader: 

<bean id="genericBatchItemReader" class="org.beanio.spring.BeanIOFlatFileItemReader"
scope="step">
<property name="streamMapping" value="classpath:beanio-mapping.xml" />
<property name="streamName" value="myStream" />
<property name="resource"
value="/home/tmp/files/myfile.txt" />
</bean>

BeanIOFlatFileItemReader.doRead() uses BeanReader, which has the beanReader.getLineNumber() like you mentioned in your post. However, since this operation is abstracted out for me, I don't have a way to use line number. 

I was considering extending BeanIOFlatFileItemReader, and overriding doRead(). That way I could still use the BeanIOFlatFileItemReader's doRead and add the line number after a line has been parsed. However, the instance of BeanReader seems to be private with no accessor methods. So, this may not work unless I implement my own version of BeanIOFlatFileItemReader. 

Another alternative -- (seems like a stretch though), is there anyway to map line number via the mapping configuration? 


As an aside, the one place I've had easy access to line-number information is when an error occurs and the control passes to a SkipListener: 

<bean id="genericItemSkipListener" class="com.mypackage.GenericBatchItemSkipListener"
scope="step">
</bean>

public class GenericBatchItemSkipListener implements
SkipListener<MyBean, Throwable>, StepExecutionListener {

// ... other code


@Override
public void onSkipInRead(Throwable t) {
// ... other code

BeanReaderException beanReaderEx = (BeanReaderException) t;
int lineNum = beanReaderEx.getRecordContext().getLineNumber();
   }

// ... other code

}

Thanks, 
Niyant




--
You received this message because you are subscribed to a topic in the Google Groups "beanio-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/beanio/rnN5Rc7gZl4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to beanio+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Kevin

unread,
Nov 2, 2013, 5:17:18 PM11/2/13
to bea...@googlegroups.com
Hi Nyant,

Release 2.1.0.M1 exposes the line number on BeanIOFlatFileItemReader as well as a protected method for the BeanReader.  Thats the best I can do for now- there is no way include it in a mapping file.

Thanks,
Kevin
Reply all
Reply to author
Forward
0 new messages