sql - 通过流分析将带有数组的流传递到 SQL 中的多行

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

我有一个来自 IoT 中心的流,例如:

{
    "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数据库中传递这个流,如下所示:

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** 函数来处理,但我无法管理它。

最佳答案

您应该使用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  

你会得到你想要的结果。 enter image description here

关于sql - 通过流分析将带有数组的流传递到 SQL 中的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49693639/

相关文章:

SQL - 连接两个表并计算项目

java - 在 jpa 标准中, "in case there is at least 1 row return true"

javascript - 在 ng 类中的数组上使用的表达式

javascript - 使用 .some() 比较数组

azure - 以编程方式安排 Azure 函数的一次性执行

sql-server - Azure SQL 管理实例 - 恢复数据库不同版本

mysql - 我怎样才能实现这个SQL表连接呢?

mysql - 使用 ORDER BY 和 MATCH AGAINST 优化选择查询

Python NumPy : replace values in one array with corresponding values in another array

linq-to-sql - SQL Azure : "The instance of SQL Server you attempted to connect to requires encryption but this machine does not support it"