Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use spring batch to parse fixed length formats Files (File Without any delimiter)

how to Configure spring-batch reader for fixed length formats Files (File Without any delimiter).

Each element is determined in relation to its starting and ending position.

Sample of line :

120180208FAILED
220180208SUCCES
120170208SUCCES
1 : code , 20180208 : date ,FAILED : status

like image 750
B.Nbl Avatar asked Jan 28 '23 16:01

B.Nbl


1 Answers

you can use FixedLengthTokenizer reader for this.

This is how you can configure FixedLengthTokenizer .

Sample Text file

UK21341EAH4121131.11customer1
UK21341EAH4221232.11customer2
UK21341EAH4321333.11customer3
UK21341EAH4421434.11customer4
UK21341EAH4521535.11customer5

Java Config

@Bean
    public FixedLengthTokenizer fixedLengthTokenizer() {
            FixedLengthTokenizer tokenizer = new FixedLengthTokenizer();
    
            tokenizer.setNames("ISIN", "Quantity", "Price", "Customer");
            tokenizer.setColumns(new Range(1,12),
                                 new Range(13,15),
                                 new Range(16,20),
                                 new Range(21,29));
            return tokenizer;
    }

XML Config

<bean id="fixedLengthLineTokenizer"
      class="org.springframework.batch.io.file.transform.FixedLengthTokenizer">
    <property name="names" value="ISIN,Quantity,Price,Customer" />
    <property name="columns" value="1-12, 13-15, 16-20, 21-29" />
</bean>
like image 93
Niraj Sonawane Avatar answered Jan 31 '23 07:01

Niraj Sonawane