org.springframework.batch.item.file.FlatFileParseException:

org.springframework.batch.item.file.FlatFileParseException:

错误

Parsing error at line: 1 in resource=[file [C:\Users\satish_pahuja\Desktop\BO_Office_Acc_Ext.txt]], input=[BOOfficeAccExt,B0001,00006746464646,S0001,BA0001,A,ABC,XYZ,UID,SatishPahuja,SatishPahuja,090909,151512]
    at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:183)
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:83)
    at gov.in.dop.rsi.mgnregs.batch.FileBatchReader.read(FileBatchReader.java:38)
    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.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.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.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 gov.in.dop.rsi.mgnregs.batch.BatchProcessExtract.main(BatchProcessExtract.java:29)
Caused by: java.lang.NullPointerException
    at org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper.getBean(BeanWrapperFieldSetMapper.java:230)
    at org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper.mapFieldSet(BeanWrapperFieldSetMapper.java:185)
    at org.springframework.batch.item.file.mapping.DefaultLineMapper.mapLine(DefaultLineMapper.java:43)
    at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:180)
    ... 30 more

Sep 15, 2015 3:04:54 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run
INFO: Job: [FlowJob: [name=batchExtracts]] completed with the following parameters: [{}] and the following status: [FAILED]
Exit Status : FAILED
Done

工作-report.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:batch="http://www.springframework.org/schema/batch" xmlns:task="http://www.springframework.org/schema/task"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:orcl="http://www.springframework.org/schema/data/orcl"
    xsi:schemaLocation="http://www.springframework.org/schema/batch
        http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
        http://www.springframework.org/schema/beans

               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/data/orcl
       http://www.springframework.org/schema/data/orcl/spring-data-orcl-1.0.xsd">

    <batch:job id="batchExtracts">
        <batch:step id="step1">
            <batch:tasklet transaction-manager="transactionManager">
                <batch:chunk reader="cvsFileItemReader" writer="csvFileItemWriter"
                    processor="csvFileItemProcessor" commit-interval="20" />
            </batch:tasklet>
        </batch:step>
    </batch:job>

    <bean id="cvsFileItemReader" class="gov.in.dop.rsi.mgnregs.batch.FileBatchReader">
    </bean>

    <bean id="csvFileItemProcessor" class="gov.in.dop.rsi.mgnregs.batch.FileBatchProcessor">
    </bean>

    <bean id="csvFileItemWriter" class="gov.in.dop.rsi.mgnregs.batch.FileBatchWriter">
    </bean>

</beans>

context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

    <!-- stored job-meta in database -->
    <bean id="jobRepository"
        class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
        <property name="transactionManager" ref="transactionManager" />
    </bean>

    <!-- stored job-meta in memory -->

    <bean id="jobLauncher"
        class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository" />
    </bean>

<bean id="transactionManager"
        class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

</beans>

FileBatchReader.java

package gov.in.dop.rsi.mgnregs.batch;

import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

public class FileBatchReader<BoOfficeAccExtract> implements ItemReader {

    public BoOfficeAccExtract read() throws Exception, UnexpectedInputException,
    ParseException, NonTransientResourceException {

        System.out.println("Inside read method-0-");
        FlatFileItemReader<BoOfficeAccExtract>  reader =new FlatFileItemReader<BoOfficeAccExtract>();
        Resource res=new FileSystemResource("C:\Users\satish_pahuja\Desktop\BO_Office_Acc_Ext.txt");
        reader.setResource(res);
        reader.setLineMapper(new DefaultLineMapper<BoOfficeAccExtract>(){{
            setLineTokenizer(new DelimitedLineTokenizer(){{
                setNames(new String[] {"extFileName","boId","officeAccountNo","solId" ,"bankId","ind","freeField1","freeField2","freeField3","modifiedBy","createdBy","modifiedDate","createdDate"});
            }});

            setFieldSetMapper(new BeanWrapperFieldSetMapper<BoOfficeAccExtract>() {{
                setPrototypeBeanName("BoOfficeAccExtract");
            }});
        }});


        reader.open(new ExecutionContext());

        BoOfficeAccExtract boOfficAccExtract=reader.read();

        return boOfficAccExtract;
    }
}

**BO_Office_Acc_Ext.txt**
BOOfficeAccExt,B0001,00006746464646,S0001,BA0001,A,ABC,XYZ,UID,SatishPahuja,SatishPahuja,090909,151512

主要class

package gov.in.dop.rsi.mgnregs.batch;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BatchProcessExtract {

    public static void main(String[] args) {
        System.setProperty("java.security.egd", "file:///dev/urandom");

        String[] springConfig  = 
            {   "com/mgnregs/res/database.xml", 
                "com/mgnregs/res/context.xml",
                "com/mgnregs/res/job-report.xml" 
            };

        ApplicationContext context = 
                new ClassPathXmlApplicationContext(springConfig);

        JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
        Job job = (Job) context.getBean("batchExtracts");

        try {

            JobExecution execution = jobLauncher.run(job, new JobParameters());
            System.out.println("Exit Status : " + execution.getStatus());

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Done");
    }
}

我收到此错误并尝试了很多操作,例如删除 txt 文件中值之间的 space,在所有值中添加双引号。请帮我解决这个问题

package gov.in.dop.rsi.mgnregs.batch;

import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.core.io.FileSystemResource;

public class FileBatchReader<BoOfficeAccExtract> implements ItemReader {

    public BoOfficeAccExtract read() throws Exception, UnexpectedInputException,
    ParseException, NonTransientResourceException {

        System.out.println("Inside read method-0-");

        DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer();
        lineTokenizer.setNames(new String[] {"extFileName","boId","officeAccountNo","solId" ,"bankId","ind","freeField1","freeField2","freeField3","modifiedBy","createdBy","modifiedDate","createdDate"});

        BeanWrapperFieldSetMapper<BoOfficeAccExtract> fieldSetMapper = new BeanWrapperFieldSetMapper<BoOfficeAccExtract>();
        fieldSetMapper.setPrototypeBeanName("BoOfficeAccExtract");

        DefaultLineMapper<BoOfficeAccExtract> lineMapper = new DefaultLineMapper<BoOfficeAccExtract>();

        lineMapper.setLineTokenizer(lineTokenizer);
        lineMapper.setFieldSetMapper(fieldSetMapper);

        FlatFileItemReader<BoOfficeAccExtract> reader = new FlatFileItemReader<BoOfficeAccExtract>();
        reader.setResource(new FileSystemResource("C:\Users\satish_pahuja\Desktop\BO_Office_Acc_Ext.txt"));
        reader.setLineMapper(lineMapper);


        reader.open(new ExecutionContext());
        BoOfficeAccExtract boOfficAccExtract=reader.read();

        return boOfficAccExtract;
    }
}

对于初学者来说,放弃你拥有的那个丑陋的 reader,你在每次调用 read() 时一遍又一遍地阅读同一行。只需使用配置而不是 reader 包装 reader.

这也是因为 BeanWrapperFieldSetMapper 需要 BeanFactory(它实现了 BeanFactoryAware 但由于您的设置永远不会被调用。

基本上放弃你的 reader 并简单地在 XML 中配置它。将您的 cvsFileItemReader bean 定义替换为以下内容(修复包,因为我没有包含那些包)。

<bean id="cvsFileItemReader" class="FlatFileItemReader">
    <property name="resource" value="C:\Users\satish_pahuja\Desktop\BO_Office_Acc_Ext.txt" />
    <property name="lineMapper">
        <bean class="DefaultLineMapper">
            <property name="lineTokenizer">
                <bean class="DelimitedLineTokenizer">
                    <property name="names" value="extFileName,boId,officeAccountNo,solId,bankId,ind,freeField1,freeField2,freeField3,modifiedBy,createdBy,modifiedDate,createdDate" />
                </bean>
            </property>
            <property name="fieldSetMapper">
                <bean class="BeanWrapperFieldSetMapper">
                    <property name="prototypeBeanName" value="BoOfficeAccExtract" />
                </bean>
            </property>
        </bean>
    </property>
</bean>

或者您可以使用基于 Java 的配置而不是 XML。

@Configuration
public class CsvJobConfiguration {

    @Bean
    public FlatFileItemReader cvsFileItemReader() {
        FlatFileItemReader reader = new FlatFileItemReader();
        reader.setResource(new FileSystemResource("C:\Users\satish_pahuja\Desktop\BO_Office_Acc_Ext.txt"));
        reader.setLineMapper(csvLineMapper());
        return reader;
    }

    @Bean
    public LineMapper<BoOfficeAccExtract> csvLineMapper() {
        DefaultLineMapper<BoOfficeAccExtract> lineMapper = new DefaultLineMapper<BoOfficeAccExtract>();
        lineMapper.setLineTokenizer(csvLineTokenizer());
        lineMapper.setFieldSetMapper(csvFieldSetMapper());
        return lineMapper;
    }

    @Bean
    public LineTokenizer csvLineTokenizer() {
        DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer();
        lineTokenizer.setNames(new String[] {"extFileName","boId","officeAccountNo","solId" ,"bankId","ind","freeField1","freeField2","freeField3","modifiedBy","createdBy","modifiedDate","createdDate"});
        return lineTokenizer;
    }

    @Bean
    public FieldSetMapper csvFieldSetMapper() {
        BeanWrapperFieldSetMapper<BoOfficeAccExtract> fieldSetMapper = new BeanWrapperFieldSetMapper<BoOfficeAccExtract>();
        fieldSetMapper.setPrototypeBeanName("BoOfficeAccExtract");
        return fieldSetMapper;
    }


}