azure - 将数据从 Azure 流分析存储到 Blob 存储时,是否可以在路径中添加 DeviceID

标签 azure azure-storage azure-iot-hub azure-stream-analytics

我将数据从不同的设备传入 IoT 中心,并使用流分析对其进行处理并将其存储在 Blob 存储中。 我知道我们可以根据需要的格式在路径中添加我们添加的{date}{time},在该路径中我们也可以添加deviceId。

示例:2018/10/30/01(日期/月/日/小时)可以在存储到 blob 时在该路径中添加/deviceId enter image description here

最佳答案

以下是针对您的情况的解决方法示例。它基于使用 azure 函数 (HttpTrigger) 进行输出 ASA 作业,以推送方式将数据附加到特定的 Blob 存储。 请注意,以下解决方法使用最大批处理计数将事件传递到 azure 函数值 1(一次一个遥测数据)。

ASA 职位查询:

SELECT
  System.Timestamp as [time], * 
INTO outAF
FROM 
  iot TIMESTAMP BY time

Azure 函数(HttpTrigger):

运行.csx

#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Microsoft.WindowsAzure.Storage.Blob;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public static async Task<IActionResult> Run(string body, CloudBlobContainer blobContainer, ILogger log)
{
    log.LogInformation($"{body}");

    var jtoken = JToken.Parse(body);
    var jobject = jtoken is JArray ? jtoken.SingleOrDefault<JToken>() : jtoken;
    if(jobject != null)
    {
        var jtext = jobject.ToString(Formatting.None);
        var data = JsonConvert.DeserializeAnonymousType(jtext, new {IoTHub = new { ConnectionDeviceId = ""}});        
        var blobName = $"{DateTime.UtcNow.ToString("yyyy/MM/dd/hh")}/{data.IoTHub.ConnectionDeviceId}";  
        var blob = blobContainer.GetAppendBlobReference(blobName);
        if(!await blob.ExistsAsync())
        {
            await blob.CreateOrReplaceAsync();
        }
        await blob.AppendTextAsync(jtext + "\r\n");
    }
return new NoContentResult();

}

函数.json

    {
      "bindings": [
       {
           "authLevel": "function",
           "name": "body",
           "type": "httpTrigger",
           "direction": "in",
           "methods": [
             "get",
             "post"
             ]
      },
      {
          "name": "blobContainer",
          "type": "blob",
          "path": "myContainer",
          "connection": "mySTORAGE",
          "direction": "out"
      },
      {
          "name": "$return",
          "type": "http",
          "direction": "out"
      }
      ]
 }

关于azure - 将数据从 Azure 流分析存储到 Blob 存储时,是否可以在路径中添加 DeviceID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53069402/

相关文章:

azure - 如何添加受 Azure Monitor 工作簿信任的 URL?

azure - 如何使用门户中的 Azure 功能将消息发送到 Azure IOT 中心并将其显示在客户端应用程序上

Azure IOT ExportDevicesAsync 内部服务器错误

azure - 自动过期孤立订阅 (Azure ServiceBus Messaging SubscriptionClient)

azure - 如何克服日志问题?

java - 缺少订阅注册 : The subscription is not registered to use namespace 'Microsoft.Storage'

Azure Blob 存储文件级安全性

c# - Windows 10 Iot/UWP 上的 Azure 存储库客户端?

node.js - 如何检查 Azure IOT 中心发送器是否已停止使用 node.js 和 socket.io

python - Azure-sdk-for-python AKS : how to add a new nodepool using