如何将json数据存储到azure表存储中,该存储应该从单个json字符串添加为每行?

时间:2017-09-18 09:15:15

标签: azure azure-table-storage

我有以下格式的消息结构

{"deviceId":"D1","t":"2017-07-07T12:31:21Z","i1":"112.50","i2":"150.75","i3":"406.25","type":"Instant"}

我要做的是将此数据以下列格式保存到azure表存储中

enter image description here

我可以将i1,i2,i3作为列添加到一行中。但是我需要在指定的结构中使用它。 以下查询用于流分析,以将数据存储到表存储。这很有效。但我需要额外的表来存储上面指定的数据。

SELECT
deviceId,
TRY_CAST(t AS datetime) as Row,
i1,
i2,
i3,
type
INTO
[iothubstorage] 
FROM
[iotinput] timestamp by t

这很有效。但我需要额外的表来存储上面指定的数据。这可能吗?

1 个答案:

答案 0 :(得分:1)

您可以使用UNION组合3行,下面的示例代码供您参考。我对它进行了测试,它在我身边工作得很好。

With firstQueryResult AS (SELECT
CONCAT(TRY_CAST(deviceId as nvarchar(max)) , ' - i1') as PartitionKey,
t as RowKey,
TRY_CAST(t AS datetime) as TimeReceived,
CONCAT(TRY_CAST(deviceId as nvarchar(max)) , ' - i1') as deviceId,
'i1' as Input,
i1 as Value FROM [iotinput] 
UNION 
SELECT
CONCAT(TRY_CAST(deviceId as nvarchar(max)) , ' - i2') as PartitionKey,
t as RowKey,
TRY_CAST(t AS datetime) as TimeReceived,
CONCAT(TRY_CAST(deviceId as nvarchar(max)) , ' - i2') as deviceId,
'i2' as Input,
i2 as Value FROM [iotinput]
UNION 
SELECT
CONCAT(TRY_CAST(deviceId as nvarchar(max)) , ' - i3') as PartitionKey,
t as RowKey,
TRY_CAST(t AS datetime) as TimeReceived,
CONCAT(TRY_CAST(deviceId as nvarchar(max)) , ' - i3') as deviceId,
'i3' as Input,
i3 as Value FROM [iotinput])

SELECT * INTO
[iothubstorage] FROM firstQueryResult