【问题标题】:spring batch metadata in cassandra databasecassandra数据库中的spring批处理元数据
【发布时间】:2020-01-06 03:11:47
【问题描述】:

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

【问题讨论】:

    标签: java cassandra spring-batch nosql


    【解决方案1】:

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

    【讨论】:

    • 那么有什么优雅的克服吗?
    【解决方案2】:

    可以通过自定义 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...");
    }
    

    @Beans:

    @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找到

    【讨论】:

      猜你喜欢
      • 2017-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-28
      相关资源
      最近更新 更多