1 介绍
用Spring Batch实现了个简单的需求,从MySQL中读取用户表数据,根据生日计算年龄,将结果输出到csv文件。
1.1 准备表及数据
user test;
DROP TABLE IF EXISTS `test_user`;
CREATE TABLE `test_user` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(45) NOT NULL default '',
`birthday` datetime default NULL,
`age` int default 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
INSERT INTO test_user(name, birthday, age) VALUES('小明', '1993-03-09', 0);
INSERT INTO test_user(name, birthday, age) VALUES('Jack', '1973-03-09', 0);
INSERT INTO test_user(name, birthday, age) VALUES('Tom', '1963-03-09', 0);
INSERT INTO test_user(name, birthday, age) VALUES('齐天大圣', '1983-03-09', 0);
INSERT INTO test_user(name, birthday, age) VALUES('星知', '2003-03-09', 0);
2 实现
2.1 项目目录
2.2 实体类
public class TestUser {
private Integer id;
private String name;
private Date birthday;
private Integer age;
// set/get...
}
2.3 ItemReader
TestUserConfig.java
@Bean
public JdbcCursorItemReader<TestUser> itemReader(){
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://mysql-server:3306/test");
dataSource.setUsername("r00t");
dataSource.setPassword("r00t");
JdbcCursorItemReader<TestUser> reader = new JdbcCursorItemReader<TestUser>();
reader.setDataSource(dataSource);
reader.setSql("SELECT id, name, birthday FROM test_user");
reader.setRowMapper(new UserRowMapper());
return reader;
}
2.4 ItemProcessor
TestUserConfig.java
@Bean
public TestUserItemProcessor itemProcessor() {
return new TestUserItemProcessor();
}
TestUserItemProcessor.java
public class TestUserItemProcessor implements ItemProcessor<TestUser, TestUser> {
@Override
public TestUser process(TestUser testUser) throws Exception {
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(testUser.getBirthday());
testUser.setAge(cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR));
return testUser;
}
}
2.5 ItemWriter
TestUserConfig.java
@Bean
public FlatFileItemWriter<TestUser> itemWriter() {
FlatFileItemWriter<TestUser> itemWriter = new FlatFileItemWriter<>();
String userHome = System.getProperty("user.home");
Resource outputResource = new FileSystemResource(userHome + "/output/demo04/test_user.csv");
itemWriter.setResource(outputResource);
itemWriter.setLineAggregator(new DelimitedLineAggregator<TestUser>() {{
setDelimiter(",");
setFieldExtractor(new BeanWrapperFieldExtractor<TestUser>() {{
setNames(new String[] { "id", "name", "age" });
}});
}});
return itemWriter;
}
2.6 Job & Step
TestUserConfig.java
@Bean
public Step step1(JdbcCursorItemReader<TestUser> itemReader, TestUserItemProcessor itemProcessor, FlatFileItemWriter<TestUser> itemWriter) {
return steps.get("step1").<TestUser, TestUser> chunk(10)
.reader(itemReader)
.processor(itemProcessor)
.writer(itemWriter)
.build();
}
@Bean
public Job job1(Step step1) {
return jobs.get("job1")
.incrementer(new RunIdIncrementer())
.flow(step1)
.end()
.build();
}
2.7 测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestUserConfig.class})
public class TestUserTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
public void givenTaskletsJob_whenJobEnds_thenStatusCompleted() throws Exception {
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
}
}
2.8 验证
在用户目录下生成 C:Users{your-name}outputdemo04 est_user.csv
,文件内容如下
6,小明,25
7,Jack,45
8,Tom,55
9,齐天大圣,35
10,星知,15
3 总结
通过本例可了解Java配置SringBatch,读取MySQL及写入CSV等。本例完整实现 spring-batch