Search code examples
selectcassandrainsertcql

How to get next id from cassandra table


I need to insert next incremental id into the table Tried to use the syntax as mysql.

INSERT INTO demo.log ( id, transaction_type , transaction_time, transaction_data, session_id ) VALUES ( select max(id)+1 from demo.log ,'master' ,'2017-05-27 14:30:54.234' ,'1›6378925487955990›1›0›251›96544›/Date(1474519292176+0800)/›200›3›1›0' ,'789789d7f9-k97f8gh9f8f-001' );

Error:

SyntaxException: line 1:117 no viable alternative at input 'select' (..., session_id ) VALUES ( [(]select...)


Solution

  • You cannot do that (sub queries), there is no feature to support it and it will not be supported. You can make something like this work with counters or LWTs but its a really bad idea.

    I would recommend you use a type 1 or 4 uuids for the id (set type to timeuuid or uuid). In a distributed system atomically incrementing things like this is horribly expensive, and complex.