2
votes

I'm trying to have a write a query such that

  1. insert a new row if primary key doesn't match a row
  2. if match a row, only update value if the provided timestamp is later than lastTimestamp column value

The struggle here (correct me if I'm wrong) is that I cannot use Cassandra's built-in "USE TIMESTAMP" functionality because the provided timestamp from the request is almost certainly lower than the default timestamp when creating a new row, therefore it won't be able to create a new row.

Therefore I can only create a new column to store the timestamp. Then I have a OR condition to fulfill: "if lastTimestamp == null OR lastTimestamp < timestamp", but unfortunately Cassandra doesn't support the "OR" logic in IF clause.

One alternative I can think of is, I'll always create a new row by have "INSERT IF NOT EXIST", and fill the lastTimestamp = 0. Then apply the "update if timestamp is greater" operation. But this essentially is two write than one. Any other possible solution?

1
Can you provide us an example ?Ashraful Islam
"...the provided timestamp from the request is almost certainly lower than the default timestamp when creating a new row..." Why? Can you elaborate?Jay Sullivan

1 Answers

2
votes

On insert/update/delete you need to specify timestamp in microseconds.

Simple way to do this is multiply your timestamp in milliseconds with 1000

Example :

INSERT INTO data(id, map) VALUES('123-123-12321', {'object2' : {value : '2', timestamp : '2017-09-09 16:24:06.197000+0000'}}) USING TIMESTAMP 1504952646912000;

Here 1504952646912000 is the timestamp in microseconds converted from 1504952646912 milliseconds