在CSV中读取换行符,在春季批处理的FlatfileItemReader文件中引用 [英] Reading line breaks in CSV which are quoted in the file in FlatfileItemReader of spring batch

查看:1427
本文介绍了在CSV中读取换行符,在春季批处理的FlatfileItemReader文件中引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to parse a CSV file with FlatFileItemReader. This CSV contains some quoted newline characters as shown below.

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

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

But this parsing is failing with required fields are 2 but actual is 1.

我在FlatFileReader配置中丢失了什么?

What I am missing in my FlatFileReader configuration?

<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 ,为您的用户程序

out of the box the FlatFileItemReader uses a SimpleRecordSeparatorPolicy, for your usecase


  • 已注释的部分超过2行或以上

需要设置 DefaultRecordSeparatorPolicy

引用自其javadoc:

Cited from its javadoc:


将所有行视为记录结尾的RecordSeparatorPolicy ,为
,因为它们没有未终止的引号,并且不以
连续标记结束。

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.

example xml configuration

example xml configuration

<bean id="reader" 
      class="org.springframework.batch.item.file.FlatFileItemReader">
      ...
    <property name="recordSeparatorPolicy">
        <bean class="org.springframework.batch.item.file.separator.DefaultRecordSeparatorPolicy" />
    </property>
      ...
</bean>

这篇关于在CSV中读取换行符,在春季批处理的FlatfileItemReader文件中引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆