1

I have a Pojo Partner: Partner Id List

Address Pojo : AddressId, Address, City, Country, Pin

I want to create a Flat file in Spring Batch - File will be : PartnerId;AddressId;Address;City;Country;Pin

I am getting Partner Pojo with Id and List of Addresses

How can I use the FlatFileItemWriter with the PartnerPojo My FlatFileItemWriterConfiguration configuration:

<?xml version="1.0" encoding="UTF-8"?>
<bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
   <property name="resource" value="file:outputFile.txt" />
   <property name="lineAggregator">
      <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
         <property name="delimiter" value=";" />
         <property name="fieldExtractor">
            <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
               <property name="names" value="partnerId, addressId, address,city,country,pin " />
            </bean>
         </property>
      </bean>
   </property>
   <property name="headerCallback" ref="headerCallback" />
</bean>

I get an error on addressId

Nikhil Nadkar
  • 43
  • 2
  • 11

1 Answers1

1

You need to flatten your data and pass the list of flat items as expected in the output file to the writer. For example:

class Partner {
   int id;
   List<Address> addresses;
}

class Address {
   int addressId;
   String address,city,country,pin;
}

// create this Pojo to encapsulate flat data (as in the expected csv)
class PartnerAddress {
   int partnerId, addressId;
   String address,city,country,pin;
}

An item processor would prepare the data:

class PartnerItemProcessor implements ItemProcessor<Partner, List<PartnerAddress>> {

    @Override
    public List<PartnerAddress> process(Partner partner) {
        List<PartnerAddress> partnerAddresses = new ArrayList<>();
        for (Address address : partner.getAddresses()) {
            PartnerAddress partnerAddress = new PartnerAddress();
            partnerAddress.setPartnerId(partner.getId());
            partnerAddress.setAddressId(address.getAddressId());
            partnerAddress.setAddress(address.getAddress());
            partnerAddress.setCity(address.getCity());
            partnerAddress.setCountry(address.getCountry());
            partnerAddress.setPin(address.getPin());
            partnerAddresses.add(partnerAddress);
        }
        return partnerAddresses;
    }
}

Then the writer receives the list of PartnerAddress and write them to the flat file.

Hope this helps.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
  • @Mahmoud Ben Hassine How to write writer in this case? – Roberto Jul 12 '19 at 12:31
  • @Mahmoud Ben Hassine In this case, processor produce List of PartnerAddress. But writer recieves one element only and compiller error – Roberto Jul 12 '19 at 12:34
  • You have to use `FlatFileItemWriter` as delegate and implement `ItemWriter` and `ItemStream`. MultiLineTradeItemWriter class in official spring batch sample shows how to use it. https://github.com/spring-projects/spring-batch/blob/main/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/MultiLineTradeItemWriter.java – Sxc-Dev Jul 28 '23 at 00:23