有限制的 Cassandra BoundStatement

Cassandra BoundStatement with limit

我正在使用 PreparedStatement 和 BoundStatement 来执行一些 Cassandra 查询。问题是,我正在尝试对这些进行限制。这是我的:

 selectByDatasetIdAndTimePreparedStatement = getSession().prepare(
                QueryBuilder.select()
                        .all()
                        .from(KEYSPACE_NAME, TABLE_NAME)
                        .where(QueryBuilder.eq(AFFILIATION_ID_COLUMN, QueryBuilder.bindMarker()))
                        .and(QueryBuilder.eq(DATASET_ID_COLUMN, QueryBuilder.bindMarker()))
                        .and(QueryBuilder.lte(TIME_COLUMN, QueryBuilder.bindMarker()))
                        //.limit(QueryBuilder.bindMarker())
        );

每当我需要运行查询时,我调用这个函数:

public Statement buildSelectByDatsetIdAndTimePreparedStatment(String affiliationId, String datasetId, Long time, int limit)
{
    BoundStatement boundStatement = selectByDatasetIdAndTimePreparedStatement
            .bind()
            .setString(AFFILIATION_ID_COLUMN, affiliationId)
            .setString(DATASET_ID_COLUMN, datasetId)
            .setLong(TIME_COLUMN, time);
    databaseManager.applyReadStatementsConfiguration(boundStatement);
    return boundStatement;
}

但是,这仅在第一个代码片段中没有 limit 子句的情况下有效。我不知道如何在第二个片段中指定限制。我不想使用字符串,比如

databaseManager.getSession().execute("SELECT * FROM myTable where ... limit 10);

有什么方法可以使用 BoundStatement 做到这一点吗?我没有看到 BoundStatement.limit() 或 setLimit().

之类的东西

谢谢, 塞尔维亚语

最简单的方法是使用命名标记作为限制:

PreparedStatement pst = session.prepare(
        select().all()
                .from("foo")
                .where(eq("k", bindMarker()))
                .limit(bindMarker("l")));     // here

session.execute(pst.bind()
        .setInt("k", 1)
        .setInt("l", 10));

您也可以保留匿名标记并使用位置设置器:

                .where(eq("k", bindMarker()))
                .limit(bindMarker())); // no name
session.execute(pst.bind()
        .setInt(0, 1)
        .setInt(1, 10));

郑重声明,当您使用匿名标记时,Cassandra 会自动为它们命名。对于列,这些是列名(您的示例依赖于它),对于限制,它是 [limit]。所以这也行得通:

                .where(eq("k", bindMarker()))
                .limit(bindMarker())); // no name
session.execute(pst.bind()
        .setInt("k", 1)
        .setInt("[limit]", 10));

如有疑问,您可以检查准备好的语句的元数据以查看其预期内容:

for (ColumnDefinitions.Definition v : pst.getVariables()) {
    System.out.printf("%s %s%n", v.getName(), v.getType().getName());
}

为了清晰起见,我个人喜欢命名标记,但是 YMMV。