Java 弹簧靴+弹簧批+弹簧JPA
我正在从事Spring批处理作业,该作业将数据从Sql Server移动到Cassandra。我使用SpringDataJPA来读写数据。我已经为这两个数据库创建了实体和JPA存储库 现在我不知道如何将我的JpaRepostorty与Spring Batch ItemReader一起使用。我在互联网上搜索,发现很少有参考文献提到使用JpaPageItemReader。但这需要指定查询和配置其他详细信息。但我不知道如何使用我现有的JpaRepository。下面是相关代码的片段- 我的SQL Server JpaRepostory- 我的春季批量工作- 更新1: 如前所述,我将条目读取器添加到批处理作业中Java 弹簧靴+弹簧批+弹簧JPA,java,spring,spring-boot,spring-data-jpa,spring-batch,Java,Spring,Spring Boot,Spring Data Jpa,Spring Batch,我正在从事Spring批处理作业,该作业将数据从Sql Server移动到Cassandra。我使用SpringDataJPA来读写数据。我已经为这两个数据库创建了实体和JPA存储库 现在我不知道如何将我的JpaRepostorty与Spring Batch ItemReader一起使用。我在互联网上搜索,发现很少有参考文献提到使用JpaPageItemReader。但这需要指定查询和配置其他详细信息。但我不知道如何使用我现有的JpaRepository。下面是相关代码的片段- 我的SQL Se
@Configuration
@EnableBatchProcessing
public class FortifySSCBatchConfigurationCassandra {
....
@Autowired
public ScanItemReader itemReader;
.....
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<Scan, CScan> chunk(100)
.reader(itemReader)
.processor(processor())
.writer(cqlWriter())
.build();
}
}
我的IDE对此表示不满-
The method reader(ItemReader<? extends Scan>) in the type SimpleStepBuilder<Scan,CScan> is not applicable for the arguments (ScanItemReader)
更新2:
public class CassandraItemProcessor implements ItemProcessor<Scan, CScan> {
@Override
public CScan process(Scan s) throws Exception {
..
return new CScan();
}
}
public class CassandraBatchItemWriter implements ItemWriter<CScan> {
@Override
public void write(List<? extends CScan> arg0) throws Exception {
// TODO Auto-generated method stub
}
}
你可以这样声明你的阅读器
@Component
@JobScope
public class ScanItemReader extends RepositoryItemReader<Scan> {
private final ScanJpaRepository repository;
@Autowired
public ScanItemReader(final ScanJpaRepository repository) {
super();
this.repository = repository;
}
@PostConstruct
protected void init() {
final Map<String, Sort.Direction> sorts = new HashMap<>();
sorts.put("Your sort parameter"), Direction.ASC);// This could be any field name of your Entity class
this.setRepository(this.repository);
this.setSort(sorts);
this.setMethodName(""); // You should sepcify the method which
//spring batch should call in your repository to fetch
// data and the arguments it needs needs to be
//specified with the below method.
// And this method must return Page<T>
this.setArguments();
}
}
自动连接此reader bean并在您的StepBuilder中使用它。我正在尝试做同样的事情,但被绊住了。如果它对您有效,请共享git存储库或任何其他参考。我想看看我们如何从一个数据库读取数据并写入另一个数据库。我已经有了JPA的方法。只想自动连线并使用它们。谢谢您的回复。我无法使用当前pom导入QAbstractOrder类的包。我是否需要导入任何其他依赖项?很抱歉,这是一个错误,如果需要排序,您需要为结果排序提供排序参数。您可以指定任何实体classI的字段名。我在下一个错误时更新了我的问题。StepBuilder不接受ScanItemReader。处理器类和编写器类如何。至少显示类声明。处理器应将扫描作为输入,将CScan作为输出。因为你已经声明对此感到抱歉。按要求添加了这两个类。找到解决方案了吗。我有同样的问题。提前谢谢
public class CassandraItemProcessor implements ItemProcessor<Scan, CScan> {
@Override
public CScan process(Scan s) throws Exception {
..
return new CScan();
}
}
public class CassandraBatchItemWriter implements ItemWriter<CScan> {
@Override
public void write(List<? extends CScan> arg0) throws Exception {
// TODO Auto-generated method stub
}
}
@Component
@JobScope
public class ScanItemReader extends RepositoryItemReader<Scan> {
private final ScanJpaRepository repository;
@Autowired
public ScanItemReader(final ScanJpaRepository repository) {
super();
this.repository = repository;
}
@PostConstruct
protected void init() {
final Map<String, Sort.Direction> sorts = new HashMap<>();
sorts.put("Your sort parameter"), Direction.ASC);// This could be any field name of your Entity class
this.setRepository(this.repository);
this.setSort(sorts);
this.setMethodName(""); // You should sepcify the method which
//spring batch should call in your repository to fetch
// data and the arguments it needs needs to be
//specified with the below method.
// And this method must return Page<T>
this.setArguments();
}
}