使用弹簧批在固定长度的平面文件中编写多个布局

时间:2015-11-25 13:14:17

标签: spring-batch fixed-length-file

要求是从数据库中获取数据(请参阅下面的文件中的示例数据),并使用Spring Batch创建固定长度的平面文件。该文件的规范是将有多个具有相同对齐的记录,如1-5,4-7,8-24 ......现在问题是由于具有相同对齐的记录显然数据将被覆盖。我需要一些解决方案来解决这个问题。

我的示例代码:

这里bean1,bean2是表的映射bean对象。

     <!-- ======================================================= -->
    <!-- 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>

示例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  

0 个答案:

没有答案