Azure Steam 分析查询

问题描述 投票:0回答:1

我想将以下 JSON 数据存储到 Azure 表。

[
  {
    "device1": [
      {
        "name": "temperature",
        "value": 50
      },
      "name": "voltage",
      "value": 220
    }
  ],
  "device2": [
    {
      "name": "temperature",
      "value": 10
    },
    "name": "voltage",
    "value": 200
  }
]
}
]

最终结果如下图所示

我尝试使用简单的 json 格式

  [{
    "messageId": messageId,
    "name": "temperature"
   }]



SELECT
    i.messageId AS messageId,
    i.name AS name,
    i.PartitionId,
    i.EventProcessedUtcTime,
    i.EventEnqueuedUtcTime
INTO
    [table]
FROM
    [input] i
    

但是对于嵌套和动态关键数据我很困惑。

azure azure-storage azure-stream-analytics
1个回答
0
投票

以下是如何使用 C# 和 Azure 表存储 SDK 展平 JSON 数据并将其插入 Azure 表中:

using System;
using System.Collections.Generic;
using Microsoft.Azure.Cosmos.Table;
using Newtonsoft.Json;

public class DeviceEntity : TableEntity
{
    public string Name { get; set; }
    public int Value { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        string json = @"
        [
            {
                'device1': [
                    {
                        'name': 'temperature',
                        'value': 50
                    },
                    {
                        'name': 'voltage',
                        'value': 220
                    }
                ]
            },
            {
                'device2': [
                    {
                        'name': 'temperature',
                        'value': 10
                    },
                    {
                        'name': 'voltage',
                        'value': 200
                    }
                ]
            }
        ]";

        var devices = 
        JsonConvert.DeserializeObject<List<Dictionary<string, 
        List<Dictionary<string, object>>>>>(json);

        List<DeviceEntity> entities = new List<DeviceEntity>();

        foreach (var device in devices)
        {
            foreach (var keyValuePair in device)
            {
                string deviceId = keyValuePair.Key;
                foreach (var sensorData in keyValuePair.Value)
                {
                    string name = sensorData["name"].ToString();
                    int value = Convert.ToInt32(sensorData["value"]);

                    var entity = new DeviceEntity
                    {
                        PartitionKey = deviceId,
                        RowKey = name,
                        Name = name,
                        Value = value
                    };
                    entities.Add(entity);
                }
            }
        }

        // Connect to Azure Table Storage
        string connectionString = "DefaultEndpointsProtocol=https;AccountName=<your-account-name>;AccountKey=<your-account-key>;TableEndpoint=https://<your-account-name>.table.cosmos.azure.com:443/;";
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient(new TableClientConfiguration());
        CloudTable table = tableClient.GetTableReference("YourTableName");

        // Create the table if it doesn't exist
        table.CreateIfNotExists();

        // Batch insert entities into the table
        var batchOperation = new TableBatchOperation();
        foreach (var entity in entities)
        {
            batchOperation.InsertOrMerge(entity);
        }
        table.ExecuteBatch(batchOperation);

        Console.WriteLine("Data inserted into Azure Table Storage successfully.");
    }
}

将 、 和“YourTableName”替换为您的 Azure 存储帐户凭据和 Azure 表的名称。

© www.soinside.com 2019 - 2024. All rights reserved.