在 spring 批次的 FlatfileItemReader 文件中引用的 CSV 中的读取换行符

Reading line breaks in CSV which are quoted in the file in FlatfileItemReader of spring batch

我正在尝试使用 FlatFileItemReader 解析 CSV 文件。此 CSV 包含一些引用的换行符,如下所示。

email, name
abc@z.com, "NEW NAME
 ABC"

但是此解析失败,必填字段为 2,但实际为 1。

我的 FlatFileReader 配置中缺少什么?

<property name="lineMapper">
            <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">

                <!-- The lineTokenizer divides individual lines up into units of work -->
                <property name="lineTokenizer">
                    <bean
                        class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">

                        <!-- Names of the CSV columns -->
                        <property name="names"
                            value="email,name" />
                    </bean>
                </property>

                <!-- The fieldSetMapper maps a line in the file to a Product object -->
                <property name="fieldSetMapper">
                    <bean
                        class="com.abc.testme.batchjobs.util.CustomerFieldSetMapper" />
                </property>
            </bean>
        </property>

开箱即用的 FlatFileItemReader 使用 SimpleRecordSeparatorPolicy,用于您的用例

  • 注释部分超过 2 行或更多行

您需要设置 DefaultRecordSeparatorPolicy

引自其 javadoc:

A RecordSeparatorPolicy that treats all lines as record endings, as long as they do not have unterminated quotes, and do not end in a continuation marker.

示例xml 配置

<bean id="reader" 
      class="org.springframework.batch.item.file.FlatFileItemReader">
      ...
    <property name="recordSeparatorPolicy">
        <bean class="org.springframework.batch.item.file.separator.DefaultRecordSeparatorPolicy" />
    </property>
      ...
</bean>
itemReader.setRecordSeparatorPolicy(new DefaultRecordSeparatorPolicy());