【问题标题】:CassandraTemplate fetch last 5 minutes' records based on timeuuidCassandraTemplate 根据 timeuuid 获取最近 5 分钟的记录
【发布时间】:2021-12-01 19:13:49
【问题描述】:

我有一个带有“created_at”列的 Cassandra 表,该表存储了基于时间的 uuid。现在,我需要编写一个函数来获取该表中最近 5 分钟内创建的所有记录。如果我为这个“created_at”列存储时间戳而不是 uuid,如下所示:

      LocalDateTime time = LocalDateTime.now().minusMinutes(5);

      Select.Where select = select(COLUMNS)
    .from(VIEW)
    .where(gt('created_at', Timestamp.valueOf(time)));

但我需要使用时间 uuid 而不是时间戳来解决这个问题。

【问题讨论】:

    标签: java spring-boot cassandra spring-data-cassandra


    【解决方案1】:

    CQL functions minTimeuuid and maxTimeuuid 可用于选择时间戳 UUID 满足给定时间范围的行。 CQL 查询示例,其中ttimeuuid 列:

    SELECT * FROM myTable
     WHERE t > maxTimeuuid('2013-01-01 00:05+0000')
       AND t < minTimeuuid('2013-02-02 10:00+0000');
    

    如果我没记错这些函数不是spring封装的,所以有必要使用CqlTemplate写原始的CQL查询。

    【讨论】:

      【解决方案2】:

      已经能够使用 QueryBuilder 做到这一点:

          final UUID min = UUIDs.startOf(LocalDateTime.now().minusMinutes(5).toInstant(ZoneOffset.UTC).toEpochMilli());
      
          Select.Where select = QueryBuilder.select().from("my_table").where(QueryBuilder.gt("created_at", min));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-21
        • 2022-01-11
        • 1970-01-01
        • 2012-03-16
        • 2018-04-27
        相关资源
        最近更新 更多