Reputation: 352
My process transforms the data into SCD2 pattern. Thus, any update in the source data culminates into updating the end_date
& active_ind
in the dimension
table and inserting a new record.
I have configured the SQL in an ItemReader
implementation which identifies the records which got changed in the source data.
I need help/suggestion on how to route the data to 2 writers, 1 each for update & insert?
Upvotes: 0
Views: 2426
Reputation: 10142
There is a general pattern in Spring for this type of use case and not necessarily for Spring Batch using Classifier Interface.
You can use BackToBackPatternClassifier implementation of this interface.
Additionally, you need to use Spring Batch provided ClassifierCompositeItemWriter
.
Here is a summary of steps:
The POJO/Java Bean that is passed on to writer should have some kind of String field that can identify the target ItemWriter
for that POJO.
Then you write a Classifier
that returns that String type for each POJO like this:
public class UpdateOrInsertClassifier {
@Classifier
public String classify(WrittenMasterBean writtenBean){
return writtenBean.getType();
}
}
and
@Bean
public UpdateOrInsertClassifier router() {
return new UpdateOrInsertClassifier();
}
I assume that WrittenMasterBean
is POJO that you sent to either of writers and it has a private String type;
field This Classifier
is your router.
BackToBackPatternClassifier
like - @Bean public Classifier classifier() { BackToBackPatternClassifier classifier = new BackToBackPatternClassifier(); classifier.setRouterDelegate(router()); Map<String,ItemWriter<WrittenMasterBean>> writerMap = new HashMap(); writerMap.put("Writer1", writer1()); writerMap.put("Writer2", writer2()); classifier.setMatcherMap(writerMap); return classifier; }
i.e. I assume that keys Writer1
and Writer2
will identify your writers for that particular bean.
writer1()
and writer2()
return actual ItemWriter
beans.
BackToBackPatternClassifier
needs two fields - one router classifier and another matcher map.
Restriction is that keys are Strings in this classifier. You can't use any other type of keys.
BackToBackPatternClassifier
to ClassifierCompositeItemWriter
- You need to use Spring Batch provided ClassifierCompositeItemWriter @Bean public ItemWriter<WrittenMasterBean> classifierWriter(){ ClassifierCompositeItemWriter<WrittenMasterBean> writer = new ClassifierCompositeItemWriter(); writer.setClassifier(classifier()); return writer; }
You configure this - classifierWriter()
into your Step
.
Then you are good to go.
Upvotes: 1