在写入数据库之前使用Spring Batch处理批量记录 [英] processing batch of records using spring batch before writing to DB
本文介绍了在写入数据库之前使用Spring Batch处理批量记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Spring批处理代码中,我正在读取100条记录。对于块中的每条记录,我都在检查该记录是否存在于数据库中。如果数据库中存在该记录,我不会插入它。
第一次,如果我在100的块中有重复的记录,Spring Batch Processor不能识别该记录是重复的,因为DB中没有数据,处理器将首先选择全部100,然后执行插入。
有没有一种方法可以先在100的区块内执行检查,然后检查DB,然后再将这两个检查插入到DB中?
推荐答案
您可以实现自己的自定义ItemProcessor
来检查重复项并将其删除。
举个例子:
public class DropDuplicateItemProcessor<T> implements ItemProcessor<T, T> {
// Will be used to save previous items;
private List<T> previousItems = new ArrayList<T>();
@Override
public T process(T item) throws Exception {
// Check for duplicates with your own logic (method equals)
if (previousItems.contains(item))
return null; // Drop duplicate
// Save item to check for duplicates later
previousItems.add(item);
// Continue with non-duplicate item
return item;
}
}
这篇关于在写入数据库之前使用Spring Batch处理批量记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文