简体   繁体   English

读取 CSV 中的换行符,这些换行符在 spring 批处理的 FlatfileItemReader 中的文件中引用

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

I am trying to parse a CSV file with FlatFileItemReader.我正在尝试使用 FlatFileItemReader 解析 CSV 文件。 This CSV contains some quoted newline characters as shown below.此 CSV 包含一些带引号的换行符,如下所示。

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

But this parsing is failing with required fields are 2 but actual is 1.但是此解析失败,必填字段为 2,但实际为 1。

What I am missing in my FlatFileReader configuration?我的 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>

out of the box the FlatFileItemReader uses a SimpleRecordSeparatorPolicy , for your usecase开箱即用的 FlatFileItemReader 使用SimpleRecordSeparatorPolicy ,用于您的用例

  • commented part goes over 2 or more lines注释部分超过 2 行或更多行

you need to set the DefaultRecordSeparatorPolicy你需要设置DefaultRecordSeparatorPolicy

Cited from its javadoc:引用自其 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.将所有行视为记录结尾的 RecordSeparatorPolicy,只要它们没有未终止的引号,并且不以继续标记结尾。

example xml configuration示例 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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM