Search code examples
cassandracassandra-2.0cassandra-2.1

Insert row with auto increment


I create this table

CREATE TABLE table_test(
   id uuid PRIMARY KEY,
   varchar,
  description varchar
);

And I was expecting that since id was uuid, it would be auto incremented automatically

INSERT INTO table_test (title,description) VALUES ('a','B');

It throw error

com.datastax.driver.core.exceptions.InvalidQueryException: Some partition key parts are missing: id
  com.datastax.driver.core.exceptions.InvalidQueryException: Some partition key parts are missing: id

Any idea what I´m doing wrong.


Solution

  • Use now() function

    In the coordinator node, generates a new unique timeuuid in milliseconds when the statement is executed. The timestamp portion of the timeuuid conforms to the UTC (Universal Time) standard. This method is useful for inserting values. The value returned by now() is guaranteed to be unique.

    There is no auto increment option in Cassandra.

    now() function return a timeuuid, it is not auto increment. It has two bigint part MSB and LSB. MSB is the current timestamp and LSB is the combination of clock sequence and node ip of the host. And It is universal unique.

    Use the below query to insert :

    INSERT INTO table_test (id,title,description) VALUES (now(),'a','B');
    

    Source : http://docs.datastax.com/en/cql/3.3/cql/cql_reference/timeuuid_functions_r.html