cassandrabatch操作添加字符串查询

kq0g1dla  于 2021-06-09  发布在  Cassandra
关注(0)|答案(1)|浏览(354)

我写了以下代码:

@Service
public class CassandraBatchOperationService {
@Autowired CassandraTemplate cassandraTemplate;
public void batchOps(List<Object>entities){
        CassandraBatchOperations batchCql = cassandraTemplate.batchOps().insert(entities);
        batchCql.execute(); 
}}

我知道它是如何工作的,它是好的。但我想知道有没有可能添加查询字符串( "INSERT INTO table1(value1,value2) VALUES ('test','test');" )去cassandrabatchoperations对象?我希望类似的情况如下:

@Service
public class CassandraBatchOperationService {
@Autowired CassandraTemplate cassandraTemplate;
public void batchOps(List<Object>entities,List<String> queries){
        CassandraBatchOperations batchCql = cassandraTemplate.batchOps().insert(entities).insert(queries);
        batchCql.execute(); 
}}
olhwl3o2

olhwl3o21#

批原始查询:

UUID uuid = UUIDs.timeBased();
String insertPreparedCql = 
  "insert into book (isbn, title, publisher, tags) values (?, ?, ?, ?)";
List<Object> singleBookArgsList = new ArrayList<>();
List<List<?>> bookList = new ArrayList<>();
singleBookArgsList.add(uuid);
singleBookArgsList.add("Head First Java");
singleBookArgsList.add("OReilly Media");
singleBookArgsList.add(ImmutableSet.of("Software"));
bookList.add(singleBookArgsList);
cassandraTemplate.ingest(insertPreparedCql, bookList);

如果可能,使用batch语句(忽略ormMap):

//session is your cassandra session
BatchStatement insertBatch = new BatchStatement();

PreparedStatement insertQuery =  session.prepare("INSERT ......"); 
PreparedStatement insertQuery2 =  session.prepare("INSERT ......"); 
    .
    .
insertBatch.add(insertQuery);
insertBatch.add(insertQuery2);
...
session.execute(insertBatch);

相关问题