How to customize Spring Batch DelimitedLineTokenizer

I have two types of files to insert into a database.

Format: aa; bb; cc and aa; bb; cc; dd; ee

This is my FlatFileItemReader:

    <bean name="readerContractToAddIntoPRV" class="org.springframework.batch.item.file.FlatFileItemReader">
        <property name="comments" value="#" />
        <property name="linesToSkip" value="1" />
        <property name="strict" value="false" />
        <property name="lineMapper">
            <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
                <property name="fieldSetMapper">
                    <bean class="net.wl.batchs.fieldSetMapper.LineToCreateIntoPrvFieldSetMapper" />
                </property>
                <property name="lineTokenizer">
                    <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                        <property name="delimiter" value=";"/>
                        <property name="names" value="aa,bb,cc,dd,ee" />
                    </bean>
                </property>
            </bean>
        </property>
    </bean>

      

I need a setting that works for both types of files.

At the moment I have the following:

org.springframework.batch.item.file.transform.IncorrectTokenCountException: Invalid number of tokens found in record: 3 actual 5 expected

Do you have any ideas?

Thank.

Edit: after correction:

<bean name="readerContractToAddIntoPRV" class="org.springframework.batch.item.file.FlatFileItemReader">
    <property name="comments" value="#" />
    <property name="linesToSkip" value="1" /> 
    <property name="strict" value="false" />
    <property name="lineMapper">
        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper" p:lineTokenizer-ref="multilineFileTokenizer">
            <property name="fieldSetMapper">
                <bean class="net.wl.batchs.fieldSetMapper.LineToCreateIntoPrvFieldSetMapper" />
            </property>
        </bean>
    </property>
</bean>

<bean id="multilineFileTokenizer" class="org.springframework.batch.item.file.transform.PatternMatchingCompositeLineTokenizer">
  <property name="tokenizers">
    <map>
      <entry key="*;*;*;*;*" value-ref="NSCE_ICCID_MSISDN_LOGIN_PWD"/>
      <entry key="*;*;*" value-ref="NSCE_ICCID_MSISDN"/>
      <entry key="*" value-ref="headerDefault"/>
    </map>
  </property>
</bean>

<bean id="parentLineTokenizer" class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer" abstract="true">
    <property name="delimiter" value=";"/>
</bean>
<bean id="NSCE_ICCID_MSISDN_LOGIN_PWD" parent="parentLineTokenizer">
    <property name="names" value="nsce,iccid,msisdn,login,pwd" />
</bean>
<bean id="NSCE_ICCID_MSISDN" parent="parentLineTokenizer">
    <property name="names" value="nsce,iccid,msisdn" />
</bean>
<bean id="headerDefault" parent="parentLineTokenizer">
    <property name="names" value="nsce,iccid,msisdn" />
</bean>

      

+3


source to share


1 answer


The problem is not with your tokenizer. What you need to do is use PatternMatchingCompositeLineMapper

( http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/file/mapping/PatternMatchingCompositeLineMapper.html ). This will allow you to create a template for each line type you have and link it to the appropriate one LineTokenizer

.



You can see this LineMapper

in action in our examples here: https://github.com/spring-projects/spring-batch/blob/master/spring-batch-samples/src/main/resources/jobs/multilineOrderInputTokenizers.xml

+3


source







All Articles