我正在使用PreparedStatement和boundStatment执行一些Cassandra查询。我正在尝试执行范围查询。这是我要创建的查询:
getAllRecordsOnIndexRange = getSession.prepare(QueryBuilder.select(documentId, documentName, documentIndex)
.from(tableName)
.where(QueryBuilder.eq(documentId,QueryBuilder.bindMarker()))
.and(QueryBuilder.gte(documentIndex, QueryBuilder.bindMarker()))
.and(QueryBuilder.lte(documentIndex, QueryBuilder.bindMarker())));
'documentId'是分区键,'documentIndex'是群集键。我想对documentIndex列进行范围查询,例如“获取具有给定documentId和documentIndex> = 3和documentIndex <= 10的所有记录”
当我想运行查询时,我打电话
public Statement buildGetAllRecordsOnIndexRange(int documentId, String documentName, int startDocumentIndex, int endDocumentIndex)
{
BoundStatement boundStatement = getAllRecordsOnIndexRange
.bind()
.setString(DOCUMENT_ID, documentId)
//how to set start and end documentIndex
databaseManager.applyReadStatementsConfiguration(boundStatement);
return boundStatement;
}
如何为上述查询设置startDocumentIndex和endDocumentIndex?
答案 0 :(得分:1)
我建议使用named bind markers而不是未命名的名称-读取使用它们的代码要容易得多。因此,在您的情况下,代码将如下所示:
PreparedStatement pStatement = session.prepare(QueryBuilder.select(documentId, documentName, documentIndex)
.from(tableName)
.where(QueryBuilder.eq(documentId,QueryBuilder.bindMarker("documentId")))
.and(QueryBuilder.gte(documentIndex, QueryBuilder.bindMarker("documentIndexStart")))
.and(QueryBuilder.lte(documentIndex, QueryBuilder.bindMarker("documentIndexEnd"))));
然后您可以按名称绑定:
BoundStatement stmt = pStatement.bind()
.setString("documentId", startDocumentIndex)
.setInt("documentIndexStart", startDocumentIndex)
.setInt("documentIndexEnd", endDocumentIndex);