在 Impala 表中重新采样...按时间段分组

问题描述

我在 Impala 中有一个表格,其中每秒都有数据。我想每 5 分钟后获得一次数据。给定表:

Time  Data

2021-01-01 00:00:01 123

2021-01-01 00:00:02 145

2021-01-01 00:00:03 456

2021-01-01 00:00:04 698

2021-01-01 00:00:05 589

我需要这样的输出

2021-01-01 00:00:00 123

2021-01-01 00:05:00 458

2021-01-01 00:10:00 784

我知道在 SQL 中使用以下代码很容易:

GROUP BY UNIX_TIMESTAMP(time_stamp) DIV 30

但是当我在 Impala 中尝试此操作时出现错误:

select from_unixtime(ts DIV 1000) as NewTime,ts,unit,Temperature
FROM Sensor_Data.Table
where unit='Unit102'
and cast(ts/1000 as TIMESTAMP) BETWEEN '2020-11-16 00:00:00' and '2021-01-23 00:00:00'
group by from_unixtime(ts DIV 1000) DIV 30

然后我收到以下错误:

算术运算需要数字操作数:from_unixtime(ts DIV 1000) DIV 30

关于如何在 Impala 中实现这一点的任何想法。

谢谢!!!

解决方法

你可以试试下面的sql吗。
我提取分钟(每 5 分钟)和秒(第 0)并添加它们,以便我可以选择第 0、第 5、第 10...分钟。如果总和为 0,这意味着它将始终选择第 5 分钟和 0 秒。

select  ts,unit,Temperature
FROM Sensor_Data.Table
where unit='Unit102'
and cast(ts/1000 as TIMESTAMP) BETWEEN '2020-11-16 00:00:00' and '2021-01-23 00:00:00'
and mod(minute (ts),5)+ second(ts) =0

现在,您的 sql 在 from_unixtime 语法、DIV 输入方面存在一些根本性缺陷。 from_unixtime 这将返回一个字符串。 请让我知道上述 sql 是否适合您。
编辑:通用解决方案-
假设您创建了一个具有采样间隔的新表 - sample_interval 列名 si - 值必须以秒为单位。

select  ts,Temperature
FROM Sensor_Data.Table
Join sample_interval si 
where unit='Unit102'
and cast(ts/1000 as TIMESTAMP) BETWEEN '2020-11-16 00:00:00' and '2021-01-23 00:00:00'
and 
case when si.si >60 then mod(minute (ts),floor(si.si,60) ) + mod (second(ts),mod(si.si,60)) -- floor(si.si,60) will give minutes and mod(si.si,60) will give reminder.
else mod (second(ts),si.si)
end =0

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...