在 cassandra cql select 中获取当前日期 [英] Get current date in cassandra cql select

查看:68
本文介绍了在 cassandra cql select 中获取当前日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 SQL 中,我能够做到:

In SQL, I am able to do:

select getdate(), getdate() - 7

返回当前日期和当前日期 - 7 天.我想在 Cassandra CQL 中实现相同的目标.我试过了:

Which returns the current date as well as current date - 7 days. I want to achieve the same in Cassandra CQL. I tried:

select dateof(now())

但这行不通.它仅适用于插入而不适用于选择.我怎样才能得到相同的?任何帮助将不胜感激.

But that does not work. It works only on insert and not in select. How can I get the same? Any help would be appreciated.

推荐答案

select dateof(now())

就其本身而言,您是对的,这是行不通的.但是如果你有一个你知道只有一行的表(比如 system.local):

On its own, you are correct, that does not work. But if you have a table that you know only has one row (like system.local):

aploetz@cqlsh:stackoverflow> SELECT dateof(now()) FROM system.local ;

 dateof(now())
--------------------------
 2015-03-26 03:18:39-0500

(1 rows)

不幸的是,Cassandra CQL 没有(还没有?CASSANDRA-5505)包括对算术运算的支持,更不用说日期算术了.因此,从该值中减去 7 天是您必须在应用程序级别执行的操作.

Unfortunately, Cassandra CQL does not (yet? CASSANDRA-5505) include support for arithmetic operations, let alone date arithmetic. So subtracting 7 days from that value is something that you would have to do in your application level.

编辑 20200422

较新的语法使用 toTimestamp() 函数代替:

The newer syntax uses the toTimestamp() function instead:

aploetz@cqlsh> SELECT toTimestamp(now()) FROM system.local;

 system.totimestamp(system.now())
----------------------------------
  2020-04-22 13:22:04.752000+0000

(1 rows)

这两种语法都适用于 20200422.

Both syntaxes work as of 20200422.

这篇关于在 cassandra cql select 中获取当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆