对于异步,它就像使用
executeAsync函数一样简单:
...DatastaxConnection.getSession().executeAsync(query);
对于批处理,您需要构建查询(我使用字符串,因为编译器知道如何很好地优化字符串连接):
String cql = "BEGIN BATCH " cql += "INSERT INTO test.prepared (id, col_1) VALUES (?,?); "; cql += "INSERT INTO test.prepared (id, col_1) VALUES (?,?); "; cql += "APPLY BATCH; "DatastaxConnection.getInstance();PreparedStatement prepStatement = DatastaxConnection.getSession().prepare(cql);prepStatement.setConsistencyLevel(ConsistencyLevel.ONE);// this is where you need to be careful// bind expects a comma separated list of values for all the params (?) above// so for the above batch we need to supply 4 params: BoundStatement query = prepStatement.bind(userId, "col1_val", userId_2, "col1_val_2");DatastaxConnection.getSession().execute(query);
附带一提,假设您将属性更改为映射列表,其中每个映射表示批处理中的更新/插入,我认为您对语句的绑定可能类似于以下内容:
BoundStatement query = prepStatement.bind(userId, attributesList.get(0).values().toArray(new Object[attributes.size()]), userId_2, attributesList.get(1).values().toArray(new Object[attributes.size()]));



