从 StreamInsight 迁移到 Azure 流分析

Moving to Azure Stream Analytics from StreamInsight

我已经使用 StreamInsight for CEP 几个月了,并且有兴趣迁移到 ASA。我有一个无法从 ASA 文档中回答的问题。在 StreamInsight 中,我可以更改事件的持续时间以从离散的时间点事件创建信号流。例如

var mySignal = from s in myEventStream
                .AlterEventDuration(s => TimeSpan.MaxValue)
                .ClipEventDuration(myEventStream, (s, e) => s.DeviceId == e.DeviceId && s.EventCode == e.EventCode && s.Value != e.Value)
                .ShiftEventTime(e => TimeSpan.FromTicks(1))
                where s.EventCode == "512"
                join e in myEventStream on s.DeviceId equals e.DeviceId
                select new
                    {
                       DeviceId = s.DeviceId,
                       EventCode = s.EventCode,
                       Start = s.Timestamp,
                       End = e.Timestamp,
                       Duration = e.Timestamp - s.Timestamp,
                       Value = s.Value
                    };

我想要做的是检测此 high/low 类型信号与另一时间点事件流之间的重叠。我怎样才能在 ASA 中实现同样的目标?

背景:我有一些设备可以将来自多个源的事件发送到 Azure 事件中心。其中一些事件是来自联系人的 on/off 事件。每个联系类型由 'EventCode' 表示,'Value' 字段表示联系是打开还是关闭。我正在寻找的是一个 ASA 查询,它将告诉我 EventCode 512 何时处于 'On' 状态,同时 EventCode X 也处于 'On' 状态。

解决方案

下面是来自@Andrew 的修改代码,它检测在开关打开时关闭的触点。 'inStream' 在 WITH 语句中定义,时间戳由 Timestamp 字段标记:

SELECT
    SwitchOpen.DeviceId,
    SwitchOpen.Timestamp,
    '511' AS [EventCode],
    'Switch is Open' AS [EventDescription],
    SwitchOpen.Value,
    SwitchOpen.LocationCode
INTO [outStream]
FROM
    [inStream] AS SwitchOpen
    INNER JOIN [inStream] AS ContactClosed
    ON SwitchOpen.DeviceId = ContactClosed.DeviceId
    AND DateDiff(second, SwitchOpen, ContactClosed) BETWEEN 1 AND 3600
    LEFT JOIN [inStream] AS SwitchClosed
    ON SwitchOpen.DeviceId = SwitchClosed.DeviceId
    AND SwitchClosed.EventCode = SwitchOpen.EventCode
    AND SwitchClosed.Value != SwitchOpen.Value
    AND DateDiff(second, SwitchOpen, SwitchClosed) BETWEEN 1 AND 3600
    AND DateDiff(second, SwitchClosed, ContactClosed) BETWEEN 1 AND 3600
WHERE
SwitchOpen.Value = 0.0
AND SwitchOpen.EventCode = '256'
AND ContactClosed.Value = 1.0
AND ContactClosed.EventCode = '512'
AND SwitchClosed.Timestamp IS NULL

我没有你的具体数据,但这种格式应该符合你的要求。

SELECT
    DeviceOn.Time AS StartFault,
    Device2On.Time AS Endfault,
    DATEDIFF(second, DeviceOn.Time, Device2On.Time) AS FaultDuraitonSeconds
    DeviceOn.ID ,
    DeviceOn.EventCode,
    DeviceOn.Value
FROM
    Input AS DeviceOn TIMESTAMP by Time
    INNER JOIN Input as Device2On TIMESTAMP by Time on DeviceOn.DeviceId equals Device2On.DeviceId
    ON DATEDIFF(Second, DeviceOn, Device2On) BETWEEN 1 AND 3600
    INNER JOIN Input AS Device2Off TIMESTAMP BY Time
    ON DATEDIFF(second, DeviceOn , Device2Off ) BETWEEN 1 AND 3600
    AND DATEDIFF(second, Device2Off , DeviceOn) BETWEEN 1 AND 3600
    LEFT JOIN Input AS DeviceOff TIMESTAMP BY Time
    ON DATEDIFF(second, DeviceOn , DeviceOff ) BETWEEN 1 AND 3600
    AND DATEDIFF(second, DeviceOff , DeviceOn ) BETWEEN 1 AND 3600
WHERE
DeviceOn.Value = "On"
AND Device2On.Value =Null

类似于查询示例:在此处检测条件的持续时间https://azure.microsoft.com/en-us/documentation/articles/stream-analytics-stream-analytics-query-patterns/