📜  Spring Batch-读者,作家和处理器(1)

📅  最后修改于: 2023-12-03 15:05:15.646000             🧑  作者: Mango

Spring Batch: Reader, Writer and Processor

Spring Batch is a framework for batch processing in Java. It provides components for reading, processing and writing large volumes of data in batches. The three main components of Spring Batch are:

  1. Reader: Reads data from a specified source and creates a Java object or a POJO for each record. Spring Batch provides different readers for different data sources such as CSV files, databases, XML files, etc.

  2. Processor: Processes the data read from the reader and transforms it into a new format. Processors are optional and can be omitted if a simple conversion is not required.

  3. Writer: Writes the processed data into the specified destination. Spring Batch provides different writers for different output formats such as CSV files, databases, XML files, etc.

Reader

Spring Batch provides different readers for different data sources. Some of the readers are:

  1. FlatFileItemReader: Reads data from a CSV or flat file. It provides various configurations for defining the separator, header and footer lines, etc.
@Bean
public FlatFileItemReader<Person> reader() {
    FlatFileItemReader<Person> reader = new FlatFileItemReader<>();
    reader.setResource(new ClassPathResource("people.csv"));
    reader.setLineMapper(new DefaultLineMapper<Person>() {{
        setLineTokenizer(new DelimitedLineTokenizer() {{
            setNames(new String[] {"firstName", "lastName"});
        }});
        setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
            setTargetType(Person.class);
        }});
    }});
    return reader;
}
  1. JdbcCursorItemReader: Reads data from a database using a JDBC cursor. It provides various configurations for defining the SQL query, parameter values, etc.
@Bean
public JdbcCursorItemReader<Person> reader(DataSource dataSource) {
    JdbcCursorItemReader<Person> reader = new JdbcCursorItemReader<>();
    reader.setSql("SELECT first_name, last_name FROM people");
    reader.setDataSource(dataSource);
    reader.setRowMapper(new BeanPropertyRowMapper<>(Person.class));
    return reader;
}
Processor

Processors are optional and can be omitted if a simple conversion is not required. If a processor is defined, it receives the input object from the reader, processes it and returns the output object.

@Bean
public PersonItemProcessor processor() {
    return new PersonItemProcessor();
}
public class PersonItemProcessor implements ItemProcessor<Person, Person> {

    @Override
    public Person process(Person person) throws Exception {
        final String firstName = person.getFirstName().toUpperCase();
        final String lastName = person.getLastName().toUpperCase();

        final Person transformedPerson = new Person(firstName, lastName);

        return transformedPerson;
    }

}
Writer

Spring Batch provides different writers for different output formats. Some of the writers are:

  1. FlatFileItemWriter: Writes data to a CSV or flat file. It provides various configurations for defining the separator, header and footer lines, etc.
@Bean
public FlatFileItemWriter<Person> writer() {
    FlatFileItemWriter<Person> writer = new FlatFileItemWriter<>();
    writer.setResource(new FileSystemResource("output.txt"));
    writer.setLineAggregator(new DelimitedLineAggregator<Person>() {{
        setDelimiter(",");
        setFieldExtractor(new BeanWrapperFieldExtractor<Person>() {{
            setNames(new String[] {"firstName", "lastName"});
        }});
    }});
    return writer;
}
  1. JdbcBatchItemWriter: Writes data to a database using batch updates.
@Bean
public JdbcBatchItemWriter<Person> writer(JdbcTemplate jdbcTemplate) {
    JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<>();
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
    writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
    writer.setDataSource(jdbcTemplate.getDataSource());
    return writer;
}
Conclusion

In this article, we have discussed the three main components of Spring Batch: Reader, Processor and Writer. We have also seen some examples of different readers, processors and writers provided by Spring Batch. With these components, we can easily read, process and write large volumes of data in batches.