guilhermerama
guilhermerama

Reputation: 750

Spring Batch ItemReader to process Tree structured File

I'm trying to read a fixed-length text file and import its registers to a database using spring batch but the input file general layout is nested, something like (tabulation added just to simplify visualization):

FILE HEADER
    ENTERPISE1 HEADER
        DEPARTMENT1 HEADER
            WORKER1
            WORKER2
        DEPARTMENT1 FOOTER
        DEPARTMENT2 HEADER
            WORKER3
        DEPARTMENT2 FOOTER
    ENTERPRISE1 FOOTER  
    ENTERPRISE2 HEADER
        DEPARTMENT3 HEADER
            WORKER4
        DEPARTMENT3 FOOTER
    ENTERPRISE2 FOOTER
             .
             .
             .
FILE FOOTER

the file has to be imported to a database with the tables and relations like.

TABLE       RELATION   TABLE 

ENTERPRISE (ONETOMANY) DEPARTMENTS
DEPARTMENT (ONETOMANY) WORKERS

In other spring batch projects i had implemented, we used files with same-layout-regiters with fields that refers to the relation that they belong. In this case the type of the register is identified by an "id" on the first column of the line and the relation by the "tree" structure. Is there any spring out-of-the-box ItemReader that can help on this task? The solution I get is to build a custom ItemReader that reads the enterprise register and keep reading lines building department and worker objects and returning an Enterprise object with all departments and workers to be write into the database at once but I don't know if that would scale when the number of workers and departments are too high (using JPA to write to the database).

Thanks in advance for any help.

Upvotes: 0

Views: 558

Answers (2)

guilhermerama
guilhermerama

Reputation: 750

I ended up putting an solution very similar to the toy example at my fork from spring-batch-samples project here.

Hope this helps someone with a similar issue.

Upvotes: 0

victortav
victortav

Reputation: 7

You can read twice. First, read the lines, adding to database. In second step, read again, updating the relationship into database, saving the values from enterprise and department in custom ItemReader.

Upvotes: 1

Related Questions