流分析查询以从设备数据中获取 EnqueuedTime device_id

stream analytics query to get EnqueuedTime device_id from device data

我正在尝试编写流分析查询以将 iothub 数据推送到 SQL 数据库。

我的IOTHUB数据如下

{
    "Device_Id":"P371602011",
    "kWL1":11.683551775144204,
    "EventProcessedUtcTime":"2017-12-28T07:21:14.3829760Z",
    "PartitionId":0,
    "EventEnqueuedUtcTime":"2017-12-28T07:21:04.6660000Z",
    "IoTHub":
    {
        "MessageId":null,
        "CorrelationId":null,
        "ConnectionDeviceId":"iotclient",
        "ConnectionDeviceGenerationId":"636500361000571958",
        "EnqueuedTime":"2017-12-28T07:21:04.0540000Z",
        "StreamId":null
    }
}

现在我正在尝试将 Device_Id、kWL1 和 EnqueuedTime 写入 SQL 数据库。我的流分析查询如下所示。

SELECT
Device_Id AS PowerScout,
IoTHub.EnqueuedTime AS [ReadingTime],
kWL1 AS [kW L1]
INTO
[DataBase]
FROM
[IoTHub]

当我 运行 流分析作业时,我可以看到 Device_Id 和 kWL1 值,但 EnqueuedTime 显示为 NULL。 如何从 IOTHub 数据中获取 EnqueuedTime。

{
"DeviceData": [
    {
        "Device_Id": "5",
        "AMPSL1": 1.2515641182178531E-38
   }
],
"EventEnqueuedUtcTime": "2018-01-08T05:03:08.0840000Z",
"IoTHub": {
    "MessageId": null,
    "CorrelationId": null,
    "ConnectionDeviceId": "VHW1",
    "ConnectionDeviceGenerationId": "636509839893748612",
    "EnqueuedTime": "2018-01-08T05:03:06.7460000Z",
    "StreamId": null
}
}

如何获得上述格式的 Device_Id 和 AMPSL1。

提前致谢

根据您提供的信息,我认为问题在于名称 IoTHub 是重复的,一个是您的流分析中的 inputs,但同时 IoTHub 是您 data.You 中的一个对象,可以将名称修改为 IoTHubMessageContent (例如,您可以将其更改为其他名称)在您的数据如下。

{
   "Device_Id":"P371602011",
   "kWL1":11.683551775144204,
   "EventProcessedUtcTime":"2017-12-28T07:21:14.3829760Z",
   "PartitionId":0,
   "EventEnqueuedUtcTime":"2017-12-28T07:21:04.6660000Z",
   "IoTHubMessageContent":
   {
       "MessageId":null,
       "CorrelationId":null,
       "ConnectionDeviceId":"iotclient",
       "ConnectionDeviceGenerationId":"636500361000571958",
       "EnqueuedTime":"2017-12-28T07:21:04.0540000Z",
       "StreamId":null
   }
}

然后修改查询如下。

SELECT
Device_Id AS PowerScout,
IoTHubMessageContent.EnqueuedTime AS [ReadingTime],
kWL1 AS [kW L1]
INTO
[DataBase]
FROM
[IoTHub]

之后执行查询命令时,结果就出来了

另一种方法是如下修改查询,以便能够读取 IoTHub 输入中名为 IoTHub 的嵌套 JSON 对象:

SELECT
Device_Id AS PowerScout,
[IoTHub].[IoTHub].EnqueuedTime AS [ReadingTime],
kWL1 AS [kW L1]
INTO
[DataBase]
FROM
[IoTHub]

在@Michael Xu - MSFT 的帮助下,我终于成功了。最终查询如下所示。

WITH DeviceDataCollection AS 
( 
     SELECT GetArrayElement(DeviceData,0)as DeviceData, 
     IoTHub.EnqueuedTime as time FROM IoTHubIn     
) 
SELECT 
     DeviceData.Device_Id AS PowerScout,
     time AS [TimeStamp]
INTO
[DataBase]
FROM 
[DeviceDataCollection]