Cassandra: inserting timeuuid error

cassandra, cql, datastax-java-driver

Solution

You should insert the timeuuid generated via datastax driver. In your case , since you are using version 1 timeuuid, you must use

UUIDs.timeBased()

You can find the above in following reference

http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/utils/UUIDs.html

Problem

I have following table ``` create table test( userId varchar, notifId timeuuid, notification varchar, time bigint,read boolean, primary key(userId, notifId)) with clustering order by (notifId desc); ``` I am running following query: ``` PreparedStatement pstmt = session.prepare("INSERT INTO notifications(userId, notifId, notification, time, read) VALUES(?, now(), ?, ?, ?)"); BoundStatement boundStatement = new BoundStatement(pstmt); session.execute(boundStatement.bind("123", "hello", new Date().getTime(), false)); ``` I am getting following error: ``` Exception in thread "main" com.datastax.driver.core.exceptions.InvalidQueryException: Type error: cannot assign result of function now (type timeuuid) to notifid (type 'org.apache.cassandra.db.marshal.ReversedType(org.apache.cassandra.db.marshal.TimeUUIDType)') at com.datastax.driver.core.exceptions.InvalidQueryException.copy(InvalidQueryException.java:35) at com.datastax.driver.core.ResultSetFuture.extractCause(ResultSetFuture.java:277) at com.datastax.driver.core.Session.toPreparedStatement(Session.java:281) at com.datastax.driver.core.Session.prepare(Session.java:172) at com.example.cassandra.SimpleClient.loadData(SimpleClient.java:130) at com.example.cassandra.SimpleClient.main(SimpleClient.java:214) Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: Type error: cannot assign result of function now (type timeuuid) to notifid (type 'org.apache.cassandra.db.marshal.ReversedType(org.apache.cassandra.db.marshal.TimeUUIDType)') at com.datastax.driver.core.ResultSetFuture.convertException(ResultSetFuture.java:307) ``` I am using cassandra 1.2.2.

Original source