您好,登錄后才能下訂單哦!
Spring-Batch學(xué)習(xí)總結(jié)(3)——如何數(shù)據(jù)輸入
一.ItemReader概述
1.ItemReader:提供數(shù)據(jù)的接口
2.在這個(gè)接口中只有一個(gè)方法read(),它讀取一個(gè)數(shù)據(jù)并且移動(dòng)到下一個(gè)數(shù)據(jù)上去,在讀取結(jié)束時(shí)必須返回一個(gè)null,否則表明數(shù)據(jù)沒有讀取完畢;
例:
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;
}
}
}
運(yùn)行結(jié)果:
二.從數(shù)據(jù)庫中讀取數(shù)據(jù)
1.在實(shí)際應(yīng)用中,我們都需要從數(shù)據(jù)庫中讀取數(shù)據(jù),并且進(jìn)行分頁讀取,在spring-batch中為我們提供了JDBCPagingItemReader這個(gè)類進(jìn)行數(shù)據(jù)庫數(shù)據(jù)讀取
2.例:再舉這個(gè)例子之前我們?cè)跀?shù)據(jù)庫中建立了個(gè)person_buf表,并向表中插入了100001條數(shù)據(jù)
接下來我們讀取這個(gè)表中的數(shù)據(jù)為例,進(jìn)行學(xué)習(xí):
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); // 設(shè)置數(shù)據(jù)源
reader.setFetchSize(100); // 設(shè)置一次最大讀取條數(shù)
reader.setRowMapper(new PersonRowMapper()); // 把數(shù)據(jù)庫中的每條數(shù)據(jù)映射到Person對(duì)中
MySqlPagingQueryProvider queryProvider = new MySqlPagingQueryProvider();
queryProvider.setSelectClause("id,name,per_desc,create_time,update_time,sex,score,price"); // 設(shè)置查詢的列
queryProvider.setFromClause("from person_buf"); // 設(shè)置要查詢的表
Map<String, Order> sortKeys = new HashMap<String, Order>();// 定義一個(gè)集合用于存放排序列
sortKeys.put("id", Order.ASCENDING);// 按照升序排序
queryProvider.setSortKeys(sortKeys);
reader.setQueryProvider(queryProvider);// 設(shè)置排序列
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;
/**
* 實(shí)現(xiàn)將數(shù)據(jù)庫中的每條數(shù)據(jù)映射到Person對(duì)象中
* @author Administrator
*
*/
public class PersonRowMapper implements RowMapper<Person> {
/**
* rs一條結(jié)果集,rowNum代表當(dāng)前行
*/
@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"));
}
}
運(yùn)行結(jié)果:
1.FlatFileItemReader:
(1)set lines to skip:設(shè)置跳過前幾行
(2)set resource:指定源文件地址
(3)設(shè)置一個(gè)行解析器映射行對(duì)應(yīng)的結(jié)果集
2.例:我們?cè)陧?xiàng)目中的resources中放入了兩個(gè)csv文件,我們以讀取springbatchtest1.csv為例:
(1)觀察文件存放路徑:
(2)展示部分文件結(jié)構(gòu):
(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);
}
}
運(yùn)行結(jié)果:
四.使用ItemReader從xml文件中讀取數(shù)據(jù)
1.使用StaxEventItemReader<T>讀取xml數(shù)據(jù)
2.例:在項(xiàng)目中加入一個(gè)weather.xml文件,以讀取此文件為例
觀察xml文件數(shù)據(jù)結(jié)構(gòu):
<datas>
<data>
<date>2018-10-08</date>
<icon>d07|n02</icon>
<weather>小雨轉(zhuǎn)陰</weather>
<temperature>19/9℃</temperature>
<winddirect>西南風(fēng)</winddirect>
</data>
<data>
<date>2018-10-09</date>
<icon>d01|n00</icon>
<weather>多云轉(zhuǎn)晴</weather>
<temperature>21/7℃</temperature>
<winddirect>東北風(fēng)</winddirect>
</data>
<data>
<date>2018-10-10</date>
<icon>d00|n01</icon>
<weather>晴轉(zhuǎn)多云</weather>
<temperature>20/8℃</temperature>
<winddirect>東北風(fēng)3-4級(jí)轉(zhuǎn)</winddirect>
</data>
<data>
<date>2018-10-11</date>
<icon>d01|n01</icon>
<weather>多云</weather>
<temperature>18/9℃</temperature>
<winddirect>東北風(fēng)</winddirect>
</data>
<data>
<date>2018-10-12</date>
<icon>d00|n02</icon>
<weather>晴轉(zhuǎn)陰</weather>
<temperature>18/12℃</temperature>
<winddirect>東北風(fēng)</winddirect>
</data>
<data>
<date>2018-10-13</date>
<icon>d02|n02</icon>
<weather>陰</weather>
<temperature>18/13℃</temperature>
<winddirect>北風(fēng)轉(zhuǎn)南風(fēng)</winddirect>
</data>
<data>
<date>2018-10-14</date>
<icon>d01|n02</icon>
<weather>多云轉(zhuǎn)陰</weather>
<temperature>17/11℃</temperature>
<winddirect>東北風(fēng)3-4級(jí)轉(zhuǎn)</winddirect>
</data>
</datas>
使用spring batch讀取xml文件數(shù)據(jù)代碼如下:
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);
}
}
運(yùn)行結(jié)果:
觀察結(jié)果可得,我們成功的讀取了xml文件
五.從多個(gè)文件讀取數(shù)據(jù)
1.在一個(gè)給定的目錄下一次讀取多個(gè)文件時(shí)非常常見的
2.我們可以使用MultiResourceItemReader來注冊(cè)一個(gè)input file并且設(shè)置代理的ItemReader去處理每一個(gè)源文件
例:我們?cè)陧?xiàng)目中同時(shí)存放兩個(gè)csv文件,如下所示:
我們以讀取這兩個(gè)文件為例:
代碼:
實(shí)體類與上面例子實(shí)體類一樣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.對(duì)于chunk類型的Step,spring batch為我們提供了用于管理它的狀態(tài)
2.狀態(tài)的管理是通過ItemStream接口來實(shí)現(xiàn)的
3.ItemStream接口:
(1)open():每一次step執(zhí)行會(huì)調(diào)用
(2)Update():每一個(gè)chunk去執(zhí)行都會(huì)調(diào)用
(3)Close():所有的chunk執(zhí)行完畢會(huì)調(diào)用
4.圖形:
此處不再展示例子
最后附上項(xiàng)目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>
<!-- 內(nèi)存數(shù)據(jù)庫h3 <dependency> <groupId>com.h3database</groupId> <artifactId>h3</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>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。