将数据从Azure Stream Analytics存储到Blob存储时,我可以在路径中添加DeviceID吗?

时间:2018-10-30 17:01:50

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

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

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

2 个答案:

答案 0 :(得分:1)

  

我知道我们可以根据需要在路径中添加{date} {time}   格式,我们也可以在该路径中添加deviceId。'

正如comment中的@Peter Bons所述,到目前为止,尚不支持输出中的变量名。

作为解决方法,您可以使用Blob Trigger Azure Function。您需要在输出列中传递deviceId,然后在blob触发函数中获取它。然后使用blob sdk创建/deviceId目录,以将blob复制到其中并删除先前的blob。

希望它对您有帮助。

答案 1 :(得分:1)

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

ASA作业查询:

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

Azure函数(HttpTrigger):

run.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();

}

function.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"
      }
      ]
 }