FlatFileParseException在Spring Batch

时间:2016-02-17 03:53:35

标签: java spring spring-boot spring-batch

每行的分隔符不同,可以是“,”或“|”或固定长度。所以我必须逐行解析,为此我实现了CustomLineMapper extends DefaultLineMapper<Domain>。当我运行以下代码时,我得到 org.springframework.batch.item.file.FlatFileParseException


能帮我解决这个问题吗?

作业读files.xml

<bean id="domain" class="com.di.pos.Domain" />
<job id="readMultiFileJob" xmlns="http://www.springframework.org/schema/batch">
    <step id="step1">
        <tasklet>
            <chunk reader="multiResourceReader" writer="flatFileItemWriter"
                commit-interval="1" />
            <!-- <listeners> <listener ref="customItemReaderListener" /> </listeners> -->
        </tasklet>
    </step>
</job>

<bean id="multiResourceReader" class="org.springframework.batch.item.file.MultiResourceItemReader">
    <property name="resources" value="file:csv/inputs/domain-*.csv" />
    <property name="delegate" ref="flatFileItemReader" />
</bean>

<bean id="flatFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="prototype">
    <property name="lineMapper">
    <!--    <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">-->
            <bean class="com.di.pos.CustomLineMapper">

             <property name="lineTokenizer">
                    <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                        <property name="names" value="id,domain" />
                    </bean>
                </property>
                <property name="fieldSetMapper">
                    <bean class="com.di.pos.Domain" />
                </property>
        </bean>
    </property>
</bean>


CustomLineMapper.java

public class CustomLineMapper extends DefaultLineMapper<Domain> {
private LineTokenizer lineTokenizer;
private FieldSetMapper<Domain> fieldSetMapper;

@Override
public Domain mapLine(String line, int lineNumber) throws Exception {

    try {
        DelimitedLineTokenizer delimitedLineTokenizer = new DelimitedLineTokenizer();
        if (line.indexOf("|") == -1) {
            if (line.indexOf(",") == -1) {
                // TODO
            } else {
                delimitedLineTokenizer.setDelimiter(",");
            }
        } else {
            delimitedLineTokenizer.setDelimiter("|");
        }
        setLineTokenizer(delimitedLineTokenizer);
        return this.fieldSetMapper.mapFieldSet(delimitedLineTokenizer.tokenize(line));
    } catch (Exception ex) {
        throw new FlatFileParseException("Parsing error at line: " + lineNumber + ", input=[" + line + "]", ex,
                line, lineNumber);
    }
}

public LineTokenizer getLineTokenizer() {
    return lineTokenizer;
}

public void setLineTokenizer(LineTokenizer lineTokenizer) {
    super.setLineTokenizer(lineTokenizer);
    System.out.println(lineTokenizer == null);
    this.lineTokenizer = lineTokenizer;
}

public FieldSetMapper<Domain> getFieldSetMapper() {
    return fieldSetMapper;
}

@Override
public void setFieldSetMapper(FieldSetMapper<Domain> fieldSetMapper) {
    super.setFieldSetMapper(fieldSetMapper);
}}


Domain.java

public class Domain implements FieldSetMapper<Domain>{
    int id;
    String domain;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getDomain() {
        return domain;
    }
    public void setDomain(String domain) {
        this.domain = domain;
    }
    @Override
    public Domain mapFieldSet(FieldSet arg0) throws BindException {
        Domain  domain = new Domain();
        domain.setDomain(arg0.readString(1));
        domain.setId(arg0.readInt(0));
        return domain;
    }
}


异常

org.springframework.batch.item.file.FlatFileParseException: Parsing error at line: 1 in resource=[file [/Users/rr250393/Documents/Apps/SpringBatchExample-Multiple-Files-Reader/csv/inputs/domain-1-3-2013.csv]], input=[1,facebook.com]
at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:183)
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:83)
at org.springframework.batch.item.file.MultiResourceItemReader.readFromDelegate(MultiResourceItemReader.java:140)
at org.springframework.batch.item.file.MultiResourceItemReader.readNextItem(MultiResourceItemReader.java:119)
at org.springframework.batch.item.file.MultiResourceItemReader.read(MultiResourceItemReader.java:108)
at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:91)
at org.springframework.batch.core.step.item.SimpleChunkProvider.read(SimpleChunkProvider.java:155)
at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:114)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:368)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
at org.springframework.batch.core.step.item.SimpleChunkProvider.provide(SimpleChunkProvider.java:108)
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:69)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:395)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:131)
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:267)
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:77)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:368)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:253)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:195)
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:137)
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:60)
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:152)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:131)
    at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:135)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:301)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:134)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:127)
    at com.di.pos.App.run(App.java:29)
    at com.di.pos.App.main(App.java:15)
Caused by: org.springframework.batch.item.file.FlatFileParseException: Parsing error at line: 1, input=[1,facebook.com]
    at com.di.pos.CustomLineMapper.mapLine(CustomLineMapper.java:31)
    at com.di.pos.CustomLineMapper.mapLine(CustomLineMapper.java:1)
    at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:180)
    ... 33 more
Caused by: java.lang.NullPointerException
at com.di.pos.CustomLineMapper.mapLine(CustomLineMapper.java:30)
... 35 more

0 个答案:

没有答案