10

Is there an "IF EXISTS UPDATE ELSE INSERT" command in CQL (Cassandra)? If not, what's the most efficient way to perform such a query?

Justin Borromeo
  • 1,201
  • 3
  • 13
  • 26
  • 1
    Usually you would just use an upsert. Can you provide more info on the query and use case. http://mechanics.flite.com/blog/2013/11/01/how-to-do-an-upsert-in-cassandra/ – Marko Švaljek Apr 13 '17 at 15:06
  • I'm storing a partition key, a clustering key, then a set which I want to add to every hour. However, I need to check if the row exists before I add to the set. Is it possible to use an update to create a row? If so, then the existence of the row wouldn't matter. – Justin Borromeo Apr 13 '17 at 15:12
  • partitioning key and clustering key you can always upsert, if it's known to you you can just insert it. Can you please provide table description? Also an insert statement and what is your collision concern would be nice – Marko Švaljek Apr 13 '17 at 15:16
  • Table description from the DESCRIBE table command? – Justin Borromeo Apr 13 '17 at 15:16
  • yes, also show me inserts, and why are they colliding. I just need columns, primary key and insert statement – Marko Švaljek Apr 13 '17 at 15:17
  • I just tried using UPDATE as an insert and it worked – Justin Borromeo Apr 13 '17 at 15:22
  • :) o.k. then you are good – Marko Švaljek Apr 13 '17 at 15:23

1 Answers1

5

Basically you should use update. In cassandra they have a bit different mechanics when compared to relational world and will work as implied inserts.

Answer: Does an UPDATE become an implied INSERT

Community
  • 1
  • 1
Marko Švaljek
  • 2,071
  • 1
  • 14
  • 26