【问题标题】:Spring Data for Cassandra - CassandraOperations get max of columnCassandra 的 Spring Data - CassandraOperations 获得最大列数
【发布时间】:2018-07-13 08:51:03
【问题描述】:

我正在开发 Spring Boot 应用程序以使用 Spring Data for Cassandra v1.5.10.Release 连接到 Cassandra。

我找不到任何显示如何使用 CassandraOperations 和 QueryBuilder 从 Cassandra 表中检索 max(col) 的参考资料。我在下面尝试过,但它返回 - 未定义的列名“max(id)”

public class CassandraTest{

  @Autowired
  private CassandraOperations cassandraTemplate;

  public void execute() {
    Select sel = QueryBuilder.select("max(id)").from("table").;
    Integer maxId= cassandraTemplate.queryForObject(sel, Integer.class);
    System.out.println("maxid ===> " + maxId);
  }
}

【问题讨论】:

    标签: spring spring-data-jpa datastax-java-driver spring-data-cassandra


    【解决方案1】:

    在通过reference doc 之后,我发现了另一种方法——不使用 QueryBuilder 的老式方法。

        String selectQuery = "select max(id) as maxId from table";      
        Integer maxId= csOps.selectOne(selectQuery, Integer.class);
        System.out.println("maxid ===> " + maxId);
    

    我仍然想知道 QueryBuilder 是否可行。有趣的是,datastax driver Select api 支持 max 函数。

    public Select.SelectionOrAlias max(Object column)
    Description copied from class: Select.Selection
    Creates a max(x) built-in function call.
    Overrides:
    max in class Select.Selection
    Returns:
    the function call.
    

    【讨论】:

      【解决方案2】:

      这应该可以工作(尽管未经测试):

      Select sel = QueryBuilder.select().max(column("id")).from("table").;
      

      【讨论】:

        猜你喜欢
        • 2016-01-10
        • 2021-03-13
        • 1970-01-01
        • 2018-08-23
        • 2020-10-31
        • 2020-02-19
        • 1970-01-01
        • 2021-08-19
        • 2019-02-22
        相关资源
        最近更新 更多