本文介绍了Cassandra:插入 timeuuid 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表

create table test(
         userId varchar,
         notifId timeuuid,
         notification varchar,
         time bigint,read boolean,
         primary key(userId, notifId)) with clustering order by (notifId desc);

我正在运行以下查询:

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));

我收到以下错误:

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)

我使用的是 cassandra 1.2.2.

I am using cassandra 1.2.2.

推荐答案

您应该插入通过 datastax 驱动程序生成的 timeuuid.在您的情况下,由于您使用的是版本 1 timeuuid,您必须使用

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

这篇关于Cassandra:插入 timeuuid 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 06:58