【问题标题】:pass stream with array to multiple rows in SQL by Stream Analytics通过流分析将带有数组的流传递给 SQL 中的多行
【发布时间】:2018-04-06 13:11:53
【问题描述】:

我有来自IoT Hub 的流,例如:

{
    "topic": "saveData",
    "deviceId": "testDevice",
    "data": [
        {
            "timestamp": "2018-04-06T11:46:11.842305",
            "device": "baiTest",
            "variable": "Status01_Test",
            "name": "m_01_test",
            "value": 365
        },
        {
            "timestamp": "2018-04-06T11:46:11.842306",
            "device": "hmuTest",
            "variable": "Status02_Test",
            "name": "m_02_test",
            "value": 817
        },
        {
            "timestamp": "2018-04-06T11:46:11.842307",
            "device": "vwzTest",
            "variable": "Status03_Test",
            "name": "m_03_test",
            "value": 247
        }
    ]
}

我想像这样在SQL DB 中传递这个流:

deviceId    timestamp                   device   variable       name       value
testDevice  2018-04-06T11:46:11.842305  baiTest  Status01_Test  m_01_test  365
testDevice  2018-04-06T11:46:11.842306  hmuTest  Status02_Test  m_02_test  817
testDevice  2018-04-06T11:46:11.842307  vwzTest  Status03_Test  m_03_test  247

到目前为止我的代码是:

WITH itemList AS ( 
    SELECT deviceId, GetArrayElement(data,0) as datas
    FROM [iotHub] WHERE topic = 'saveData' )
SELECT deviceId, datas.timestamp, datas.device, datas.variable, datas.name, datas.value
INTO [sqlTable]
FROM itemList

但这只会将data.array 的第一个索引[0] 存储到SQL 中。 我认为可以使用**GetArrayElements** 函数处理存储数组,但我无法管理它。

【问题讨论】:

    标签: sql arrays azure-sql-database azure-iot-hub azure-stream-analytics


    【解决方案1】:

    您应该使用 GetArrayElements 在 Azure 流分析中展平复杂的 json。请参考以下查询。

    SELECT   
      iothubAlias.deviceId,
      arrayElement.ArrayValue.timestamp,
      arrayElement.ArrayValue.device,
      arrayElement.ArrayValue.variable,
      arrayElement.ArrayValue.name,
      arrayElement.ArrayValue.value
    FROM [iothub-input] as iothubAlias  
    CROSS APPLY GetArrayElements(iothubAlias.data) AS arrayElement  
    

    你会得到你想要的结果。

    【讨论】:

      猜你喜欢
      • 2020-11-06
      • 2011-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-07
      • 2010-10-17
      • 1970-01-01
      相关资源
      最近更新 更多