Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - FlatFileItemReader usage with FixedLengthTokenizer

I want to read transactions from text file and then write this transactions to database with JdbcBatchItemWriter. When I use csv file and DelimitedLineTokenizer, it works fine. If I use text file and FixedLengthTokenizer, couldn't read lines because readLine() method return null in doRead() method. How can I resolve this issue. Thanks for your help.

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
like image 993
S.Balaban Avatar asked Nov 06 '22 22:11

S.Balaban


1 Answers

try this - you have used DelimitedLineTokenizer , instead try using 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>
like image 154
Ashish Shetkar Avatar answered Nov 14 '22 21:11

Ashish Shetkar