0

The requirement is to fetch the data(pls refer below for sample data in file) from database and create fixed length flat file using Spring Batch. The specification for this file is there will be multiple records with the same alignment like 1-5,4-7,8-24...... Now the problem is due to records with same alignment obviously data will get overwritten. I need some solution to resolve this.

My sample code:

Here bean1,bean2 are the mapped bean object for the table.

     <!-- ======================================================= -->
    <!-- Jobs Definition -->
    <!-- ======================================================= -->

    <!-- Main processes -->

    <!-- Active -->

    <job id="FixedLengthFlatFileGenerationJob" xmlns="http://www.springframework.org/schema/batch">

        <step id="FixedLengthFlatFileGenerationStep">
            <tasklet start-limit="1">
                <chunk reader="SampleReader" writer="SampleWriter"
                    commit-interval="1000">
                </chunk>                
                <listeners>
                    <listener ref="stepExecutionListener"/>
                </listeners>
            </tasklet>      
            <next on="COMPLETED" to="FileTransferStep"/>    
            <next on="STOPPED" to="SendMailOnFailure"/>
            <fail on="*"/>  
        </step>     

        <step id="SendMailOnFailure">
            <tasklet ref="OnFailureTasklet"/>
        </step>

        <step id="FileTransferStep">
            <tasklet ref="FileTransferTasklet" />
        </step>

    </job>

    <!-- ======================================================= -->
    <!-- Readers -->
    <!-- ======================================================= -->

    <bean id="SampleReader"
        class="org.springframework.batch.item.database.JdbcCursorItemReader">
        <property name="dataSource" ref="SampleDataSource" />
        <property name="sql">
            <value>
                SQL READ FROM TABLE
            </value>
        </property>
        <property name="rowMapper" ref="SampleMapper" />
    </bean>


    <!-- ======================================================= -->
    <!-- Writers -->
    <!-- ======================================================= -->

    <bean id="SampleWriter"
        class="org.springframework.batch.item.support.CompositeItemWriter">
        <property name="delegates">
            <list>
                <ref local="FileTransferWriter" />              
            </list>
        </property>
    </bean>
    <bean id="FileTransferWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
    <property name="resource" value="file:filelocation/file.txt" />

    <property name="lineAggregator">
        <bean class="org.springframework.batch.item.file.transform.FormatterLineAggregator">
            <property name="fieldExtractor">
                <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                 <property name="names" value="bean1,bean2" /> 

                </bean>
            </property>
            <property name="format" value="%-20s%-30s" />
        </bean>
    </property>
   </bean>

Sample Fixedlengthflatfile.txt

file.txt                                                                                               TM                          45150605033000                
UJK5457                          0000000000HC605-B045285                                      D34151631115600                   A                                                                                                                                                                        
BHJ5457                          724570420                                                                                            34151631315600                                                                                                                                                                                                                                      77014                                                                                               ct scan for therapy guide     Physical Therapy              1S   001          002060  O   AA
NTS5457                          This is a test for Policy Number 5457
UJK8334                          0000000000HC605-B045285                                      D34151631315600                   A                                                                                                                                                                        
QWS6334                          724570420                                                                                            34151631315600                                                                                                                                                                                                                                      72142                                                                                               mri neck spine w/dye          Occupational Therapy          2V001             002060  O   AA
ETS4334                          This is a test for Policy Number 4334.
RYT6313                          0000000000HC216-B406574                                      D34151611115600                   A  
Ishaan
  • 21
  • 1
  • 5
  • could you provide example data: what is read from the database; what is written into the file; what should be written into the file. Moreover, could you also provide your step configuration and not just the reader and writer. – Hansjoerg Wingeier Nov 26 '15 at 06:23
  • Thanks Wingeier.I have updated my question with details – Ishaan Nov 26 '15 at 15:38
  • Can you explain what you expect? Because if I understood your question, you're asking for a solution to be able to fit `n + x (x > 0)` characters in a `n` sized filed. – Thrax Nov 26 '15 at 15:40

0 Answers0