具有范围边界查询的 Cassandra BoundStatement

Cassandra BoundStatement with Range Boundary Query

我正在使用 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 进行范围查询,例如 "get me all records with given documentId and documentIndex >= 3 and 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?

我建议使用 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);