Spring-batch(ItemReader)—數據讀取從普通文件,數據庫,XML,多文件數據讀取

Spring-Batch學習總結(3)——如何數據輸入
一.ItemReader概述
1.ItemReader:提供數據的接口
2.在這個接口中只有一個方法read(),它讀取一個數據並且移動到下一個數據上去,在讀取結束時必須返回一個null,否則表明數據沒有讀取完畢;
例:
OverViewApplication:

package com.dhcc.batch.batchDemo.input.overview;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableBatchProcessing
public class OverViewApplication {

    public static void main(String[] args) {
        SpringApplication.run(OverViewApplication.class, args);
    }
}

InputOverViewDemoJobConfiguration:

package com.dhcc.batch.batchDemo.input.overview;

import java.util.Arrays;
import java.util.List;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class InputOverViewDemoJobConfiguration {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job inputOverViewDemoJob() {
        return jobBuilderFactory.get("inputOverViewDemoJob").start(inputOverViewDemoJobStep()).build();

    }

    public Step inputOverViewDemoJobStep() {
        return stepBuilderFactory.get("inputOverViewDemoJobStep").<String, String>chunk(2)
                .reader(inputOverViewDemoReader()).writer(outputOverViewDemoWriter()).build();
    }

    private ItemWriter<? super String> outputOverViewDemoWriter() {
        return new ItemWriter<String>() {

            @Override
            public void write(List<? extends String> items) throws Exception {
                for (String item : items) {
                    System.out.println("output writer data: " + item);
                }
            }
        };
    }

    @Bean
    public InputOverVierDemoItemReader inputOverViewDemoReader() {
        List<String> data = Arrays.asList("dazhonghua", "xiaoriben", "meilijian", "falanxi", "deyizhi", "aierlan",
                "fandigang", "bajisitan", "baieluosi");
        return new InputOverVierDemoItemReader(data);
    }
}

InputOverVierDemoItemReader:

package com.dhcc.batch.batchDemo.input.overview;

import java.util.Iterator;
import java.util.List;

import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;

public class InputOverVierDemoItemReader implements ItemReader<String> {
    private final Iterator<String> iterator;

    public InputOverVierDemoItemReader(List<String> data) {
        this.iterator = data.iterator();
    }

    @Override
    public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
        if (iterator.hasNext()) {
            return this.iterator.next();
        } else {
            return null;
        }
    }

}

運行結果:
Spring-batch(ItemReader)—數據讀取從普通文件,數據庫,XML,多文件數據讀取
二.從數據庫中讀取數據
1.在實際應用中,我們都需要從數據庫中讀取數據,並且進行分頁讀取,在spring-batch中爲我們提供了JDBCPagingItemReader這個類進行數據庫數據讀取
2.例:再舉這個例子之前我們在數據庫中建立了個person_buf表,並向表中插入了100001條數據

Spring-batch(ItemReader)—數據讀取從普通文件,數據庫,XML,多文件數據讀取
接下來我們讀取這個表中的數據爲例,進行學習:
InputItemReaderJDBCApplication:

package com.dhcc.batch.batchDemo.input.db.jdbc;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableBatchProcessing
public class InputItemReaderJDBCApplication {
    public static void main(String[] args) {
        SpringApplication.run(InputItemReaderJDBCApplication.class, args);

    }
}

InputDBJdbcItemReaderConfigruation:

package com.dhcc.batch.batchDemo.input.db.jdbc;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.JdbcPagingItemReader;
import org.springframework.batch.item.database.Order;
import org.springframework.batch.item.database.support.MySqlPagingQueryProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class InputDBJdbcItemReaderConfigruation {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    @Qualifier("DBJdbcWriterDemo")
    private ItemWriter<? super Person> DBJbbcWriterDemo;

    @Autowired
    private DataSource dataSource;

    @Bean
    public Job DBJdbcItemReaderJob() {
        return jobBuilderFactory.get("DBJdbcItemReaderJob4")
                .start(DBJdbcItemReaderJobStep())
                .build();

    }

    @Bean
    public Step DBJdbcItemReaderJobStep() {
        return stepBuilderFactory.get("DBJdbcItemReaderJobStep4")
                .<Person, Person>chunk(100)
                .reader(DBJbbcReaderDemo())
                .writer(DBJbbcWriterDemo)
                .build();
    }

    @Bean
    @StepScope
    public JdbcPagingItemReader<Person> DBJbbcReaderDemo() {
        JdbcPagingItemReader<Person> reader = new JdbcPagingItemReader<>();
        reader.setDataSource(this.dataSource); // 設置數據源
        reader.setFetchSize(100); // 設置一次最大讀取條數
        reader.setRowMapper(new PersonRowMapper()); // 把數據庫中的每條數據映射到Person對中
        MySqlPagingQueryProvider queryProvider = new MySqlPagingQueryProvider();
        queryProvider.setSelectClause("id,name,per_desc,create_time,update_time,sex,score,price"); // 設置查詢的列
        queryProvider.setFromClause("from person_buf"); // 設置要查詢的表
        Map<String, Order> sortKeys = new HashMap<String, Order>();// 定義一個集合用於存放排序列
        sortKeys.put("id", Order.ASCENDING);// 按照升序排序
        queryProvider.setSortKeys(sortKeys);
        reader.setQueryProvider(queryProvider);// 設置排序列
        return reader;
    }

}

DBJdbcWriterDemo:

package com.dhcc.batch.batchDemo.input.db.jdbc;

import java.util.List;

import org.springframework.batch.item.ItemWriter;
import org.springframework.stereotype.Component;
@Component("DBJdbcWriterDemo")
public class DBJdbcWriterDemo implements ItemWriter<Person>{

    @Override
    public void write(List<? extends Person> items) throws Exception {
        for(Person person:items) {
            System.out.println(person);
        }
    }

}

Person

package com.dhcc.batch.batchDemo.input.db.jdbc;

import java.util.Date;

public class Person {
    private Integer id;
    private String name;
    private String perDesc;
    private Date createTime;
    private Date updateTime;
    private String sex;
    private Float score;
    private Double price;

    public Person() {
        super();
    }

    public Person(Integer id, String name, String perDesc, Date createTime, Date updateTime, String sex, Float score,
            Double price) {
        super();
        this.id = id;
        this.name = name;
        this.perDesc = perDesc;
        this.createTime = createTime;
        this.updateTime = updateTime;
        this.sex = sex;
        this.score = score;
        this.price = price;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public String getPerDesc() {
        return perDesc;
    }

    public void setPerDesc(String perDesc) {
        this.perDesc = perDesc;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Float getScore() {
        return score;
    }

    public void setScore(Float score) {
        this.score = score;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", perDesc=" + perDesc + ", createTime=" + createTime + ", updateTime="
                + updateTime + ", sex=" + sex + ", score=" + score + ", price=" + price + "]";
    }

}

PersonRowMapper:

package com.dhcc.batch.batchDemo.input.db.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;
/**
 * 實現將數據庫中的每條數據映射到Person對象中
 * @author Administrator
 *
 */
public class PersonRowMapper implements RowMapper<Person> {

    /**
     * rs一條結果集,rowNum代表當前行
     */
    @Override
    public Person mapRow(ResultSet rs, int rowNum) throws SQLException {
        return new Person(rs.getInt("id")
                ,rs.getString("name")
                ,rs.getString("per_desc")
                ,rs.getDate("create_time")
                ,rs.getDate("update_time")
                ,rs.getString("sex")
                ,rs.getFloat("score")
                ,rs.getDouble("price"));
    }

}

運行結果:
Spring-batch(ItemReader)—數據讀取從普通文件,數據庫,XML,多文件數據讀取

1.FlatFileItemReader:
(1)set lines to skip:設置跳過前幾行
(2)set resource:指定源文件地址
(3)設置一個行解析器映射行對應的結果集
2.例:我們在項目中的resources中放入了兩個csv文件,我們以讀取springbatchtest1.csv爲例:
(1)觀察文件存放路徑:
Spring-batch(ItemReader)—數據讀取從普通文件,數據庫,XML,多文件數據讀取

(2)展示部分文件結構:

Spring-batch(ItemReader)—數據讀取從普通文件,數據庫,XML,多文件數據讀取

(3)編寫代碼:
AlipayTranDo :

package com.dhcc.batch.batchDemo.input.flatFile;

public class AlipayTranDo {
        private String tranId;
        private String channel;
        private String tranType;
        private String counterparty;
        private String goods;
        private String amount;
        private String isDebitCredit;
        private String state;

        public AlipayTranDo(String tranId, String channel, String tranType, String counterparty, String goods,
                String amount, String isDebitCredit, String state) {
            super();
            this.tranId = tranId;
            this.channel = channel;
            this.tranType = tranType;
            this.counterparty = counterparty;
            this.goods = goods;
            this.amount = amount;
            this.isDebitCredit = isDebitCredit;
            this.state = state;
        }

        public String getTranId() {
            return tranId;
        }

        public void setTranId(String tranId) {
            this.tranId = tranId;
        }

        public String getChannel() {
            return channel;
        }

        public void setChannel(String channel) {
            this.channel = channel;
        }

        public String getTranType() {
            return tranType;
        }

        public void setTranType(String tranType) {
            this.tranType = tranType;
        }

        public String getCounterparty() {
            return counterparty;
        }

        public void setCounterparty(String counterparty) {
            this.counterparty = counterparty;
        }

        public String getGoods() {
            return goods;
        }

        public void setGoods(String goods) {
            this.goods = goods;
        }

        public String getAmount() {
            return amount;
        }

        public void setAmount(String amount) {
            this.amount = amount;
        }

        public String getIsDebitCredit() {
            return isDebitCredit;
        }

        public void setIsDebitCredit(String isDebitCredit) {
            this.isDebitCredit = isDebitCredit;
        }

        public String getState() {
            return state;
        }

        public void setState(String state) {
            this.state = state;
        }

        @Override
        public String toString() {
            return "AlipayTranDO{" +
                    "tranId='" + tranId + '\'' +
                    ", channel='" + channel + '\'' +
                    ", tranType='" + tranType + '\'' +
                    ", counterparty='" + counterparty + '\'' +
                    ", goods='" + goods + '\'' +
                    ", amount='" + amount + '\'' +
                    ", isDebitCredit='" + isDebitCredit + '\'' +
                    ", state='" + state + '\'' +
                    '}';
        }
    }

AlipayTranDoFileMapper:

package com.dhcc.batch.batchDemo.input.flatFile;

import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;

public class AlipayTranDoFileMapper implements FieldSetMapper<AlipayTranDo> {

    @Override
    public AlipayTranDo mapFieldSet(FieldSet fieldSet) throws BindException {
        return new AlipayTranDo(fieldSet.readString("tranId")
                , fieldSet.readString("channel")
                ,fieldSet.readString("tranType")
                , fieldSet.readString("counterparty")
                , fieldSet.readString("goods")
                ,fieldSet.readString("amount")
                , fieldSet.readString("isDebitCredit")
                , fieldSet.readString("state")
                );
    }

}

FlatFileWriterDemo:

package com.dhcc.batch.batchDemo.input.flatFile;

import java.util.List;

import org.springframework.batch.item.ItemWriter;
import org.springframework.stereotype.Component;
@Component("FlatFileWriterDemo")
public class FlatFileWriterDemo implements ItemWriter<AlipayTranDo>{

    @Override
    public void write(List<? extends AlipayTranDo> items) throws Exception {
        for(AlipayTranDo alipayTranDo:items) {
            System.out.println(alipayTranDo);
        }
    }

}

InputFaltFileItemReaderConfigruation:

package com.dhcc.batch.batchDemo.input.flatFile;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

@Configuration
public class InputFaltFileItemReaderConfigruation {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    @Qualifier("FlatFileWriterDemo")
    private ItemWriter<? super AlipayTranDo> FlatFileWriterDemo;

    @Bean
    public Job FaltFileItemReaderJob() {
        return jobBuilderFactory.get("FaltFileItemReaderJob")
                .start(FaltFileItemReaderJobStep())
                .build();

    }

    @Bean
    public Step FaltFileItemReaderJobStep() {
        return stepBuilderFactory.get("FaltFileItemReaderJobStep")
                .<AlipayTranDo, AlipayTranDo>chunk(100)
                .reader(FaltFileReaderDemo())
                .writer(FlatFileWriterDemo)
                .build();
    }

    @Bean
    @StepScope
    public FlatFileItemReader<AlipayTranDo> FaltFileReaderDemo() {
        FlatFileItemReader<AlipayTranDo> reader=new FlatFileItemReader<AlipayTranDo>();
        reader.setResource(new ClassPathResource("/data/init/springbatchtest1.csv"));
        reader.setLinesToSkip(5);

        DelimitedLineTokenizer tokenizer=new DelimitedLineTokenizer();
        tokenizer.setNames(new String[] 
                {"tranId","channel","tranType","counterparty","goods","amount","isDebitCredit","state"}
        );
        DefaultLineMapper<AlipayTranDo> lineMapper=new DefaultLineMapper<AlipayTranDo>();
        lineMapper.setLineTokenizer(tokenizer);
        lineMapper.setFieldSetMapper(new AlipayTranDoFileMapper());
        lineMapper.afterPropertiesSet();
        reader.setLineMapper(lineMapper);
        return reader;
    }

}

InputItemReaderFileApplication:

package com.dhcc.batch.batchDemo.input.flatFile;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableBatchProcessing
public class InputItemReaderFileApplication {
    public static void main(String[] args) {
        SpringApplication.run(InputItemReaderFileApplication.class, args);

    }
}

運行結果:
Spring-batch(ItemReader)—數據讀取從普通文件,數據庫,XML,多文件數據讀取

四.使用ItemReader從xml文件中讀取數據
1.使用StaxEventItemReader<T>讀取xml數據
2.例:在項目中加入一個weather.xml文件,以讀取此文件爲例
觀察xml文件數據結構:

<datas>
<data>
<date>2018-10-08</date>
<icon>d07|n02</icon>
<weather>小雨轉陰</weather>
<temperature>19/9℃</temperature>
<winddirect>西南風</winddirect>
</data>
<data>
<date>2018-10-09</date>
<icon>d01|n00</icon>
<weather>多雲轉晴</weather>
<temperature>21/7℃</temperature>
<winddirect>東北風</winddirect>
</data>
<data>
<date>2018-10-10</date>
<icon>d00|n01</icon>
<weather>晴轉多雲</weather>
<temperature>20/8℃</temperature>
<winddirect>東北風3-4級轉</winddirect>
</data>
<data>
<date>2018-10-11</date>
<icon>d01|n01</icon>
<weather>多雲</weather>
<temperature>18/9℃</temperature>
<winddirect>東北風</winddirect>
</data>
<data>
<date>2018-10-12</date>
<icon>d00|n02</icon>
<weather>晴轉陰</weather>
<temperature>18/12℃</temperature>
<winddirect>東北風</winddirect>
</data>
<data>
<date>2018-10-13</date>
<icon>d02|n02</icon>
<weather>陰</weather>
<temperature>18/13℃</temperature>
<winddirect>北風轉南風</winddirect>
</data>
<data>
<date>2018-10-14</date>
<icon>d01|n02</icon>
<weather>多雲轉陰</weather>
<temperature>17/11℃</temperature>
<winddirect>東北風3-4級轉</winddirect>
</data>
</datas>
使用spring batch讀取xml文件數據代碼如下:
WeatherData:

package com.dhcc.batch.batchDemo.input.xmlFile;

public class WeatherData {
    private String date;
    private String icon;
    private String weather;
    private String temperature;
    private String winddirect;

    public WeatherData() {
        super();
    }

    public WeatherData(String date, String icon, String weather, String temperature, String winddirect) {
        super();
        this.date = date;
        this.icon = icon;
        this.weather = weather;
        this.temperature = temperature;
        this.winddirect = winddirect;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getWeather() {
        return weather;
    }

    public void setWeather(String weather) {
        this.weather = weather;
    }

    public String getTemperature() {
        return temperature;
    }

    public void setTemperature(String temperature) {
        this.temperature = temperature;
    }

    public String getWinddirect() {
        return winddirect;
    }

    public void setWinddirect(String winddirect) {
        this.winddirect = winddirect;
    }

    @Override
    public String toString() {
        return "WeatherData [date=" + date + ", icon=" + icon + ", weather=" + weather + ", temperature=" + temperature
                + ", winddirect=" + winddirect + "]";
    }

}

XMLFileDemoJobConfiguration :

package com.dhcc.batch.batchDemo.input.xmlFile;

import java.util.HashMap;
import java.util.Map;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.xml.StaxEventItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.oxm.xstream.XStreamMarshaller;

@Configuration
public class XMLFileDemoJobConfiguration {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    @Qualifier("XMLFileWriterDemo")
    private ItemWriter<? super WeatherData> XMLFileWriterDemo;

    @Bean
    public Job XMLFileDemoReaderJobTwo() {
        return jobBuilderFactory.get("XMLFileDemoReaderJobTwo")
                .start(XMLFileDemoReaderStepTwo())
                .build();

    }

    @Bean
    public Step XMLFileDemoReaderStepTwo() {
        return stepBuilderFactory.get("XMLFileDemoReaderStepTwo")
                .<WeatherData,WeatherData>chunk(2)
                .reader(XMLFileItemReader())
                .writer(XMLFileWriterDemo)
                .build();
    }

    @Bean
    @StepScope
    public StaxEventItemReader<WeatherData> XMLFileItemReader() {
        StaxEventItemReader<WeatherData> reader=new StaxEventItemReader<WeatherData>();
        reader.setResource(new ClassPathResource("/xdata/init/weather.xml"));
        reader.setFragmentRootElementName("data");
        XStreamMarshaller unmarshaller=new XStreamMarshaller();
        Map<String,Class> map=new HashMap<>();
        map.put("data", WeatherData.class);
        unmarshaller.setAliases(map);
        reader.setUnmarshaller(unmarshaller);//序列化
        return reader;

    }

}

XMLFileWriterDemo :

package com.dhcc.batch.batchDemo.input.xmlFile;

import java.util.List;

import org.springframework.batch.item.ItemWriter;
import org.springframework.stereotype.Component;
@Component("XMLFileWriterDemo")
public class XMLFileWriterDemo implements ItemWriter<WeatherData>{

    @Override
    public void write(List<? extends WeatherData> items) throws Exception {
        for(WeatherData weatherInfo:items) {
            System.out.println(weatherInfo);
        }
    }

}

XmlFileReaderApplication :

package com.dhcc.batch.batchDemo.input.xmlFile;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableBatchProcessing
public class XmlFileReaderApplication {

    public static void main(String[] args) {
        SpringApplication.run(XmlFileReaderApplication.class, args);
    }
}

運行結果:
Spring-batch(ItemReader)—數據讀取從普通文件,數據庫,XML,多文件數據讀取

觀察結果可得,我們成功的讀取了xml文件
五.從多個文件讀取數據
1.在一個給定的目錄下一次讀取多個文件時非常常見的
2.我們可以使用MultiResourceItemReader來註冊一個input file並且設置代理的ItemReader去處理每一個源文件
例:我們在項目中同時存放兩個csv文件,如下所示:
Spring-batch(ItemReader)—數據讀取從普通文件,數據庫,XML,多文件數據讀取

我們以讀取這兩個文件爲例:
代碼:
實體類與上面例子實體類一樣AlipaytranDo,不做展示
AlipayTranDoMutipleMapper :

package com.dhcc.batch.batchDemo.input.mutiple;

import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;

public class AlipayTranDoMutipleMapper implements FieldSetMapper<AlipayTranDo> {

    @Override
    public AlipayTranDo mapFieldSet(FieldSet fieldSet) throws BindException {
        return new AlipayTranDo(fieldSet.readString("tranId")
                , fieldSet.readString("channel")
                ,fieldSet.readString("tranType")
                , fieldSet.readString("counterparty")
                , fieldSet.readString("goods")
                ,fieldSet.readString("amount")
                , fieldSet.readString("isDebitCredit")
                , fieldSet.readString("state")
                );
    }

}

MutipleFileDemoReaderApplication :

package com.dhcc.batch.batchDemo.input.mutiple;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableBatchProcessing
public class MutipleFileDemoReaderApplication {

    public static void main(String[] args) {
        SpringApplication.run(MutipleFileDemoReaderApplication.class, args);
    }
}

MutipleFileDemoReaderConfiguration :

package com.dhcc.batch.batchDemo.input.mutiple;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.MultiResourceItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;

@Configuration
public class MutipleFileDemoReaderConfiguration {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    @Qualifier("MutipleFileItemWriterDemo")
    private ItemWriter<? super AlipayTranDo> MutipleFileItemWriterDemo;

    @Value("classpath*:/data/init/springbatchtest*.csv")
    private Resource[] resources;

    @Bean
    public Job MutipleFileDemoReaderJob3() {
        return jobBuilderFactory.get("MutipleFileDemoReaderJob3")
                .start(MutipleFileDemoItemReaderJobStep3())
                .build();

    }

    @Bean
    public Step MutipleFileDemoItemReaderJobStep3() {
        return stepBuilderFactory.get("MutipleFileDemoItemReaderJobStep3")
                .<AlipayTranDo, AlipayTranDo>chunk(100)
                .reader(MutipleResourceItemReaderDemo())
                .writer(MutipleFileItemWriterDemo)
                .build();
    }

    @Bean
    @StepScope
    public MultiResourceItemReader<AlipayTranDo> MutipleResourceItemReaderDemo() {
        MultiResourceItemReader<AlipayTranDo> reader=new MultiResourceItemReader<AlipayTranDo>();
        reader.setDelegate(FaltFileReaderDemo());
        reader.setResources(resources);
        return reader;
    }

    @Bean
    @StepScope
    public FlatFileItemReader<AlipayTranDo> FaltFileReaderDemo() {
        FlatFileItemReader<AlipayTranDo> reader=new FlatFileItemReader<AlipayTranDo>();
//      reader.setResource(new ClassPathResource("alipayTranDo"));
        reader.setLinesToSkip(5);

        DelimitedLineTokenizer tokenizer=new DelimitedLineTokenizer();
        tokenizer.setNames(new String[] 
                {"tranId","channel","tranType","counterparty","goods","amount","isDebitCredit","state"}
        );
        DefaultLineMapper<AlipayTranDo> lineMapper=new DefaultLineMapper<AlipayTranDo>();
        lineMapper.setLineTokenizer(tokenizer);
        lineMapper.setFieldSetMapper(new AlipayTranDoMutipleMapper());
        lineMapper.afterPropertiesSet();
        reader.setLineMapper(lineMapper);
        return reader;
    }

}

MutipleFileItemWriterDemo :

package com.dhcc.batch.batchDemo.input.mutiple;

import java.util.List;

import org.springframework.batch.item.ItemWriter;
import org.springframework.stereotype.Component;
@Component("MutipleFileItemWriterDemo")
public class MutipleFileItemWriterDemo implements ItemWriter<AlipayTranDo>{

    @Override
    public void write(List<? extends AlipayTranDo> items) throws Exception {
        for(AlipayTranDo alipayTranDo:items) {
            System.out.println(alipayTranDo);
        }
    }

}

六.ItemReader異常處理及重啓
1.對於chunk類型的Step,spring batch爲我們提供了用於管理它的狀態
2.狀態的管理是通過ItemStream接口來實現的
3.ItemStream接口:
(1)open():每一次step執行會調用
(2)Update():每一個chunk去執行都會調用
(3)Close():所有的chunk執行完畢會調用
4.圖形:
Spring-batch(ItemReader)—數據讀取從普通文件,數據庫,XML,多文件數據讀取

此處不再展示例子

最後附上項目pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.dhcc.batch</groupId>
    <artifactId>batchDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>batchDemo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-oxm -->
        <!-- 用於讀取xml文件序列化 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>5.0.8.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.10</version>
        </dependency>

        <!-- 內存數據庫h2 <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> 
            </dependency> -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章