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

huangapple go评论78阅读模式
英文:

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"的类型是"timestamp"。我不能将其修改为"TimeUUID"类型。

我得到了这个错误:

com.datastax.driver.core.exceptions.InvalidQueryException: 类型错误:
无法将函数system.now(类型为timeuuid)的结果分配给update_time(类型为timestamp)

函数调用QueryBuilder.now()返回Java类com.datastax.driver.core.querybuilder.Utils$FCall的实例。

我已经搜索过了,但文档没有说明如何在驱动程序中使用这个now()函数。

英文:

I've tried using the code below:

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

The column "UPDATE_TIME" is of type "timestamp". I can't modify it to "TimeUUID" type.

I'm getting this error:

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

The function call QueryBuilder.now() returns a Java instance of class: com.datastax.driver.core.querybuilder.Utils$FCall.

I've searched around, but the documentation doesn't specify how to use this now() function using the driver.

答案1

得分: 3

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

对于Java驱动程序4.x,将其翻译为以下内容:不再使用QueryBuilder.now(),而是需要使用QueryBuilder.toTimestamp(QueryBuilder.now())

对于Java驱动程序3.x,您需要将QueryBuilder.now()包装在QueryBuilder.fcall中,类似于这样:QueryBuilder.fcall("toTimestamp", QueryBuilder.now())

英文:

in CQL you need to use following expression: toTimestamp(now()).

For Java driver 4.x it's translated into the following: instead of QueryBuilder.now(), you need to use QueryBuilder.toTimestamp(QueryBuilder.now().

For Java driver 3.x, you need to wrap QueryBuilder.now() into the QueryBuilder.fcall, something like this: QueryBuilder.fcall("toTimestamp", QueryBuilder.now())

huangapple
  • 本文由 发表于 2020年10月24日 23:11:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64514859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定