数据写入——写入到txt、csv等平面文件

spring batch读取完数据,然后执行process,执行完毕开始写入文件,本章以写入txt、csv等平面文件为例。
Spring batch提供了FlatFileItemWriter类来实现写入文件时的配置。
示例:读取下面User.xml文件,然后把xml中数据写入UserWrite.txt文件中。


<root>
    <user>
        <id>1id>
        <name>lzjname>
        <age>28age>
    user>
    <user>
        <id>2id>
        <name>Tomname>
        <age>20age>
    user>
    <user>
        <id>3id>
        <name>Jerryname>
        <age>18age>
    user>
    <user>
        <id>4id>
        <name>Jerryname>
        <age>18age>
    user>
    <user>
        <id>5id>
        <name>Bobname>
        <age>25age>
    user>
    <user>
        <id>5id>
        <name>Ferryname>
        <age>30age>
    user>
    <user>
        <id>6id>
        <name>Marryname>
        <age>39age>
    user>
    <user>
        <id>7id>
        <name>Lindaname>
        <age>20age>
    user>
    <user>
        <id>8id>
        <name>Lindaname>
        <age>23age>
    user>
    <user>
        <id>9id>
        <name>Jikenname>
        <age>15age>
    user>
root>

在读取xml文件时,把文件中的标签映射到工程中的User对象中,然后把对象中数据输入到txt文件中。该作业配置如下:

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.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.transform.LineAggregator;
import org.springframework.batch.item.xml.StaxEventItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.oxm.xstream.XStreamMarshaller;

import com.lzj.xml.reader.User;

@Configuration
public class BatchConfig1 {

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    private int chunkSize = 5;

    /*1、创建一个Job作业*/
    @Bean
    public Job xmlReaderJob() throws Exception{
        return jobBuilderFactory.get("xmlReaderJob")
        .start(chunkStep())
        .build();
    }

    //2、创建一个step*/
    @Bean
    public Step chunkStep() throws Exception{
        return stepBuilderFactory.get("chunkStep")
                .chunk(chunkSize)                                   //每chunkSize次提交一次
                .reader(xmlItemReader())                            //读取xml文件,并把文件中每个标签中数据映射到工程中的User bean中
                .writer(userWriteItem())
                .allowStartIfComplete(true)
                .build();
    }

    //3、配置要读取文件的特性*/
    @Bean
    public ItemReader xmlItemReader(){
        StaxEventItemReader reader = new StaxEventItemReader<>(); //StaxEventItemReader用来读取xml文件
        reader.setResource(new ClassPathResource("/data/User.xml"));    //设置xml文件位置
        reader.setFragmentRootElementName("user");  //指定xml文件的根元素,或者在User.java类上用@XmlRootElement(name = "user")
        reader.setUnmarshaller(getMarShaller());    //把xml文件中数据映射到User.java中
        return reader;
    }

    private XStreamMarshaller getMarShaller(){
        XStreamMarshaller marShaller = new XStreamMarshaller();
        Map map = new HashMap<>();
        map.put("user", User.class); //把标签中数据映射到User.class类中
        marShaller.setAliases(map);
        return marShaller;
    }


    //4、配置写
    @Bean
    public ItemWriter userWriteItem() throws Exception{
        FlatFileItemWriter fileWrite = new FlatFileItemWriter<>();
        fileWrite.setLineAggregator(new UserLineAggregator());  //设置数据聚合器,数据按什么格式写入文件
        String workFolder = System.getProperty("user.dir");     //获取工程目录
        fileWrite.setResource(new FileSystemResource(workFolder.concat("/output/UserWriter.txt")));
        fileWrite.afterPropertiesSet();     //设置完参数后进行校验,参数设置不正确时会抛异常
        return fileWrite;
    }

    /*按指定格式写入文件*/
    private class UserLineAggregator implements LineAggregator{
        @Override
        public String aggregate(User item) {
            return item.getId() + ", " + item.getName() + ", " + item.getAge();
        }

    }
}

运行该job作业,会在该工程目录下生成/output/UserWrite.txt文件,其内容如下:

1, lzj, 28
2, Tom, 20
3, Jerry, 18
4, Jerry, 18
5, Bob, 25
5, Ferry, 30
6, Marry, 39
7, Linda, 20
8, Linda, 23
9, Jiken, 15

你可能感兴趣的:(spring,batch)