0

Small question regarding Cassandra, with the context of a SpringBoot application please.

I am interested in adding onto the table the timestamp of when a row gets inserted onto the table.

Therefore, when creating the table, I do this:

create table mytable (someprimarykey text PRIMARY KEY, timestamp long, someotherfield text);

I can see a column timestamp, happy.

Then, the web application:

@Table
public class MyTable {

    @PrimaryKey
    private final String somePrimaryKey;

    private final long timestamp;

    private final String someOtherField;

//constructor + getters + setters

And when I insert, I will do the usual:

MyTable myTable = new MyTable(somePK, System.currentTimeMillis(), "foo");
myTableRepository.save(myTable);

This works fine, I can see in the table my record, with the time of the insert, happy.

Problem: Now, for the hundreds of POJOs I am interested to insert into Cassandra, all of them are carrying this timestamp long field. Somehow, on the web application layer, I am dealing with a database concept, the timestamp of the write.

Question: May I ask if it is possible to delegate this back to the database? Some kind of:

create table mytable (someprimarykey text PRIMARY KEY, hey-cassandra-please-automatically-add-the-time-when-this-row-is-written long, someotherfield text);
or
create table mytable (someprimarykey text PRIMARY KEY, someotherfield text WITH default timestamp-insert-time-column);

And the web app can have the abstraction creating and inserting POJOs without carrying this timestamp field?

Thank you

1 Answer 1

1

It isn't necessary to store the insert time of each row separately since Cassandra already stores this for all writes in the metadata.

There is a built-in CQL function WRITETIME() which returns the date/time (encoded in microseconds) when a column was written to the database.

In your case, you can query your table with:

SELECT WRITETIME(someotherfield) FROM mytable WHERE someprimarykey = ?

For details, see the CQL doc on retrieving the write time. Cheers!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.