春季-使用FixedLengthTokenizer的FlatFileItemReader用法

时间:2019-01-14 10:49:45

标签: java spring-batch spring-jdbc jdbctemplate

我想从文本文件中读取事务,然后使用JdbcBatchItemWriter将此事务写入数据库。当我使用csv文件和DelimitedLineTokenizer时,它工作正常。如果我使用文本文件和FixedLengthTokenizer,则无法读取行,因为readLine()方法在doRead()方法中返回null。我该如何解决此问题。感谢您的帮助。

fileManager.xml

<bean id="transactionDataFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader"
      scope="step">
    <property name="resource" value="#{jobParameters['processPath']}"/>
    <property name="lineMapper">
        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
            <property name="lineTokenizer">
                <bean class="org.springframework.batch.item.file.transform.FixedLengthTokenizer">
                    <property name="names" value="type,code"/>
                    <!--<property name="columns" value="1-12, 13-15"/>-->
                </bean>
            </property>
            <property name="fieldSetMapper">
                <bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                    <property name="prototypeBeanName" value="transactionMap"/>
                </bean>
            </property>
        </bean>
    </property>
    <property name="linesToSkip" value="1"/>
</bean>


<bean id="transactionDataFileItemWriter"
      class="org.springframework.batch.item.database.JdbcBatchItemWriter">
    <property name="dataSource" ref="dataSource"/>
    <property name="sql">
        <value>
            <![CDATA[
       insert into TB_FORMAT(TYPE, CODE)
        values (:type, :code)
   ]]>
        </value>
    </property>
    <property name="itemSqlParameterSourceProvider">
        <bean class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider"/>
    </property>
</bean>

file.txt

123         abc              
456         def

1 个答案:

答案 0 :(得分:1)

尝试-您已使用DelimitedLineTokenizer,而是尝试使用FixedLengthTokenizer

<bean id="transactionDataFileItemReader"
    class="org.springframework.batch.item.file.FlatFileItemReader"
    scope="step">
    <property name="resource"
        value="#{jobParameters['processPath']}" />
    <property name="lineMapper">
        <bean
            class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
            <property name="lineTokenizer">
                <bean
                    class="org.springframework.batch.io.file.transform.FixedLengthTokenizer">
                    <property name="names" value="type,code" />
                    <property name="columns" value="1-12, 13-15" />
                </bean>
            </property>
            <property name="fieldSetMapper">
                <bean
                    class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                    <property name="prototypeBeanName" value="transactionMap" />
                </bean>
            </property>
        </bean>
    </property>
    <property name="linesToSkip" value="1" />
</bean>