Azure 流分析 - 从组中的最后一个事件中获取字段?
Azure Stream Analytics - Get field from last event in group?
我有以下查询,它结合了来自两个输入的事件。
WITH combined AS (
SELECT a.deviceId, temperature, 'a' as source FROM [inputA] a timestamp by a.devicetime
UNION
SELECT b.deviceId, temperature, 'b' as source FROM [inputB] b timestamp by b.devicetime
)
SELECT c.deviceId, system.Timestamp as 'windowend', avg(c.temperature) as 'avg_temperature'
INTO [ehA-output]
FROM combined c
GROUP BY c.deviceId, TumblingWindow(Duration(second, 10), Offset(second, 1))
我还没有弄清楚的是:如何将字段 "source" 添加到第二部分的输出中,而值应该取自 window 组中的最后一个事件?
所以我想象了一些类似伪代码的东西(注意:LAST() 实际上是一个现有的函数,但据我所知不是为了这个目的)。
SELECT c.deviceId, ..., LAST(source) as sourceOfLastEvent
...
尝试以下查询(请调整您的时间 window,因为我出于测试目的更改了它):
WITH combined AS (
SELECT a.deviceId, temperature, 'a' as source, a.deviceTime FROM [input] a timestamp by a.devicetime
UNION
SELECT b.deviceId, temperature, 'b' as source, b.deviceTime FROM [input1] b timestamp by b.devicetime
),
result AS (
SELECT c.deviceId, system.Timestamp as 'windowend', avg(c.temperature) as 'avg_temperature', topone() over (order by deviceTime desc)
FROM combined c
GROUP BY c.deviceId, TumblingWindow(Duration(minute, 1), Offset(second, 1))
)
select result.topone.source, result. *
into output
from result
在这里您可以看到,在第一个子查询中,传播了 deviceTime,在第二个子查询中,我们对 topone 元素进行了排序由设备时间递减。
这应该从时间 window 获取最后一个事件,但不会更改 GROUP BY 子句,因为 topone() 函数是一个聚合表达式。
最后在结果中,从 topone 对象,我们只是将源 属性 传播到输出。
旁注:根据我的测试,这里的 UNION 子句似乎需要源 'a' 和源 'b' 有事件才能产生输出,如果您对这些事件有实时要求,这可能很重要,例如只有输入源 'a' 正在获取事件。
我有以下查询,它结合了来自两个输入的事件。
WITH combined AS (
SELECT a.deviceId, temperature, 'a' as source FROM [inputA] a timestamp by a.devicetime
UNION
SELECT b.deviceId, temperature, 'b' as source FROM [inputB] b timestamp by b.devicetime
)
SELECT c.deviceId, system.Timestamp as 'windowend', avg(c.temperature) as 'avg_temperature'
INTO [ehA-output]
FROM combined c
GROUP BY c.deviceId, TumblingWindow(Duration(second, 10), Offset(second, 1))
我还没有弄清楚的是:如何将字段 "source" 添加到第二部分的输出中,而值应该取自 window 组中的最后一个事件?
所以我想象了一些类似伪代码的东西(注意:LAST() 实际上是一个现有的函数,但据我所知不是为了这个目的)。
SELECT c.deviceId, ..., LAST(source) as sourceOfLastEvent
...
尝试以下查询(请调整您的时间 window,因为我出于测试目的更改了它):
WITH combined AS (
SELECT a.deviceId, temperature, 'a' as source, a.deviceTime FROM [input] a timestamp by a.devicetime
UNION
SELECT b.deviceId, temperature, 'b' as source, b.deviceTime FROM [input1] b timestamp by b.devicetime
),
result AS (
SELECT c.deviceId, system.Timestamp as 'windowend', avg(c.temperature) as 'avg_temperature', topone() over (order by deviceTime desc)
FROM combined c
GROUP BY c.deviceId, TumblingWindow(Duration(minute, 1), Offset(second, 1))
)
select result.topone.source, result. *
into output
from result
在这里您可以看到,在第一个子查询中,传播了 deviceTime,在第二个子查询中,我们对 topone 元素进行了排序由设备时间递减。
这应该从时间 window 获取最后一个事件,但不会更改 GROUP BY 子句,因为 topone() 函数是一个聚合表达式。
最后在结果中,从 topone 对象,我们只是将源 属性 传播到输出。
旁注:根据我的测试,这里的 UNION 子句似乎需要源 'a' 和源 'b' 有事件才能产生输出,如果您对这些事件有实时要求,这可能很重要,例如只有输入源 'a' 正在获取事件。