在 Azure SQL 数据库中存储 D2C 消息

Store D2C messages in Azure SQL database

我正在尝试将从 Cloudrail.Box 接收到的传感器读数作为 JSON 有效负载存储到 Azure SQL 数据库中。 我当前使用的边缘函数根据设置的频率批量输出消息(如下所示)。 { “Temp_V_Raw”:0.789, “Pressure_V_Raw”:0.006, “Temp_C”:23.67, “Pressure_Bar”:1.8450718792014, “时间戳”:1617990070392, “日期”:“2021 年 4 月 9 日星期五 19:41:10 GMT+0200 (CEST)” }

如何将其以表格格式存储在 Azure SQL 数据库中?

我建议您创建一个存储过程来处理 json 数据,然后通过存储过程将 JSON 数据传递到 Azure SQL 数据库中。

参考存储过程示例:

CREATE TABLE dbo.SystemRecord(
    RecordedDateTime        datetime2(0) NOT NULL,
    RecordedDateTimeLocal   datetime2(0) NOT NULL,
    CpuPctProcessorTime     smallint     NOT NULL,
    MemAvailGbytes          smallint     NOT NULL
)

CREATE PROCEDURE dbo.InsertSystemRecordData
 
@json NVARCHAR(max)

AS
BEGIN
 
INSERT INTO dbo.SystemRecord (
  [RecordedDateTime]
, [RecordedDateTimeLocal]
, [CpuPctProcessorTime]
, [MemAvailGbytes])
 
    SELECT
        RecordedDateTime
       ,RecordedDateTimeLocal
       ,CpuPctProcessorTime
       ,MemAvailGbytes
    FROM OPENJSON(@json)
    WITH (
      RecordedDateTime      DATETIME2(0) '$.dateTime'
    , RecordedDateTimeLocal DATETIME2(0) '$.dateTimeLocal'
    , CpuPctProcessorTime   SMALLINT     '$.cpuPctProcessorTime'
    , MemAvailGbytes        SMALLINT     '$.memAvailGbytes'
    ) AS jsonValues
 
END

EXEC dbo.InsertSystemRecordData @json ='{"dateTime":"2018-03-19T15:15:40.222Z","dateTimeLocal":"2018-03-19T11:15:40.222Z","cpuPctProcessorTime":"0","memAvailGbytes":"28"}'

您可以参考这些链接:

  1. How to Insert JSON Data into SQL Server Database

HTH.