cassandra数据库中的spring批处理元数据

q7solyqu  于 2023-02-12  发布在  Cassandra
关注(0)|答案(2)|浏览(241)

问题很简单:我可以在Cassandra数据库中为Spring Batch创建元数据模式吗?如果可以,我该如何做?我读到过Spring Batch需要RDBMS数据库,而不支持No-SQL数据库。这仍然是Spring Batch的限制吗?我最终该如何克服这个问题?

6ioyuze2

6ioyuze21#

由于Casandra不支持键的简单序列,SpringBatch不支持将其用于作业存储库。

wribegjk

wribegjk2#

可以通过定制ItemReader和ItemWriter来扩展Spring Batch以支持Cassandra。
读者:

@Override
public Company read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
    final List<Company> companies = cassandraOperations.selectAll(aClass);

    log.debug("Read operations is performing, the object size is  {}", companies.size());

    if (index < companies.size()) {
        final Company company = companies.get(index);
        index++;
        return company;
    }

    return null;
}

作者:

@Override
public void write(final List<? extends Company> items) throws Exception {
    logger.debug("Write operations is performing, the size is {}" + items.size());
    if (!items.isEmpty()) {
        logger.info("Deleting in a batch performing...");
        cassandraTemplate.deleteAll(aClass);
        logger.info("Inserting in a batch performing...");
        cassandraTemplate.insert(items);
    }

    logger.debug("Items is null...");
}

@豆豆:

@Bean
public ItemReader<Company> reader(final DataSource dataSource) {
    final CassandraBatchItemReader<Company> reader = new CassandraBatchItemReader<Company>(Company.class);
    return reader;
}

@Bean
public ItemWriter<Company> writer(final DataSource dataSource) {
    final CassandraBatchItemWriter<Company> writer = new CassandraBatchItemWriter<Company>(Company.class);
    return writer;
}

完整的源代码可以在Github Spring-Batch-with-Cassandra中找到

相关问题