Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

spring batch - SpringBatch - Get Line Number on FieldSetMapper

I need to get the line number into the FieldSet Mapper. How can I do this?

I want to use the lineNumber as a field of my resulting object produced by the Mapper.

<beans:bean id="fileReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
    <beans:property name="resource" 
        value="file:${batch.source}"/>
    <beans:property name="lineMapper">
        <beans:bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
            <beans:property name="lineTokenizer">
                <beans:bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                    <beans:property name="delimiter" value="${batch.delimiter}"/>
                    <beans:property name="names" value="${batch.columns}"/>
                </beans:bean>
            </beans:property>

            <beans:property name="fieldSetMapper">
                <beans:bean class="my.batch.mapper.ResourceFieldSetMapper"/>
            </beans:property>
        </beans:bean>
    </beans:property>
</beans:bean>

Thank You.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I realized that I can get the lineNumber value into MyObject by overriding the DefaultLineMapper with my own LineMapper in this way:

import org.springframework.batch.item.file.FlatFileParseException;
import org.springframework.batch.item.file.LineMapper;
import org.springframework.batch.item.file.transform.LineTokenizer;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

import my.model.MyObject;

public class MyLineMapper<T> implements LineMapper<MyObject>, InitializingBean {

    private LineTokenizer tokenizer;

    private ResourceFieldSetMapper fieldSetMapper;

    public MyObject mapLine(String line, int lineNumber) throws Exception {
        try{
            MyObject r = fieldSetMapper.mapFieldSet(tokenizer.tokenize(line));
            // this is the modification
            r.setLineNumber(lineNumber);
            return r;
        }
        catch(Exception ex){
            throw new FlatFileParseException("Parsing error at line: " + lineNumber + 
                    ", input=[" + line + "]", ex, line, lineNumber); 
        }
    }

    public void setLineTokenizer(LineTokenizer tokenizer) {
        this.tokenizer = tokenizer;
    }

    public void setFieldSetMapper(ResourceFieldSetMapper fieldSetMapper) {
        this.fieldSetMapper = fieldSetMapper;
    }

    public void afterPropertiesSet() {
        Assert.notNull(tokenizer, "The LineTokenizer must be set");
        Assert.notNull(fieldSetMapper, "The FieldSetMapper must be set");
    }

}

Thanks for your help! I hope this works for someone!

Blessings!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...