cassandra-boundstatement

xuo3flqw  于 2021-06-14  发布在  Cassandra
关注(0)|答案(1)|浏览(340)

我使用preparedstatement和boundstament执行一些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?

tkqqtvp1

tkqqtvp11#

我建议使用命名的绑定标记而不是未命名的绑定标记—阅读使用它们的代码要容易得多。因此,在您的情况下,代码如下所示:

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);

相关问题