Spring Batch – Read once, Write multi example

Sometimes, we need to read once, write multi ETL with Spring Batch. Spring Batch supports it with CompositeItemWriter (org.springframework.batch.item.support.CompositeItemWriter).

Sample table schema

CREATE TABLE "TB_SOURCE"
   (	"COL1" VARCHAR2(10 BYTE),
	"COL2" VARCHAR2(20 BYTE),
	"COL3" VARCHAR2(20 BYTE)
   );

CREATE TABLE "TB_TARGET1"
   (	"NEW_COL1" VARCHAR2(10 BYTE),
	"NEW_COL2" VARCHAR2(20 BYTE)
   );

  CREATE TABLE "TB_TARGET2"
   (	"NEW_COL1" VARCHAR2(10 BYTE),
	"NEW_COL3" VARCHAR2(20 BYTE)
   );

Declaration of CompositeItemWriter

Make sure that CompositeItemWriter has 2 delegates (TestWriter1, TestWriter2). You can use any Writer as it’s delegate.

Full Job declaration

Make sure that Job task is using new CompositeItemWriter instead of JdbcBatchItemWriter.

 

You can download the full sample source at https://github.com/tkstone/spring_batch_sample01

  • Spring context : src/main/resources/spring/job-test-composite-context.xml
  • Launcher class : test.main.TestCompositeRun.java
  • Processor class : test.main.TestProcessor.java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.