springbatch-ItemReader使用
从数据库中读取数据
@Configuration
public class ItemReaderDbDemo {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private DataSource dataSource;
@Bean
public Job itemReaderDbJob(){
return jobBuilderFactory
.get("itemReaderDbJob")
.start(itemReaderDbStep())
.build();
}
@Bean
public Step itemReaderDbStep(){
return stepBuilderFactory
.get("itemReaderDbStep")
.<User,User>chunk(2)
.reader(dbJdbcReader())
.writer(new ItemWriter<User>() {
@Override
public void write(List<? extends User> list) throws Exception {
System.out.println(list);
}
})
.build();
}
@Bean
@StepScope
public JdbcPagingItemReader<User> dbJdbcReader(){
JdbcPagingItemReader<User> reader = new JdbcPagingItemReader<>();
reader.setDataSource(dataSource);//设置数据源
reader.setFetchSize(2);//一次去多少记录
//把读取到的记录转换成user对象
reader.setRowMapper(new RowMapper<User>() {
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User user = new User();
user.setId(rs.getInt(1));
user.setName(rs.getString(2));
user.setPass(rs.getString(3));
return user;
}
});
//指定sql语句
MySqlPagingQueryProvider provider = new MySqlPagingQueryProvider();
provider.setSelectClause("id,name,pass");//查哪些字段
provider.setFromClause("from t_user");//从哪个表查
//排序
Map<String, Order> sort = new HashMap<>(1);
sort.put("id",Order.DESCENDING);//排序字段,升序还是降序
provider.setSortKeys(sort);
reader.setQueryProvider(provider);
return reader;
}
}
从文件中读取数据
@Configuration
public class FileItemReaderDemo {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public Job fileItemReaderDemoJob(){
return jobBuilderFactory
.get("fileItemReaderDemoJob")
.start(fileItemReaderDemoStep())
.build();
}
@Bean
public Step fileItemReaderDemoStep(){
return stepBuilderFactory
.get("fileItemReaderDemoStep")
.<User,User>chunk(2)
.reader(flatFileItemReader())
.writer(System.out::println)
.build();
}
@Bean
@StepScope
public FlatFileItemReader<User> flatFileItemReader(){
FlatFileItemReader<User> reader = new FlatFileItemReader<>();
reader.setResource(new ClassPathResource("users.txt"));//文件地址
reader.setLinesToSkip(1);//跳过第一行
//解析数据
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
tokenizer.setNames(new String[]{"id","name","pass"});//指明表头字段
//把解析的一行数据映射为对象
DefaultLineMapper<User> mapper = new DefaultLineMapper<>();
mapper.setLineTokenizer(tokenizer);
mapper.setFieldSetMapper(new FieldSetMapper<User>() {
@Override
public User mapFieldSet(FieldSet fieldSet) throws BindException {
User user = new User();
user.setId(fieldSet.readInt("id"));
user.setName(fieldSet.readString("name"));
user.setPass(fieldSet.readString("pass"));
return user;
}
});
mapper.afterPropertiesSet();
reader.setLineMapper(mapper);
return reader;
}
}
从xml中读取数据
@Configuration
public class XmlItemReaderDemo {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public Job xmlItemReaderDemoJob(){
return jobBuilderFactory
.get("xmlItemReaderDemoJob")
.start(xmlItemReaderDemoStep())
.build();
}
@Bean
public Step xmlItemReaderDemoStep(){
return stepBuilderFactory
.get("xmlItemReaderDemoStep")
.<User,User>chunk(2)
.reader(xmlFileReader())
.writer(System.out::println)
.build();
}
@Bean
@StepScope
public StaxEventItemReader<User> xmlFileReader(){
StaxEventItemReader<User> reader = new StaxEventItemReader<>();
reader.setResource(new ClassPathResource("users.xml"));//从哪个文件读
//设置需要处理的根标签
reader.setFragmentRootElementName("user");
//把xml转成对象
XStreamMarshaller marshaller = new XStreamMarshaller();
//设置类型转换
Map<String,Class> map = new HashMap<>();
map.put("user",User.class);//标签名,类型
marshaller.setAliases(map);
reader.setUnmarshaller(marshaller);
return reader;
}
}
需要引入依赖:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>5.3.10</version> </dependency> <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.4.7</version> </dependency>
读取多个文件的数据
@Configuration
public class MultiFileItemReaderDemo {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Value("classpath:/user*.txt")
private Resource[] fileResources;
@Bean
public Job multiFileItemReaderDemoJob(){
return jobBuilderFactory
.get("multiFileItemReaderDemoJob")
.start(multiFileItemReaderDemoStep())
.build();
}
@Bean
public Step multiFileItemReaderDemoStep(){
return stepBuilderFactory
.get("multiFileItemReaderDemoStep")
.<User,User>chunk(2)
.reader(multiFileReader())
.writer(System.out::println)
.build();
}
@Bean
@StepScope
public MultiResourceItemReader<User> multiFileReader(){
MultiResourceItemReader<User> reader = new MultiResourceItemReader<>();
reader.setDelegate(flatFileItemReader());
reader.setResources(fileResources);//设置文件源
return reader;
}
//从一个文件中读取
@Bean
@StepScope
public FlatFileItemReader<User> flatFileItemReader(){
FlatFileItemReader<User> reader = new FlatFileItemReader<>();
//解析数据
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
tokenizer.setNames(new String[]{"id","name","pass"});//指明表头字段
//把解析的一行数据映射为对象
DefaultLineMapper<User> mapper = new DefaultLineMapper<>();
mapper.setLineTokenizer(tokenizer);
mapper.setFieldSetMapper(new FieldSetMapper<User>() {
@Override
public User mapFieldSet(FieldSet fieldSet) throws BindException {
User user = new User();
user.setId(fieldSet.readInt("id"));
user.setName(fieldSet.readString("name"));
user.setPass(fieldSet.readString("pass"));
return user;
}
});
mapper.afterPropertiesSet();
reader.setLineMapper(mapper);
return reader;
}
}