Reader、Processor、Writer
チャンク指向のバッチフローを構築します
「Reader、Processor、Writer」はCoddyKit上の無料Spring Boot 4 Microservices & REST APIsレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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
よくある質問
「Reader、Processor、Writer」レッスンは無料ですか?
はい。「Reader、Processor、Writer」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Spring Boot 4 Microservices & REST APIsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。
「Reader、Processor、Writer」で何を学びますか?
チャンク指向のバッチフローを構築します ブラウザで直接実行するハンズオンコードでSpring Boot 4 Microservices & REST APIsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Spring Boot 4 Microservices & REST APIsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSpring Boot 4 Microservices & REST APIsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「Reader、Processor、Writer」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSpring Boot 4 Microservices & REST APIsレッスンでコードを書いて実行できますか?
はい。すべてのSpring Boot 4 Microservices & REST APIsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- @Scheduledによるスケジューリング
- Spring Batchのジョブとステップ
- Reader、Processor、Writer
- 再実行とエラー処理