正在 Impala 表中重新采样...按时间段分组
Resampling in Impala Tables ... Group by segment of Time
我在 Impala 中有 table,其中我每秒都有数据。我想每 5 分钟获取一次数据。给定 Table :
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
这个returns一个字符串。
如果以上 sql 适合您,请告诉我。
编辑:通用解决方案-
假设您创建一个新的 table,采样间隔为 - sample_interval 列名称 si - 值必须以秒为单位。
select ts, unit, 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
我在 Impala 中有 table,其中我每秒都有数据。我想每 5 分钟获取一次数据。给定 Table :
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
这个returns一个字符串。
如果以上 sql 适合您,请告诉我。
编辑:通用解决方案-
假设您创建一个新的 table,采样间隔为 - sample_interval 列名称 si - 值必须以秒为单位。
select ts, unit, 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