Spring-Batch处理MySQL数据后存到CSV文件

Posted okokabcd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring-Batch处理MySQL数据后存到CSV文件相关的知识,希望对你有一定的参考价值。

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}\output\demo04\test_user.csv ,文件内容如下

6,小明,25
7,Jack,45
8,Tom,55
9,齐天大圣,35
10,星知,15

3 总结

通过本例可了解Java配置SringBatch,读取MySQL及写入CSV等。本例完整实现 spring-batch

以上是关于Spring-Batch处理MySQL数据后存到CSV文件的主要内容,如果未能解决你的问题,请参考以下文章

将一个字符串按分隔符分隔开后存到一个表变量中

用同一个js的MD5加密文件对密码进行加密,注册的密码加密后存到数据库里的数据怎么跟登录时不一样

spring-batch入门

spring-batch (ItemProcessor) 数据处理过程

php单线程爬虫类

Spring-Batch学习总结——重要概念,环境搭建,名词解释,第一个项目及异常处理