CassandraTemplate fetch last 5 minutes' records based on timeuuid

Viewed 165

I have a Cassandra table with a column 'created_at' which stored time based uuids. Now, I need to write a function to fetch all the records in this table that were created in the last 5 minutes. I am able to do this if I store timestamps rather than uuids for this 'created_at' column like below:

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

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

But I need to work this out with time uuids rather than timestamps.

2 Answers

CQL functions minTimeuuid and maxTimeuuid can be used to select rows which timestamp UUIDs satisfy range of given time. An example of CQL query, where t is timeuuid column:

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

If I don't mistake these functions are not wrapped by spring, so it is necessary to use CqlTemplate to write raw CQL queries.

Have been able to do this using 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));
Related