Spring-Batch Multi-line record Item Writer with variable number of lines per record

后端 未结 3 1655
旧巷少年郎
旧巷少年郎 2020-12-09 06:56

I have the below requirement but am not able to decide on the approach to take:

I need to write data to a fixed format out put file where each record spans over mult

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-09 07:36

    I had a similar problem writing multiple rows to a database. Because the job step will create a List of your items, how can you return a List from the processer to the writer? That would create a List of Lists, and the doWrite method in the Writer is not set up to handle that scenario.

    I am using the 1.2.0 spring-boot-starter-parent (which gives me spring-core 4.1.3) along with hibernate (4.3.7), In my case I have 'receivables', and for every 1 receivable that I read from a csv file, I then might need to update or insert many receivables into my table. I am also using HibernateItemWriter.

    My solution was to extend the HibernateItemWriter. The sessionFactory field is private, so I am passing it via the constructor and also using the setter (to accomadate the existing code). So my original code (which works fine for a single 'receivable') originally looked like this:

        @Bean
        public ItemWriter recivableWriter() {
            HibernateItemWriter hibernateItemWriter = new HibernateItemWriter(){{ setSessionFactory(sessionFactory); }};
            return hibernateItemWriter;
        }
    

    After extending the HibernateItemWriter, and modifying my processor to return a List, my code changed to:

        @Bean
        public ItemWriter> receivableWriter() {
            HibernateListItemWriter> hibernateItemWriter = new HibernateListItemWriter>(this.sessionFactory){{ setSessionFactory(sessionFactory); }};
            return hibernateItemWriter;
        }
    

    And my extended class looks like this (I might clean it up but this is my initial pass. I also wish the sessionFactory and clearSession fields were not private)

    package com.work;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.hibernate.SessionFactory;
    
    public class HibernateListItemWriter extends org.springframework.batch.item.database.HibernateItemWriter {
    
        public HibernateListItemWriter(SessionFactory sessionFactory) {
            super();
            this.sessionFactory = sessionFactory;
        }
    
        private SessionFactory sessionFactory; 
        private boolean clearSession = true;
    
        @Override
        public void write(List items) {
            List unwrappedItems = new ArrayList();
            List> wrappedItems = (List>)items;
            for (List singleList: wrappedItems) {
                unwrappedItems.addAll(singleList);
            }
            doWrite(sessionFactory, unwrappedItems);
            sessionFactory.getCurrentSession().flush();
            if(clearSession) {
                sessionFactory.getCurrentSession().clear();
            }
        }
    
    }
    

    Big shout out to grepcode.com for making life easier.

提交回复
热议问题