简体   繁体   English

KSQL - 相对时间查询 (ANSI SQL)

[英]KSQL - Relative time queries (ANSI SQL)

How can i write a KSQL query on a bigint column that uses relative time?如何在使用相对时间的 bigint 列上编写 KSQL 查询?

IE How can i achieve something like this.... IE 我怎样才能实现这样的目标......

SELECT timestamp,value FROM XYZ where timestamp > now() - INTERVAL 5 MINUTES

Where timestamp is a bigint field;其中 timestamp 是一个 bigint 字段; milliseconds since epoch.自纪元以来的毫秒数。

EDIT: Context.编辑:上下文。 I need these queries to be passed over the network/database savable and we have many queries that get the last 20mins of data from a stream on a BIGINT field.我需要通过可保存的网络/数据库传递这些查询,并且我们有许多查询从 BIGINT 字段上的 stream 获取最后 20 分钟的数据。

IE SELECT timestamp from STREAMNAME where timestamp > 1592882020842 SELECT timestamp from STREAMNAME where timestamp > 1592882020842

The problem with saving/sending this query as more and more time elapses since the point where it was saved therefore moving further and further away from "20mins ago".自从保存/发送此查询以来,随着时间的推移越来越多,保存/发送此查询的问题因此越来越远离“20 分钟前”。

What i acutally want is a query like this.我真正想要的是这样的查询。

SELECT timestamp from STREAMNAME where timestmap > now() - 20mins

No matter how many times this query is saved/loaded/sent across network/run in future.无论此查询被保存/加载/通过网络发送/将来运行多少次。 Will always return the timestamps from 20mins ago -> now将始终返回 20 分钟前的时间戳 -> 现在

I have since found something in the documentatino that achives what i want UNIX_TIMESTAMP() which returns the time in milliseconds in BIGINT format.从那以后,我在文档中发现了一些可以实现我想要的UNIX_TIMESTAMP()的东西,它以 BIGINT 格式返回以毫秒为单位的时间。

UNIX_TIMESTAMP returns the current wallclock time in milliseconds at the time the row is processed , ie it returns the processing time. UNIX_TIMESTAMP在处理行时返回当前挂钟时间(以毫秒为单位),即返回处理时间。 The timestamp returned by the function will advance as each row is processed. function 返回的时间戳将随着每一行的处理而提前。

This is different to how a traditional database would handle now , which would be evaluated once, and this single immutable timestamp would be used for the duration of the query, regardless of how long the query took to run.这与传统数据库now的处理方式不同,后者将被评估一次,并且这个单一的不可变时间戳将用于查询期间,无论查询运行多长时间。

It's important to understand the difference.了解差异很重要。 If UNIX_TIMESTAMP does what you want... then great!如果UNIX_TIMESTAMP您的需求……那就太好了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM