如何使用cassandra查询获取与时间戳列比较的最近6个月数据? [英] How to get Last 6 Month data comparing with timestamp column using cassandra query?

查看:482
本文介绍了如何使用cassandra查询获取与时间戳列比较的最近6个月数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用cassandra查询获取与 timestamp 列进行比较的最近6个月的数据?
我需要获取属于最近3/6个月的所有对帐单,并与 updatedTime(TimeStamp列) CurrentTime
例如,在 SQL 中,我们使用 DateAdd()函数对此进行获取。我不知道如何在cassandra中进行此操作。
如果有人知道,请答复。谢谢。

How to get Last 6 Month data comparing with timestamp column using cassandra query? I need to get all account statement which belongs to last 3/6 months comparing with updatedTime(TimeStamp column) and CurrentTime. For example in SQL we are using DateAdd() function tor this to get. i dont know how to proceed this in cassandra. If anyone know,reply.Thanks in Advance.

推荐答案

Cassandra 2.2及更高版本允许用户定义函数(UDT ),可以将其作为查询结果的一部分应用于表中存储的数据。

Cassandra 2.2 and later allows users to define functions (UDT) that can be applied to data stored in a table as part of a query result.

如果您使用Cassandra 2.2和更高版本的 UDF

You can create your own method if you use Cassandra 2.2 and later UDF

CREATE FUNCTION monthadd(date timestamp, month int)
    CALLED ON NULL INPUT
    RETURNS timestamp
    LANGUAGE java
    AS $$java.util.Calendar c = java.util.Calendar.getInstance();c.setTime(date);c.add(java.util.Calendar.MONTH, month);return c.getTime();$$

此方法接收两个参数


  • 日期时间戳:要添加或减去月数的日期

  • month int:要添加或减去日期的月数(+)

返回日期时间戳

这是您可以使用的方式:

Here is how you can use this :

SELECT * FROM ttest WHERE id = 1 AND updated_time >= monthAdd(dateof(now()), -6) ;

这里monthAdd方法从当前时间戳中减去1 mont,因此该查询将获取上个月的数据

Here monthAdd method subtract 1 mont from the current timestamp, So this query will data of last month

注意:默认情况下,在cassandra.yaml中禁用用户定义的功能-设置enable_user_defined_functions = true可在您意识到安全风险的情况下启用

Note : By default User-defined-functions are disabled in cassandra.yaml - set enable_user_defined_functions=true to enable if you are aware of the security risks

这篇关于如何使用cassandra查询获取与时间戳列比较的最近6个月数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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