我的目标是从现在开始删除固定的时间()。所以我总是得到最后五分钟,或者最后五个小时。
我该如何实现?
cassandra上的可以从时间戳或a添加(+)或减去( - )持续时间 创建新时间戳或日期的日期。例如:
SELECT * FROM myTable WHERE t = '2017-01-01' - 2d
将选择2016年最后2天内t值为t的所有记录。
在cqlsh
内,show version;
给了我:
[cqlsh 5.0.1 | Cassandra 3.11.0 | CQL spec 3.4.4 | Native protocol v4]
我使用下表进行测试:
cqlsh:> CREATE TABLE t (
... ts timestamp,
... PRIMARY KEY (ts)
... )
... WITH compression = {'class': 'LZ4Compressor'}
... AND gc_grace_seconds = 60;
以下查询有效:
SELECT (float)1.55 FROM t WHERE (ts <= toTimestamp(now()));
以下内容:
cqlsh:> SELECT (float)1.55 FROM t WHERE (ts <= toTimestamp(now() - 1d));
SyntaxException: line 1:57 mismatched input '-' expecting ')' (...ts <= toTimestamp(now() [-]...)
cqlsh:> SELECT (float)1.55 FROM t WHERE (ts <= toTimestamp(now()) - 1d);
SyntaxException: line 1:58 mismatched input '-' expecting ')' (... <= toTimestamp(now()) [-]...)
cqlsh:> SELECT (float)1.55 FROM t WHERE (ts <= toTimestamp(now()) - 1m);
SyntaxException: line 1:58 mismatched input '-' expecting ')' (... <= toTimestamp(now()) [-]...)
cqlsh:> SELECT (float)1.55 FROM t WHERE ts <= toTimestamp(now()) - 1m;
SyntaxException: line 1:57 mismatched input '-' expecting EOF (... <= toTimestamp(now()) [-]...)
cqlsh:> SELECT (float)1.55 FROM t WHERE ts = toTimestamp(now()) - 1m;
SyntaxException: line 1:56 mismatched input '-' expecting EOF (... = toTimestamp(now()) [-]...)
正如您所看到的,在尝试减去持续时间时,错误始终与-
有关。 (顺便说一句,结果与+
)
我做错了什么?可能有办法实现它,但我无法弄明白!
为了记录,正如@ Ashraful-Islam所建议的,我的解决方案是创建一个UDF来完成这项工作。见下文:
CREATE FUNCTION IF NOT EXISTS timeAgo(minutes int)
CALLED ON NULL INPUT
RETURNS timestamp
LANGUAGE java AS '
long now = System.currentTimeMillis();
if (minutes == null)
return new Date(now);
return new Date(now - (minutes.intValue() * 60 * 1000));
';
-- So I can do
SELECT timeAgo(60) FROM t;
答案 0 :(得分:1)
Cassandra 4.0中引入的算术运算符
- 添加对算术运算符的支持(CASSANDRA-11935)
来源:https://github.com/apache/cassandra/blob/trunk/CHANGES.txt#L124
<强>被修改强>
如果您的cassandra版本低于4.0,则必须从应用程序层执行此操作或创建用户定义函数(UDF)。请查看以下链接