-
Notifications
You must be signed in to change notification settings - Fork 285
/
CsvFileToDatabaseJobConfig.java
125 lines (104 loc) · 5.58 KB
/
CsvFileToDatabaseJobConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package net.petrikainulainen.springbatch.csv.in;
import net.petrikainulainen.springbatch.common.LoggingStudentProcessor;
import net.petrikainulainen.springbatch.student.StudentDTO;
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.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.ItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.LineMapper;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.LineTokenizer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import javax.sql.DataSource;
/**
* @author Petri Kainulainen
*/
@Configuration
public class CsvFileToDatabaseJobConfig {
private static final String PROPERTY_CSV_SOURCE_FILE_PATH = "csv.to.database.job.source.file.path";
private static final String QUERY_INSERT_STUDENT = "INSERT " +
"INTO students(email_address, name, purchased_package) " +
"VALUES (:emailAddress, :name, :purchasedPackage)";
@Bean
ItemReader<StudentDTO> csvFileItemReader(Environment environment) {
FlatFileItemReader<StudentDTO> csvFileReader = new FlatFileItemReader<>();
csvFileReader.setResource(new ClassPathResource(environment.getRequiredProperty(PROPERTY_CSV_SOURCE_FILE_PATH)));
csvFileReader.setLinesToSkip(1);
LineMapper<StudentDTO> studentLineMapper = createStudentLineMapper();
csvFileReader.setLineMapper(studentLineMapper);
return csvFileReader;
}
private LineMapper<StudentDTO> createStudentLineMapper() {
DefaultLineMapper<StudentDTO> studentLineMapper = new DefaultLineMapper<>();
LineTokenizer studentLineTokenizer = createStudentLineTokenizer();
studentLineMapper.setLineTokenizer(studentLineTokenizer);
FieldSetMapper<StudentDTO> studentInformationMapper = createStudentInformationMapper();
studentLineMapper.setFieldSetMapper(studentInformationMapper);
return studentLineMapper;
}
private LineTokenizer createStudentLineTokenizer() {
DelimitedLineTokenizer studentLineTokenizer = new DelimitedLineTokenizer();
studentLineTokenizer.setDelimiter(";");
studentLineTokenizer.setNames(new String[]{"name", "emailAddress", "purchasedPackage"});
return studentLineTokenizer;
}
private FieldSetMapper<StudentDTO> createStudentInformationMapper() {
BeanWrapperFieldSetMapper<StudentDTO> studentInformationMapper = new BeanWrapperFieldSetMapper<>();
studentInformationMapper.setTargetType(StudentDTO.class);
return studentInformationMapper;
}
@Bean
ItemProcessor<StudentDTO, StudentDTO> csvFileItemProcessor() {
return new LoggingStudentProcessor();
}
@Bean
ItemWriter<StudentDTO> csvFileDatabaseItemWriter(DataSource dataSource, NamedParameterJdbcTemplate jdbcTemplate) {
JdbcBatchItemWriter<StudentDTO> databaseItemWriter = new JdbcBatchItemWriter<>();
databaseItemWriter.setDataSource(dataSource);
databaseItemWriter.setJdbcTemplate(jdbcTemplate);
databaseItemWriter.setSql(QUERY_INSERT_STUDENT);
ItemSqlParameterSourceProvider<StudentDTO> sqlParameterSourceProvider = studentSqlParameterSourceProvider();
databaseItemWriter.setItemSqlParameterSourceProvider(sqlParameterSourceProvider);
return databaseItemWriter;
}
private ItemSqlParameterSourceProvider<StudentDTO> studentSqlParameterSourceProvider() {
return new BeanPropertyItemSqlParameterSourceProvider<>();
}
@Bean
Step csvFileToDatabaseStep(ItemReader<StudentDTO> csvFileItemReader,
ItemProcessor<StudentDTO, StudentDTO> csvFileItemProcessor,
ItemWriter<StudentDTO> csvFileDatabaseItemWriter,
StepBuilderFactory stepBuilderFactory) {
return stepBuilderFactory.get("csvFileToDatabaseStep")
.<StudentDTO, StudentDTO>chunk(1)
.reader(csvFileItemReader)
.processor(csvFileItemProcessor)
.writer(csvFileDatabaseItemWriter)
.build();
}
@Bean
Job csvFileToDatabaseJob(JobBuilderFactory jobBuilderFactory,
@Qualifier("csvFileToDatabaseStep") Step csvStudentStep) {
return jobBuilderFactory.get("csvFileToDatabaseJob")
.incrementer(new RunIdIncrementer())
.flow(csvStudentStep)
.end()
.build();
}
}