使用Spring Batch写入Cassandra数据库

时间:2017-09-13 21:21:06

标签: spring-batch spring-data-cassandra

截至目前,我可以通过以下代码连接到Cassandra:

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;

public static Session connection() {
    Cluster cluster = Cluster.builder()
        .addContactPoints("IP1", "IP2")
        .withCredentials("user", "password")
        .withSSL()
        .build();
    Session session = null;
    try {
        session = cluster.connect("database_name");
        session.execute("CQL Statement");
    } finally {
        IOUtils.closeQuietly(session);
        IOUtils.closeQuietly(cluster);
    }
    return session;
}

问题是我需要在Spring Batch项目中写入Cassandra。大多数入门套件似乎都使用JdbcBatchItemWriter从块中写入mySQL数据库。这可能吗?似乎JdbcBatchItemWriter无法连接到Cassandra数据库。

当前的项目撰写代码如下:

@Bean
public JdbcBatchItemWriter<Person> writer() {
    JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
    writer.setItemSqlParameterSourceProvider(new 
        BeanPropertyItemSqlParameterSourceProvider<Person>());
    writer.setSql("INSERT INTO people (first_name, last_name) VALUES 
        (:firstName, :lastName)");
    writer.setDataSource(dataSource);
    return writer;
}

2 个答案:

答案 0 :(得分:2)

Spring Data Cassandra为Cassandra提供了存储库抽象,你应该能够与.facetwp-page, .pager { display: inline-block; margin-right: 6px; cursor: pointer; color: #bbbbbb; } 一起使用来从Spring Batch写入Cassandra。

答案 1 :(得分:0)

通过定制ItemReader和ItemWriter,可以扩展Spring Batch以支持Cassandra。

ItemWriter示例:

public class CassandraBatchItemWriter<Company> implements ItemWriter<Company>, InitializingBean {

    protected static final Log logger = LogFactory.getLog(CassandraBatchItemWriter.class);
    private final Class<Company> aClass;
    @Autowired
    private CassandraTemplate cassandraTemplate;

    @Override
    public void afterPropertiesSet() throws Exception { }

    public CassandraBatchItemWriter(final Class<Company> aClass) {
        this.aClass = aClass;
    }

    @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将其作为@Configuration注入

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

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