Spring batch 弹簧批量误差容限

Spring batch 弹簧批量误差容限,spring-batch,Spring Batch,我正在尝试从csv文件导入城市数据,某些数据可能会重复,从而引发冲突错误错误:重复的键值违反了唯一约束“city\uu unique\u idx” 详细信息:键(国家、城市名称、纬度、经度)=(231,蒙蒂塞洛,30.5450000000000017,-83.870300000003)已存在。 2018-03-13 14:34:03.607错误13275---[main]o.s.batch.core.step.AbstractStep:在作业导入作业中执行步骤cityStep1时遇到错误。我想知

我正在尝试从csv文件导入城市数据,某些数据可能会重复,从而引发冲突错误
错误:重复的键值违反了唯一约束“city\uu unique\u idx”
详细信息:键(国家、城市名称、纬度、经度)=(231,蒙蒂塞洛,30.5450000000000017,-83.870300000003)已存在。
2018-03-13 14:34:03.607错误13275---[main]o.s.batch.core.step.AbstractStep:在作业导入作业中执行步骤cityStep1时遇到错误
。我想知道如何忽略此错误并保持作业运行,因为它当前立即退出

作者:

@Bean
public ItemWriter<City> cityWriter(EntityManagerFactory factory) {
    JpaItemWriter<City> writer = new JpaItemWriter<>();
    writer.setEntityManagerFactory(factory);
    return writer;
}
我仍然得到错误:

2018-03-14 15:49:40.047 ERROR 26613 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: duplicate key value violates unique constraint "city__unique_idx"
  Detail: Key (country, city_name, latitude, longitude)=(231, Monticello, 30.5450000000000017, -83.8703000000000003) already exists.
2018-03-14 15:49:40.161 ERROR 26613 --- [           main] o.s.batch.core.step.AbstractStep         : Encountered an error executing step cityStep1 in job importCityJob
org.springframework.retry.ExhaustedRetryException: Retry exhausted after last attempt in recovery path, but exception is not skippable.; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.springframework.batch.core.step.item.FaultTolerantChunkProcessor$5.recover(FaultTolerantChunkProcessor.java:405) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
    at org.springframework.retry.support.RetryTemplate.handleRetryExhausted(RetryTemplate.java:512) ~[spring-retry-1.2.1.RELEASE.jar:na]
    at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:351) ~[spring-retry-1.2.1.RELEASE.jar:na]
    at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:211) ~[spring-retry-1.2.1.RELEASE.jar:na]

您需要使步骤对特定异常具有容错性

@Bean
public Step cityStep1(ItemWriter<City> writer) {
    return stepBuilderFactory.get("cityStep1")
                .<City,City>chunk(10)
                .reader(cityReader())
                .processor(cityProcessor())
                .writer(writer)
                .faultTolerant()
                .skip(YourException.class)
                .noRetry(YourException.class)
                .noRollback(YourException.class)
                .skipLimit(10)
                .build();
}
@Bean
公共步骤cityStep1(项目编写器){
返回stepBuilderFactory.get(“cityStep1”)
.chunk(10)
.reader(cityReader())
.processor(城市处理器())
.作者(作者)
.容错()
.skip(YourException.class)
.noRetry(YourException.class)
.noRollback(YourException.class)
斯基普利米特先生(10)
.build();
}
您尚未列出您收到的编写器或异常,因此您可以将该异常替换为
YourException


noRetry
noRollBack
已设置为在跳过整个区块的Spring批回滚事务时更改默认Spring批处理行为,然后重新处理项目bu item。如果您同意这种方式,您可以从步骤定义中删除这些部分。

我已经尝试过这种方式,它给了我一些令人筋疲力尽的错误(抱歉,我记不起错误名称)。如果我理解正确,一旦遇到异常,此命令将忽略所有块。如果我只想忽略产生错误的单个项,我应该怎么做?如果我删除noRollback,它将检查错误块中的项,但如果它再次迭代到单个错误项,会发生什么情况?它是否也回滚到该项,还是在第二次时忽略它?当Spring Batch逐个处理项进行重试时,它将发现有问题的项并跳过它-这是第二次迭代的全部要点。您所说的不可跳过是什么意思?该错误意味着您的
150
坏记录阈值被打破。@SabirKhan该阈值不影响该错误,它出现在同一条记录上。
2018-03-14 15:49:40.047 ERROR 26613 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: duplicate key value violates unique constraint "city__unique_idx"
  Detail: Key (country, city_name, latitude, longitude)=(231, Monticello, 30.5450000000000017, -83.8703000000000003) already exists.
2018-03-14 15:49:40.161 ERROR 26613 --- [           main] o.s.batch.core.step.AbstractStep         : Encountered an error executing step cityStep1 in job importCityJob
org.springframework.retry.ExhaustedRetryException: Retry exhausted after last attempt in recovery path, but exception is not skippable.; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.springframework.batch.core.step.item.FaultTolerantChunkProcessor$5.recover(FaultTolerantChunkProcessor.java:405) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
    at org.springframework.retry.support.RetryTemplate.handleRetryExhausted(RetryTemplate.java:512) ~[spring-retry-1.2.1.RELEASE.jar:na]
    at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:351) ~[spring-retry-1.2.1.RELEASE.jar:na]
    at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:211) ~[spring-retry-1.2.1.RELEASE.jar:na]
@Bean
public Step cityStep1(ItemWriter<City> writer) {
    return stepBuilderFactory.get("cityStep1")
                .<City,City>chunk(10)
                .reader(cityReader())
                .processor(cityProcessor())
                .writer(writer)
                .faultTolerant()
                .skip(YourException.class)
                .noRetry(YourException.class)
                .noRollback(YourException.class)
                .skipLimit(10)
                .build();
}