首页 文章

如何使用Spring Data异步插入Cassandra?

提问于
浏览
1

在旧版本的Spring Data Cassandra中,批处理实现如下:

String cqlIngest = "insert into person (id, name, age) values (?, ?, ?)";

List<Object> person1 = new ArrayList<Object>();
person1.add("10000");
person1.add("David");
person1.add(40);

List<Object> person2 = new ArrayList<Object>();
person2.add("10001");
person2.add("Roger");
person2.add(65);

List<List<?>> people = new ArrayList<List<?>>();
people.add(person1);
people.add(person2);

cassandraOperations.ingest(cqlIngest, people);

并在最新版本的文件中; https://docs.spring.io/spring-data/cassandra/docs/current/reference/html/#repositories.query-methods batchOps在CQLTemplate上引入,它获取CassandraBatchOperations . 但是这个类看起来像实体一样,如下所示

template.batchOps().insert(new User()).execute();

有没有办法传递cqlIngest和人们喜欢在顶部给出的旧版本的代码示例?

我正在使用Spring 2.0.7.RELEASE和Cassandra驱动程序3.5.0 .

1 回答

  • 1

    使用Apache Cassandra 2.0的Spring Data,API被重构和清理 .

    以前,Template API是低级和高级功能的混合,同步和异步执行模型被混合到一个类中 .

    从版本2.0开始,现在有以下API可供使用:

    ingest 方法接受了CQL字符串和 ListList 参数 . 我们简化了这种情况,因为 ingest 在没有正确同步的情况下异步执行了CQL . 您可以通过 AsyncCqlTemplate.execute(…) 实现类似的功能:

    ListenableFuture<Boolean> insert1 = template.execute("insert into person (id, name, age) values (?, ?, ?)", 
                                                         "10000", "David", 40);
    ListenableFuture<Boolean> insert2 = template.execute("insert into person (id, name, age) values (?, ?, ?)", 
                                                         "10001", "Roger", 65);
    

    有两点不同:

    • 您负责迭代参数

    • 您收到 ListenableFuture ,它允许您同步以成功执行和执行异常 .

相关问题