📅  最后修改于: 2023-12-03 15:05:15.646000             🧑  作者: Mango
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:
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.
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.
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.
Spring Batch provides different readers for different data sources. Some of the readers are:
@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;
}
@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;
}
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;
}
}
Spring Batch provides different writers for different output formats. Some of the writers are:
@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;
}
@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;
}
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.