Alexander Tsepkov
Alexander Tsepkov

Reputation: 4176

Get the autogenerated UUID in Cassandra from INSERT statement

Use case:

I have a RESTful API that inserts data into Cassandra and returns ID of new entry to the user. I'm making use of uuid() ID generator in Cassandra for populating the ID.

Problem:

INSERT statement does not return the generated ID, yet the RESTful API I described seems like a common use case. What's a way to obtain the UUID of that exact entry? I could in theory do something along the lines of SELECT id FROM table WHERE partition_key = ? ORDER BY id DESC LIMIT 1; but that seems like it would introduce a racing condition in case two writes happen around the same time.

Upvotes: 1

Views: 1498

Answers (2)

chas spenlau
chas spenlau

Reputation: 375

I know you don't state that you use Spring Data, but I'm fairly certain if you used Spring Data and called the .save() when inserting it'll return the saved entity including it's auto-generated id.

See this example as a reference: https://www.baeldung.com/spring-data-crud-repository-save

Upvotes: 0

Alex Ott
Alex Ott

Reputation: 87069

It's not possible to do, just generate UUID yourself, and insert it. Just use library for your language. If you're using Java driver for Cassandra, you can use functions from UUIDs class: random - to generate random UUID (type 4), or timeBased for UUID for current time (type 1).

Upvotes: 5

Related Questions