简体   繁体   中英

Multiple pattern in input file in Batch processing in Java with Spring

Good day,

I am referring to here in my work.

Here is part of the code :

<bean id="flatFileTokenizer"   class="org.springframework.batch.item.file.transform.PrefixMatchingCompositeLineTokenizer">
    <property name="tokenizers">
        <map>
            <entry key="10" value-ref="recordType10" />
            <entry key="20" value-ref="recordType20" />
            <entry key="21" value-ref="recordType21" />
            [...]
        </map>
    </property>
</bean>

Would like to ask what is the key stand for? and what are the 10 , 20 , 21 is refer to?

Is it means It will match with my input file, which is the first row of data start with 10 , 20 and so on?

Here is my own work, the following is my code :

<beans:bean id="salaryDeductionLineTokenizer" class="org.springframework.batch.item.file.transform.PatternMatchingCompositeLineTokenizer">
        <beans:property name="tokenizers">
            <beans:map>
                <beans:entry key="01*" value-ref="header" />
                <beans:entry key="02*" value-ref="details" />
            </beans:map>
        </beans:property>
    </beans:bean>

    <beans:bean id="header" class="org.springframework.batch.item.file.transform.FixedLengthTokenizer">
        <beans:property name="names" value="headerRecordType,headerDate,headerAGBranchCode,headerTotalRecord,headerTotalAmount,headerAgencyName,headerFiller" />
        <beans:property name="columns" value="1-2, 3-8, 9-12, 13-17, 18-28, 29-48, 49-120" />
    </beans:bean>

    <beans:bean id="details" class="org.springframework.batch.item.file.transform.FixedLengthTokenizer">
        <beans:property name="names" value="recordType,department,payCenter,region,personelNo,ic,accountNumber,name,deductionAmount,deductionCode,filler" />
        <beans:property name="columns" value="1-2, 3-10, 11-14, 15-16, 17-24, 25-36, 37-56, 57-96, 97-105, 106-109, 110-120" />
    </beans:bean>

The purpose to create this tokenizer is to read data from a txt file which consists of different styles of data row, which is first row is header, and the other row is details row.

The following is example data of my txt file : 0120140310700000500000030000AgencyA Filler1
02PTJ ABCD00123456788912190860771234567890 Chong Jun Xian
02KLM ABCE00123456798912190860781234567894 Chong Jun Xian
02KLN ABCF0012345680891219086081108187066968 Chong Jun Cian
0120140310700000500000030000AgencyA Filler1
02PTJ ABCD00123456788912190860771234567890 Chong Jun Xian
02KLM ABCE00123456798912190860781234567894 Chong Jun Xian
02KLN ABCF0012345680891219086081108187066968 Chong Jun Cian

I am getting error as follow : Caused by: java.lang.IllegalArgumentException: Cannot access column [recordType] from [headerRecordType, headerDate, headerAGBranchCode, headerTotalRecord, headerTotalAmount, headerAgencyName, headerFiller]

Try googling but cant found solution. Kindly advise.

*Please notify me if my question is confusing. *

That piece of configuration is defining a list of line tokenizers that match exact patterns. See below the explanation from the Spring Batch API itself:

A LineTokenizer implementation that stores a mapping of String patterns to delegate LineTokenizers. Each line tokenizied will be checked to see if it matches a pattern. If the line matches a key in the map of delegates, then the corresponding delegate LineTokenizer will be used. Patterns are sorted starting with the most specific, and the first match succeeds.

I was saying above that matches exact patterns, because each key doesn't contain any special character ('*' or '?'), so a line in a file will match if it is exactly "10" or "20" (as a String). If a line that is exactly "10" matches it will be handed over to the LineTokenizer "recordType10".

But, I doubt the author wanted that example, because in his blog there is another entry that describes a migration to Spring Batch 2.1 where the sample code contains patterns as "10*" and "20*" which make more sense.

"10*" means 'starts with 10', "20*" means 'starts with 20'.

Please have a look at this discussion on Spring Forum

Solution:

Try without spaces in both at names and columns

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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