Rahul
Rahul

Reputation: 379

Cassandra BoundStatement with Range Boundary Query

I am using PreparedStatement and boundStatment to execute some Cassandra queries. I am trying to execute the range query. This is the query I want to create:

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' is partition key and 'documentIndex' is clustering key. I want to have range query on column documentIndex like "get me all records with given documentId and documentIndex >= 3 and documentIndex <= 10"

When I want to run the query, I call

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

How can I set the startDocumentIndex and endDocumentIndex for the above query?

Upvotes: 0

Views: 478

Answers (1)

Alex Ott
Alex Ott

Reputation: 87359

I would recommend to use named bind markers instead of unnamed - it's much easier to read code that use them. So in your case the code will look as following:

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

And then you can bind the by name:

BoundStatement stmt = pStatement.bind()
       .setString("documentId", startDocumentIndex)
       .setInt("documentIndexStart", startDocumentIndex)
       .setInt("documentIndexEnd", endDocumentIndex);

Upvotes: 1

Related Questions