I'm trying to build small web-system (url shortcutting) using nonsql Cassandra DB, the problem I stack is id auto generation.
Did someone already stack with this problem?
Thanks.
P.S. UUID not works for me, I do need to use ALL numbers from 0 to Long.MAX_VALUE (java). so I do need something that exactly works like sql sequence
UPDATED:
The reason why I'm not ok with GUID ids is inside of scope of my application.
My app has url shortcutting part, and I do need to make url as short as possible. So I follow next approach: I'm taking numbers starting from 0 and convert it base64 string. So in result I have url like mysite.com/QA (where QA is base 64 string).
This is was very easy to implement using SQL DB, I just took auto incremented ID, convert it to URL and was 100-percents sure, that URL is unique.
To implement triggers for auto-increment, log into your MongoDB Atlas account, open the cluster you want to work on. and click on Triggers. Click on Add Trigger and set the following values to the corresponding fields.
Don't know about Cassandra, but with mongo you can have an atomic sequence (it won't scale, but will work the way it should, even in sharded environment if the query has the sharded field).
It can be done by using the findandmodify command.
Let's consider we have a special collection named sequences
and we want to have a sequence for post numbers (named postid
), you could use code similar to this:
> db.runCommand( { "findandmodify" : "sequences", "query" : { "name" : "postid"}, "update" : { $inc : { "id" : 1 }}, "new" : true } );
This command will return atomically the updated (new
) document together with status. The value
field contains the returned document if the command completed successfully.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With