ตัวอ่าน ตัวประมวลผล และตัวเขียน
สร้างกระบวนการประมวลผลแบบกลุ่มที่ทำงานเป็นส่วน
ตัวอ่าน ตัวประมวลผล และตัวเขียน เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Microservices & REST APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The read-process-write triad
A chunk step has three collaborators:
- ItemReader - supplies items one at a time
- ItemProcessor - transforms or filters an item
- ItemWriter - persists a chunk of items
ItemReader interface
ItemReader.read() returns the next item, or null when the input is exhausted. Spring Batch keeps calling it until it returns null.
public interface ItemReader<T> {
T read() throws Exception;
}FlatFileItemReader
For CSV files, FlatFileItemReader reads lines and maps them to objects via a line mapper.
@Bean
public FlatFileItemReader<Person> reader() {
return new FlatFileItemReaderBuilder<Person>()
.name("personReader")
.resource(new ClassPathResource("people.csv"))
.delimited()
.names("firstName", "lastName")
.targetType(Person.class)
.build();
}JdbcCursorItemReader
To read from a database, use JdbcCursorItemReader or JdbcPagingItemReader. Paging is preferred for very large result sets to avoid holding a long cursor.
@Bean
public JdbcCursorItemReader<Person> dbReader(DataSource ds) {
return new JdbcCursorItemReaderBuilder<Person>()
.name("dbReader")
.dataSource(ds)
.sql("SELECT first_name, last_name FROM people")
.rowMapper(new PersonRowMapper())
.build();
}ItemProcessor interface
ItemProcessor transforms an input item into an output item. The input and output types can differ.
public interface ItemProcessor<I, O> {
O process(I item) throws Exception;
}Writing a processor
Here a processor uppercases names. Returning a transformed object is the common case.
public class UpperCaseProcessor implements ItemProcessor<Person, Person> {
@Override
public Person process(Person p) {
return new Person(p.getFirstName().toUpperCase(),
p.getLastName().toUpperCase());
}
}Filtering with the processor
If a processor returns null, the item is filtered out and never reaches the writer. This is how you skip records based on business rules.
public Person process(Person p) {
if (p.getLastName().isBlank()) {
return null; // drop this record
}
return p;
}ItemWriter interface
ItemWriter receives a chunk of items at once, not single items. This lets it batch the write into one efficient operation.
public interface ItemWriter<T> {
void write(Chunk<? extends T> chunk) throws Exception;
}JdbcBatchItemWriter
JdbcBatchItemWriter performs a batched SQL insert/update for the whole chunk, which is far faster than row-by-row writes.
@Bean
public JdbcBatchItemWriter<Person> writer(DataSource ds) {
return new JdbcBatchItemWriterBuilder<Person>()
.dataSource(ds)
.sql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)")
.beanMapped()
.build();
}Wiring them into a step
The step ties the triad together with a chunk size. Items flow reader -> processor -> writer in chunks.
return new StepBuilder("step1", repo)
.<Person, Person>chunk(50, tx)
.reader(reader())
.processor(new UpperCaseProcessor())
.writer(writer(ds))
.build();Custom readers and writers
You can implement the interfaces directly for custom sources (a REST API, a queue). Spring also offers ItemStream so your component can save and restore its position for restarts.
Quick Check
Verify your understanding of the triad.
Recap
You assembled a chunk pipeline:
ItemReader.read()returns items until nullItemProcessortransforms; returning null filtersItemWriterwrites a wholeChunkat once- The step binds all three with a chunk size
คำถามที่พบบ่อย
บทเรียน “ตัวอ่าน ตัวประมวลผล และตัวเขียน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ตัวอ่าน ตัวประมวลผล และตัวเขียน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ตัวอ่าน ตัวประมวลผล และตัวเขียน”
สร้างกระบวนการประมวลผลแบบกลุ่มที่ทำงานเป็นส่วน คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Microservices & REST APIs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Microservices & REST APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “ตัวอ่าน ตัวประมวลผล และตัวเขียน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Microservices & REST APIs นี้ได้ไหม
ได้ บทเรียน Spring Boot 4 Microservices & REST APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การกำหนดเวลาผ่าน @Scheduled
- งานและขั้นตอน Spring Batch
- ตัวอ่าน ตัวประมวลผล และตัวเขียน
- การเริ่มใหม่และการจัดการข้อผิดพลาด