【问题标题】:FlatFileParseException Parsing class path resource error in spring batchSpring批处理中FlatFileParseException解析类路径资源错误
【发布时间】:2021-09-24 11:52:08
【问题描述】:

我想设置 csv 文件的 classPath 资源。这是我的 项目资源文件夹。当设置类路径并运行项目时,我 获取类路径资源的运行时问题 问题来了

org.springframework.batch.item.file.FlatFileParseException: Parsing error at line: 1 in resource=[class path resource [result-match-metadata.csv]], input=[id   city    date    player_of_match venue   neutral_venue   team1   team2   toss_winner toss_decision   winner  result  result_margin   eliminator  method  umpire1 umpire2]

result-match-metadata.csv 文件存在于我的资源文件夹中 批处理配置.kt

package com.nilmani.dashboardipl.data

import com.nilmani.dashboardipl.entity.Match
import org.springframework.batch.core.Job
import org.springframework.batch.core.Step
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory
import org.springframework.batch.core.launch.support.RunIdIncrementer
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider
import org.springframework.batch.item.database.JdbcBatchItemWriter
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder
import org.springframework.batch.item.file.FlatFileItemReader
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.io.ClassPathResource
import javax.sql.DataSource


@Configuration
@EnableBatchProcessing
class BatchConfig {
    @Autowired
    private lateinit var jobBuilderFactory: JobBuilderFactory
    @Autowired
    private lateinit var stepBuilderFactory: StepBuilderFactory
    val FIELD_NAMES = arrayOf(
        "id","city","date","player_of_match","venue","neutral_venue",
        "team1","team2","toss_winner","toss_decision",
        "winner","result","result_margin","eliminator","method","umpire1","umpire2"
    )

    @Bean
    fun reader(): FlatFileItemReader<MatchInput> {
        return FlatFileItemReaderBuilder<MatchInput>()
            .name("MatchItemReader")
            .resource(ClassPathResource("result-match-metadata.csv"))
            .delimited()
            .names(*FIELD_NAMES)
            .fieldSetMapper(object : BeanWrapperFieldSetMapper<MatchInput>() {
                init {
                    setTargetType(MatchInput::class.java)
                }
            })
            .build()
    }
}

如何在批量配置中设置csv文件路径

【问题讨论】:

    标签: kotlin spring-batch


    【解决方案1】:

    正确找到资源。该错误意味着第一行无法映射到MatchInput 的实例。您只需在项目阅读器定义中跳过带有FlatFileItemReaderBuilder#linesToSkip(1) 的标题。

    【讨论】:

    • 如果我在 FlatFileItemReaderBuilde 添加linesToSkip(1) 显然它会引发另一个运行时错误,例如“Parsing error at line: 2 in resource=[class path resource”.lineToSkip() 函数跳过该行但不是错误将错误带到下一行。这是愚蠢的想法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多