Illustration Image

Cassandra update or insert based on timestamp

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?

Become part of our
growing community!
Welcome to Planet Cassandra, a community for Apache Cassandra®! We're a passionate and dedicated group of users, developers, and enthusiasts who are working together to make Cassandra the best it can be. Whether you're just getting started with Cassandra or you're an experienced user, there's a place for you in our community.
A dinosaur
Planet Cassandra is a service for the Apache Cassandra® user community to share with each other. From tutorials and guides, to discussions and updates, we're here to help you get the most out of Cassandra. Connect with us and become part of our growing community today.
© 2009-2023 The Apache Software Foundation under the terms of the Apache License 2.0. Apache, the Apache feather logo, Apache Cassandra, Cassandra, and the Cassandra logo, are either registered trademarks or trademarks of The Apache Software Foundation. Sponsored by Anant Corporation and Datastax, and Developed by Anant Corporation.

Get Involved with Planet Cassandra!

We believe that the power of the Planet Cassandra community lies in the contributions of its members. Do you have content, articles, videos, or use cases you want to share with the world?