Istao
Istao

Reputation: 7585

How to identify a value in cassandra / NoSQL?

I am going from SQL to NoSQL with Cassandra.

I've read Do You Really Need SQL to Do It All in Cassandra?. That speaks about sql select, join, group by and order by, but there is nothing about the "id" concept in sql data base. In SQL, all values have an unique identifier.

Is there something like that with nosql/cassandra? What? Is it safe to do something like newId = lastId + 1 or something like that with Cassandra and how?

Thanks.

Upvotes: 2

Views: 204

Answers (2)

Dean Hiller
Dean Hiller

Reputation: 20192

If you are going from SQL to noSQL, another option to consider is playOrm

It does Scalable JQL like so (notice the addition of partitions but other than that, SQL is the same)

@NoSqlQuery(name="findJoinOnNullPartition", query="PARTITIONS p(:partId) select p FROM TABLE as p INNER JOIN p.security as s where s.securityType = :type and p.numShares = :shares"),

Also, it will generate unique cluster keys for you as well so you don't always need to deal with key generation ;). An example of playOrm's key generation is here(which is unique within one cluster)...

https://github.com/deanhiller/playorm/blob/master/input/javasrc/com/alvazan/orm/api/base/spi/UniqueKeyGenerator.java

Upvotes: 0

Pierre
Pierre

Reputation: 6172

IDs doesn't exist in Cassandra. It is a simple key / value store you need to provide you with your own document IDs (called keys). The suggested approach is to use UUIDs, which are designed to avoid conflicting keys.

Doing something like newId = lastId + 1 is not safe at all. Cassandra doesn't, by design, support transactions, and no way to make read + write atomic. Concurrent transactions can make this fail:

  • Process A reads 10
  • Process B reads 10
  • Process A writes 10 + 1 = 11
  • Process B writes 10 + 1 = 11... oops, this should be 12.

If you're interested, Cassandra Counters addresses this issue.

Upvotes: 1

Related Questions