如何在 Cassandra 的服务器端存储时间戳?

How to store the timestamp in the server side for Cassandra?

我试过使用下面的代码:

    final Session session = connection.getSession();
    final String keyspaceName = session.getLoggedKeyspace();
    psInsert = session.prepare(QueryBuilder
            .insertInto(keyspaceName, CampaignConstants.TABLE_NAME_SAMPLE)
            .value("ID", QueryBuilder.bindMarker())
            .value("NAME", QueryBuilder.bindMarker())
            .value("UPDATE_TIME", QueryBuilder.now()));

列“UPDATE_TIME”的类型为“时间戳”。我无法将其修改为“TimeUUID”类型。

我收到这个错误:

com.datastax.driver.core.exceptions.InvalidQueryException: Type error: cannot assign result of function system.now (type timeuuid) to update_time (type timestamp)

函数调用 QueryBuilder.now() returns Java class 实例:com.datastax.driver.core.querybuilder.Utils$FCall.

我四处搜索,但文档没有具体说明如何使用驱动程序使用此 now() 函数。

在 CQL 中您需要使用以下表达式:toTimestamp(now()).

对于 Java 驱动程序 4.x 它被翻译成以下内容:而不是 QueryBuilder.now(),您需要使用 QueryBuilder.toTimestamp(QueryBuilder.now().

对于Java驱动程序3.x,你需要将QueryBuilder.now()包装到QueryBuilder.fcall中,像这样:QueryBuilder.fcall("toTimestamp", QueryBuilder.now())