* 엑셀 reader 작성 완료
This commit is contained in:
@@ -0,0 +1,45 @@
|
|||||||
|
package com.example.springbatch.batch;
|
||||||
|
|
||||||
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
|
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
||||||
|
import org.springframework.batch.item.ItemReader;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
public class ExcelRowReader implements ItemReader<Row> {
|
||||||
|
|
||||||
|
private final String filePath;
|
||||||
|
private Iterator<Row> rowCursor;
|
||||||
|
|
||||||
|
public ExcelRowReader(String filePath) throws IOException {
|
||||||
|
|
||||||
|
this.filePath = filePath;
|
||||||
|
initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initialize() throws IOException {
|
||||||
|
|
||||||
|
FileInputStream fileInputStream = new FileInputStream(filePath);
|
||||||
|
Workbook workbook = WorkbookFactory.create(fileInputStream);
|
||||||
|
Sheet sheet = workbook.getSheetAt(0);
|
||||||
|
this.rowCursor = sheet.iterator();
|
||||||
|
// Skip header row if necessary
|
||||||
|
if (rowCursor.hasNext()) {
|
||||||
|
rowCursor.next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Row read() {
|
||||||
|
if (rowCursor != null && rowCursor.hasNext()) {
|
||||||
|
return rowCursor.next();
|
||||||
|
} else {
|
||||||
|
return null; // No more rows to read
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -47,8 +47,8 @@ public class FourthBatch {
|
|||||||
return new StepBuilder("fourthStep", jobRepository)
|
return new StepBuilder("fourthStep", jobRepository)
|
||||||
.<Row, AfterEntity> chunk(10, platformTransactionManager)
|
.<Row, AfterEntity> chunk(10, platformTransactionManager)
|
||||||
.reader(excelReader())
|
.reader(excelReader())
|
||||||
.processor(middleProcessor())
|
.processor(fourthProcessor())
|
||||||
.writer(afterWriter())
|
.writer(fourthAfterWriter())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,19 +56,20 @@ public class FourthBatch {
|
|||||||
public ItemReader<Row> excelReader() {
|
public ItemReader<Row> excelReader() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return new ExcelRowReader("path/to/your/excel/file.xlsx");
|
return new ExcelRowReader("C:\\Users\\kim\\Desktop\\yummi.xlsx");
|
||||||
|
//리눅스나 맥은 /User/형태로
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ItemProcessor<Row, AfterEntity> middleProcessor() {
|
public ItemProcessor<Row, AfterEntity> fourthProcessor() {
|
||||||
|
|
||||||
return new ItemProcessor<Row, AfterEntity>() {
|
return new ItemProcessor<Row, AfterEntity>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AfterEntity process(Row item) throws Exception {
|
public AfterEntity process(Row item) {
|
||||||
|
|
||||||
AfterEntity afterEntity = new AfterEntity();
|
AfterEntity afterEntity = new AfterEntity();
|
||||||
afterEntity.setUsername(item.getCell(0).getStringCellValue());
|
afterEntity.setUsername(item.getCell(0).getStringCellValue());
|
||||||
@@ -79,7 +80,7 @@ public class FourthBatch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public RepositoryItemWriter<AfterEntity> afterWriter() {
|
public RepositoryItemWriter<AfterEntity> fourthAfterWriter() {
|
||||||
|
|
||||||
return new RepositoryItemWriterBuilder<AfterEntity>()
|
return new RepositoryItemWriterBuilder<AfterEntity>()
|
||||||
.repository(afterRepository)
|
.repository(afterRepository)
|
||||||
|
|||||||
@@ -49,5 +49,17 @@ public class MainController {
|
|||||||
return "ok";
|
return "ok";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/fourth")
|
||||||
|
private String fourthApi(@RequestParam("value") String value) throws Exception {
|
||||||
|
|
||||||
|
JobParameters jobParameters = new JobParametersBuilder()
|
||||||
|
.addString("date", value)
|
||||||
|
.toJobParameters();
|
||||||
|
|
||||||
|
jobLauncher.run(jobRegistry.getJob("fourthJob"), jobParameters);
|
||||||
|
|
||||||
|
return "ok";
|
||||||
|
}
|
||||||
|
|
||||||
//https://docs.spring.io/spring-batch/reference/job/configuring-launcher.html
|
//https://docs.spring.io/spring-batch/reference/job/configuring-launcher.html
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user