Step 1: Project Setup
Create a new Maven project in Eclipse and add the following dependencies to your pom.xml
file:
1 2 3 4 5 6 7 8 9 10 |
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-batch</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> </dependencies> |
Step 2: Configure Batch
Create a configuration class (BatchConfiguration.java
) to set up Spring Batch:
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 |
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.ItemWriter; import org.springframework.batch.item.file.FlatFileItemReader; import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; import org.springframework.batch.item.file.transform.FieldSet; import org.springframework.batch.item.file.transform.LineTokenizer; import org.springframework.batch.item.file.mapping.FieldSetMapper; import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper; import org.springframework.batch.item.file.mapping.DefaultLineMapper; import org.springframework.batch.item.file.separator.DefaultRecordSeparatorPolicy; @Configuration @EnableBatchProcessing public class BatchConfiguration { @Bean public Job myJob(Step myStep) { return jobBuilderFactory.get("myJob") .start(myStep) .build(); } @Bean public Step myStep(ItemReader<MyInputData> reader, ItemProcessor<MyInputData, MyOutputData> processor, ItemWriter<MyOutputData> writer) { return stepBuilderFactory.get("myStep") .<MyInputData, MyOutputData>chunk(10) .reader(reader) .processor(processor) .writer(writer) .build(); } @Bean public FlatFileItemReader<MyInputData> reader() { return new FlatFileItemReaderBuilder<MyInputData>() .name("myItemReader") .resource(new ClassPathResource("input.csv")) .lineTokenizer(lineTokenizer()) .fieldSetMapper(new BeanWrapperFieldSetMapper<MyInputData>() {{ setTargetType(MyInputData.class); }}) .build(); } @Bean public LineTokenizer lineTokenizer() { DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer(); lineTokenizer.setNames("id", "name", "age"); lineTokenizer.setDelimiter(","); return lineTokenizer; } @Bean public ItemProcessor<MyInputData, MyOutputData> processor() { return new MyItemProcessor(); } @Bean public ItemWriter<MyOutputData> writer() { return items -> { for (MyOutputData item : items) { System.out.println("Writing item: " + item); } }; } } |
Step 3: Input and Output Classes
Create input and output classes (MyInputData.java
and MyOutputData.java
) to represent the data read from the input file and the processed data to be written:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class MyInputData { private Long id; private String name; private int age; // Getters and setters } public class MyOutputData { private Long id; private String processedName; private int doubledAge; // Getters and setters } |
Step 4: Processor
Implement the ItemProcessor
interface (MyItemProcessor.java
) to define the processing logic:
1 2 3 4 5 6 7 8 9 10 11 |
public class MyItemProcessor implements ItemProcessor<MyInputData, MyOutputData> { @Override public MyOutputData process(MyInputData item) throws Exception { MyOutputData processedData = new MyOutputData(); processedData.setId(item.getId()); processedData.setProcessedName(item.getName().toUpperCase()); processedData.setDoubledAge(item.getAge() * 2); return processedData; } } |
Step 5: Input CSV File
Create a CSV file (input.csv
) with sample data:
1 2 3 4 |
id,name,age 1,John,25 2,Jane,30 3,Bob,22 |
Step 6: Run the Application
Create a main class (BatchApplication.java
) to run the application:
1 2 3 4 5 6 7 |
@SpringBootApplication public class BatchApplication { public static void main(String[] args) { SpringApplication.run(BatchApplication.class, args); } } |
Run the application as a Java application, and Spring Batch will execute the defined job.