如何确定Cassandra查询的时间戳

时间:2018-07-20 20:02:58

标签: cassandra cassandra-2.0

插入表中的值之一是当前时间。。我使用 i := 1 for i <= 223129 { (some other code to prepare the data and create the chart) img := vgimg.New(450, 600) dc := draw.New(img) canvases := table.Align(plots, dc) plots[0][0].Draw(canvases[0][0]) plots[1][0].Draw(canvases[1][0]) plots[2][0].Draw(canvases[2][0]) testFile := "/media/Snaps/pics/image"+strconv.Itoa(i+60)+"_"+gain_loss+".png" w, err := os.Create(testFile) if err != nil { panic(err) } defer w.Close() png := vgimg.PngCanvas{Canvas: img} if _, err := png.WriteTo(w); err != nil { panic(err) } //move to next image i = i + 1 } 计算当前时间。现在,我要计算当前时间减去90天,当前时间减去15天。 我的问题是如何计算toTimestamp(now())

查询当前时间戳:

current time  -  nth day

在上面的查询中,val_2是当前时间戳。当前时间戳由

确定
INSERT INTO TABLE_NAME (col_1, col_2, col_3) VALUES ('val_1', toTimestamp(now()), val_3);

如何计算toTimestamp(now())

1 个答案:

答案 0 :(得分:2)

此功能未内置在CQL中。

如果您能够使用UDF,则可以(以此处给出的示例为基础: How to get Last 6 Month data comparing with timestamp column using cassandra query?)执行以下操作:

通过在cassandra.yaml中添加此行或将其更改为true来启用UDF:

enable_user_defined_functions: true

然后添加两个user defined functions,如下所示:

CREATE FUNCTION dateadd(date timestamp, daydiff 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.DATE, daydiff);return c.getTime();$$

CREATE FUNCTION weekadd(date timestamp, weekdiff 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.DATE, weekdiff*7);return c.getTime();$$

从表中选择数据,如下所示:

select dateadd(col_2,-90) from TABLE_NAME;
select weekadd(col_2,-2) from TABLE_NAME;