You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Batch文件分区导入报错:FlatFileItemReader无parameterValues属性

Hey there! Let's tackle your problem head-on. First, let's break down why you're seeing that error, then look at the right tools for file partitioning in Spring Batch.

Why the Error Occurs

The org.springframework.beans.NotWritablePropertyException is clear about the issues:

  • parameterValues isn't a valid property of FlatFileItemReader: This property belongs to JDBC-based readers like JdbcCursorItemReader, not file readers. You likely mixed up configurations from different reader types.
  • rowMapper is also invalid here: FlatFileItemReader uses a lineMapper to parse CSV lines, while rowMapper is for mapping database rows to POJOs—another mismatch in your setup.

For partitioning CSV file imports, you have two solid approaches depending on your file size and use case:

1. FlatFileItemReader with RangePartitioner (For Single Large File)

If you're working with one large CSV file, use RangePartitioner to split the file into row ranges. Then configure FlatFileItemReader to read only its assigned range using two built-in properties:

  • currentItemCount: Skips the first N rows (matches your startingIndex)
  • maxItemCount: Limits the number of rows to read (calculated as endingIndex - startingIndex + 1)

Corrected Reader Configuration

<bean id="flatFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
    <property name="resource" value="classpath:ExamResult.txt" />
    <property name="lineMapper">
        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
            <property name="fieldSetMapper">
                <bean class="com.websystique.springbatch.ExamResultFieldSetMapper" />
            </property>
            <property name="lineTokenizer">
                <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                    <property name="delimiter" value="|" />
                </bean>
            </property>
        </bean>
    </property>
    <!-- Use StepExecutionContext values to control row range -->
    <property name="currentItemCount" value="#{stepExecutionContext['startingIndex']}" />
    <property name="maxItemCount" value="#{stepExecutionContext['endingIndex'] - stepExecutionContext['startingIndex'] + 1}" />
</bean>

Example RangePartitioner Setup

<bean id="rangePartitioner" class="org.springframework.batch.core.partition.support.RangePartitioner">
    <!-- Number of partitions (adjust based on your needs) -->
    <property name="gridSize" value="2" />
</bean>

2. MultiResourceItemReader (For Pre-Split Files)

If your file is extremely large (10GB+), it's more efficient to split it into smaller, equal-sized files first (using a pre-processing tasklet or external tool). Then use MultiResourceItemReader to assign each small file to a partition.

Sample Configuration

<bean id="multiResourceItemReader" class="org.springframework.batch.item.file.MultiResourceItemReader" scope="step">
    <!-- Get the assigned file resource from StepExecutionContext -->
    <property name="resources" value="#{stepExecutionContext['fileResource']}" />
    <property name="delegate">
        <bean class="org.springframework.batch.item.file.FlatFileItemReader">
            <property name="lineMapper">
                <!-- Same lineMapper as before -->
                <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
                    <property name="fieldSetMapper">
                        <bean class="com.websystique.springbatch.ExamResultFieldSetMapper" />
                    </property>
                    <property name="lineTokenizer">
                        <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                            <property name="delimiter" value="|" />
                        </bean>
                    </property>
                </bean>
            </property>
        </bean>
    </property>
</bean>

Your partitioner would populate fileResource in each Step's execution context with the path to one of the split files.

Key Takeaways

  • Remove parameterValues and rowMapper from your FlatFileItemReader config—they don't belong here.
  • Use currentItemCount + maxItemCount with RangePartitioner for single large files.
  • Use MultiResourceItemReader for pre-split smaller files (better performance for massive datasets).

内容的提问来源于stack exchange,提问作者user2991123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 10:03:21