问题
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.
回答1:
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
回答2:
It looks like a bug (similar or the same as https://issues.apache.org/jira/browse/CASSANDRA-5472) , and you're using pretty old version of Cassandra. I recommend you to upgrade to the latest 1.2.x release, retest your scenario and file an issue, if the problem still persists
来源:https://stackoverflow.com/questions/23191933/cassandra-inserting-timeuuid-error