[英]Azure Streaming Analytics stateful aggregation
我有一个IoT中心,其中有几个将数据发送到流分析的设备。 来自设备的消息包含有关其处于浮动状态(0到1之间)的运行状况的信息。 流分析将数据输出到服务总线,我想添加有关字段的信息,该信息包含给定时刻各个设备的平均运行状况。
我想使用用户定义的聚合每10秒产生一次此值,但看起来它仅使用时间范围内的最后一条消息。
我是否正确使用了UDA,如果没有,是否可以通过其他方法对多个设备或某些其他有状态功能进行平均计算?
UDA代码:
function main() {
this.init = function () {
this.state = {};
}
this.accumulate = function (value, device_id) {
this.state[device_id] = value;
}
/*this.deaccumulate = function (value, timestamp) {
this.state -= value;
}
this.deaccumulateState = function (otherState) {
this.state -= otherState.state;
}*/
this.computeResult = function () {
length = 0,
total = 0;
for (var device in this.state) {
total += this.state[device];
length++;
}
return total/length;
}
}
查询:
SELECT
uda.fleetHealth(device_health_status.level, device_id) as avg_health
INTO
bustopic2
FROM
iotdata
GROUP BY TumblingWindow(second, 10)
您只能使用Java脚本中的map来获取最后一条消息,如1。 2第二个参数始终相同,并且等于应用程序时间戳,即使您将其定义为device_id。 如果要计算所有设备的平均水平,则应执行以下操作:
function UDASample() {
this.init = function () {
this.state = 0;
this.length = 0;
}
this.accumulate = function (value, timestamp) {
this.state += value;
this.length = length + 1;
}
/*this.deaccumulate = function (value, timestamp) {
this.state -= value;
}
this.deaccumulateState = function (otherState) {
this.state -= otherState.state;
}*/
this.computeResult = function () {
return this.state/this.length;
}
}
SELECT
uda.fleetHealth(device_health_status.level) as avg_health
INTO
bustopic2
FROM
iotdata
GROUP BY TumblingWindow(second, 10)
如果要统计每个设备的平均水平,则可以使用上面相同的UDA并使用如下脚本:
SELECT device_id,
uda.fleetHealth(device_health_status.level) as avg_health
INTO
bustopic2
FROM
iotdata
GROUP BY TumblingWindow(second, 10), device_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.