简体   繁体   中英

How to copy a field in one record to another record using BeanIO

Am trying to access the data for "accountNumber" field in record2 as a "property". File format is flat file (format="fixedlength"). Please help me on this.

<beanio xmlns="http://www.beanio.org/2012/03">
    <stream name="Definitions" format="fixedlength">
        <record name="header" order="1" minOccurs="1" maxOccurs="1" class="map">
            <field name="recordType"  literal="HD" position="0" length="2" rid="true"/>
        </record>
        <group name="Sample" order="2" minOccurs="0" maxOccurs="unbounded" class="map">
                <record name="record1" order="1" minOccurs="1" maxOccurs="1"  class="map">
                    <field name="type" rid="true" literal="PP" position="0" length="2"/>
                    <field name="accountNumber" position="2" length="30"/>
                </record>
                <record name="record2" order="2" minOccurs="0" maxOccurs="unbounded" class="map" collection="list" >
                    <field name="type" rid="true" literal="FF" position="0" length="2"/>
            < ******------ I want to access the "accountNumber" here ------****** />
                </record>
        </group>
        <record name="trailer" order="3" >
            <field name="recordType" rid="true" literal="TT" position="0" length="2"/>
        </record>  
    </stream>
</beanio>

Sample Data:

HD                                 
PPXXXXXXXXXXXXXXXXXXXXXXXXXXX   
FF                                    
FF                                    
PPYYYYYYYYYYYYYYYYYYYYYYYYYYY                  
FF                                    
FF                                    
TT

I think you will have to do the combination yourself after reading all the records. This will be possible because the information from record1 will exist in the map for the Sample group. You will have to iterate over each record2 and set the information from record1 .


I haven't fully implemented this yet, but I will soon, and here is my strategy using spring-batch:

  • I don't group my records.
  • Create different classes for the two records.
  • Set up a batch job with the bean-io ItemReader .
  • Add a ClassifierCompositeItemProcessor to the job with a SubclassClassifier .
    • The type map maps the two records' classes to specific ItemProcessors ,
    • The record1 ItemProcessor will add the information in that record to the step's ExecutionContext and return null to avoid the ItemWriter from being called.
  • Add an ItemWriter to the job which will read the ExecutionContext and write the combination of record1 with each record2 .

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM