首页 文章

使用Spring Batch写入Cassandra数据库

提问于
浏览
3

截至目前,我可以通过以下代码连接到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;
}

1 回答

  • 2

    Spring Data Cassandra为Cassandra提供了存储库抽象,你应该能够与_2771472一起使用来从Spring Batch写入Cassandra .

相关问题