Spring batch Spring批处理-跳过进程上的记录
我想跳过一些关于这个过程的记录 我尝试过的是,我创建了自定义异常,当我想跳过记录并调用skip listener onSkipInProcess方法时抛出异常。它工作正常 请查找配置Spring batch Spring批处理-跳过进程上的记录,spring-batch,Spring Batch,我想跳过一些关于这个过程的记录 我尝试过的是,我创建了自定义异常,当我想跳过记录并调用skip listener onSkipInProcess方法时抛出异常。它工作正常 请查找配置 <batch:chunk reader="masterFileItemReader" writer="masterFileWriter" processor="itemProcessor" commit-interval="5000" skip-limit="100000" > <batch
<batch:chunk reader="masterFileItemReader" writer="masterFileWriter" processor="itemProcessor" commit-interval="5000" skip-limit="100000" >
<batch:skippable-exception-classes>
<batch:include class="org.springframework.batch.item.file.FlatFileParseException"/>
<batch:include class="com.exception.SkipException"/>
</batch:skippable-exception-classes>
<batch:listeners>
<batch:listener ref="recordSkipListener"/>
</batch:listeners>
但我想知道有没有其他方法可以跳过进程中的记录
问候,,
Shankar组件
@范围(value=“步骤”)
公共类XyzItemProcessor实现ItemProcessor{
@凌驾
公共ABCInfo进程(ABCInfo ABCInfo)引发异常{
if(abcInfo.getRecordType()等于(“H”)|| extVoterInfo.getRecordType()等于(“T”))
return null;///这就是我们跳过特定记录以在数据库中持久化的方式
否则{
返回abcInfo;
}
}
}
Return null将跳过要保存在数据库中的特定记录实际上有两种方法可以做到这一点,一种是使用skip机制,另一种是使用returning
null,它将过滤掉项而不写入它。这里很好地解释了两种方法之间的区别。这也解释了详细信息中的跳过和成批处理中的事务
当您解析csv文件时,您希望每行5个项目,但一行包含6个无效项目,您可以选择不跳过它(通过将读卡器异常标记为可跳过,并在策略中定义条件,如您给出的示例所示)。但是,如果每一行都有名称,并且您的用例是不写以字母N
开头的项目,那么最好通过返回null
(过滤项目)来实现,因为它是有效的项目,但不符合您的业务案例
还请注意,如果您返回null
这些项目的数量将在getFilterCount()
中的StepContext
中,如果您使用跳过方法,它们将在getReadSkipCount()
中,getProcessorSkipCount
和getWriteSkipCount
。当我们在process()方法中返回null时,它将过滤记录并增加过滤计数
@Transactional(propagation = Propagation.REQUIRED)
@Override
public SomeObject process(SomeObject someObject) throws Exception {
if (some condition) {
return null;
}
}
如果要跳过记录,请抛出异常。这将跳过记录并增加processSkipCount
@Transactional(propagation = Propagation.REQUIRED)
@Override
public SomeObject process(SomeObject someObject) throws Exception {
if (some condition) {
throw new Exception("invalid record");
}
}
也将此异常添加到上下文文件中
<batch:skippable-exception-classes>
<batch:include class="java.lang.Exception" />
</batch:skippable-exception-classes>
还有另一种不写(跳过)东西的方法。例如,假设我们有以下步骤:
<batch:step id="createCsvStep">
<batch:tasklet>
<batch:chunk reader="jdbcCursorItemReader" processor="processor" writer="compositeWriter"
commit-interval="#{jobParameters['commit.interval']}" />
</batch:tasklet>
</batch:step>
<bean id="compositeWriter" class="org.springframework.batch.item.support.CompositeItemWriter" scope="step">
<property name="delegates">
<list>
<ref bean="csvFileItemWriter1"/>
<ref bean="csvFileItemWriter2"/>
</list>
</property>
</bean>
假设第一个编写器将写入所有值,但同时,第二个编写器将跳过其中的一些值。为了实现这一点,我们可以扩展writer(例如FlatFileItemWriter),并像下面这样重写它的write方法:
@Override
public void write(List<? extends T> items) throws Exception {
// ...
if (itemsPassesCheckingCondition) {
super.write(items);
}
}
@覆盖
public void write(列表是的,我们可以根据密码跳过记录。请查找代码段beelow this您可以随时阅读文档,并且可以找到很好的示例。感谢@Nenad提供详细的答案,以及何时使用null和何时使用skip listener。您好,Nenad,getFilterCount()方法在StepExecution类中,如何通过java获取此值?我想获取作业中跳过的记录数。在我的测试中,我正在运行作业并保存该作业执行,当我想验证过滤了多少记录时,请使用jobExecution.getStepExecutions()
这是一个集合,但我可以遍历它并通过名称找到我的步骤执行,其中包含方法getFilterCount()
如果您需要定期跳过-即根据条件筛选出记录,您可以使用返回空值跳过正在写入的记录。当您在处理过程中发生异常并继续处理,但希望对错误记录采取某些操作时,将使用onSkipInProcess(记录、提醒电子邮件)返回null
不会完全跳过…返回null
只会进行筛选。跳过用于异常处理。请阅读。@PAA抱歉,我不记得了-我已经讨论了一段时间了。但我建议阅读文档。
@Override
public void write(List<? extends T> items) throws Exception {
// ...
if (itemsPassesCheckingCondition) {
super.write(items);
}
}