Cheloute
Cheloute

Reputation: 893

Spring Batch Flat File Reader

I'm new to Spring Batch and am quite lost with it...

I have 4 kinds of input files to process, which follow the same pattern :

header record footer

Of course, "record" is what changes between my 4 different kinds of file.

When a file is read, a field in header specifies which kind of record is currently composing the processed flat file. In addition there are 3 fields in my header I must add to each (file) record before insert it into a database.

And I guess to consider it more difficult it was (for me...), I also have to calcule the MD5 checksum of the process file to add this data a all of the records of my database.

So, to illustrate what I try to explain :

File 1 (type = Contract)

1CONTRACTHDATA1HDATA2HDATA3
2CONTRACTDATA1CONTRACTDATA2CONTRACTDATA3
2CONTRACTDATA1CONTRACTDATA2CONTRACTDATA3
2CONTRACTDATA1CONTRACTDATA2CONTRACTDATA3
3FOOTERDATA

File 2 (type = THIRD)

1THIRDHDATA1HDATA2HDATA3
2THIRDDATA1THIRDDATA2
2THIRDDATA1THIRDDATA2
2THIRDDATA1THIRDDATA2
3FOOTERDATA

And what I want to insert is :

INSERT INTO CONTRACT (field1, field2, field3, field4, field5, field6, field7) VALUES (HDATA1, HDATA2, HDATA3, CONTRACTDATA1, CONTRACTDATA2, CONTRACTDATA3, MD5) for each "2*" lines of my first file

and

INSERT INTO THIRD (field1, field2, field3, field4, field5, field6) VALUES (HDATA1, HDATA2, HDATA3, THIRDDATA1, THIRDDATA2, MD5) for each "2*" lines of my second file.

Thank you to anyone who can help !

Upvotes: 1

Views: 9351

Answers (1)

Jackson Ha
Jackson Ha

Reputation: 682

For your job, you need two steps.

Step 1, it needs to calculate the MD5 checksum of the file. For this, you should use a tasklet step.

Step 2, is your normal spring batch chunk oriented processing step and a simple flat file reader with a fixed length tokenizer. Since you are new to spring batch, let's keep it simple and just do an if statement in the ItemProcessor to detect for the "1" line. When you do, save a copy of the header info. When you detect "2" line, compose an obj with your header info, and pass it on. When you detect "3" return null to skip that entry.

The writer should be pretty straight forward. Implement the ItemWriter interface and make jdbc call.

good luck.

Upvotes: 1

Related Questions