Azure函数将编码的json物联网中心数据转换为azure数据湖存储上的csv

时间:2019-08-13 12:01:56

标签: azure azure-functions azure-data-lake

我已经模拟了将消息发送到IoT中心Blob存储的设备,并通过使用Azure Data Factory创建管道从那里将数据(以JSON格式编码)复制到Azure Data Lake Gen2。

如何使用Azure函数将这些json输出文件转换为CSV文件并将其再次存储在Azure Data Lake Store中。


@Adam

非常感谢您提供的所有答案,我已经在我的azure帐户中成功实现了这些答案。但是,这实际上并没有给我想要的输出。

希望这可以使事情和我的要求变得清楚。

发送到IOT集线器的我的输入文件是:-

enter image description here

以下是存储在IOT集线器端点(Blob存储)中的数据的示例记录:-( Json-对象集):-

  

{“ EnqueuedTimeUtc”:“ 2019-08-06T10:46:39.4390000Z”,“属性”:{“ $。cdid”:“模拟文件”},“系统属性”:{“ messageId”:“ d48413d2 -d4d7-41bb-9470-dc0483466253“,” correlationId“:” a3062fcb-5513-4c09-882e-8e642f8fe38e“,” connectionDeviceId“:”模拟文件“,” connectionAuthMethod“:” {\“ scope \”:\“ device \“,\” type \“:\” sas \“,\” issuer \“:\” iothub \“,\” acceptingIpFilterRule \“:null}”,“ connectionDeviceGenerationId”:“ 637001643970703748”,“ contentType”: “UTF-8”, “enqueuedTime”: “2019-08-06T10:46:39.4390000Z”}, “身体”: “eyIiOiI1OCIsInJvdGF0ZSI6IjQ2Mi4wMjQxODE3IiwiZGF0ZXRpbWUiOiIxLzMvMjAxNSAxNjowMCIsIm1hY2hpbmVJRCI6IjEiLCJ2b2x0IjoiMTU2Ljk1MzI0NTkiLCJwcmVzc3VyZSI6IjEwNi4zNDY3MTc5IiwidmlicmF0aW9uIjoiNDguODIwMzAwODYifQ ==”}

     

{“ EnqueuedTimeUtc”:“ 2019-08-06T10:46:40.5040000Z”,“属性”:{“ $。cdid”:“模拟文件”},“系统属性”:{“ messageId”:“ 9da638d9 -fdba-41d3-86df-3ea6cedc44e7“,” correlationId“:” aeb20305-6fee-4a59-9053-5fa1d0c780a9“,” connectionDeviceId“:”模拟文件“,” connectionAuthMethod“:” {\“ scope \”:\“ device \“,\” type \“:\” sas \“,\” issuer \“:\” iothub \“,\” acceptingIpFilterRule \“:null}”,“ connectionDeviceGenerationId”:“ 637001643970703748”,“ contentType”: “UTF-8”, “enqueuedTime”: “2019-08-06T10:46:40.5040000Z”}, “身体”: “eyIiOiI1OSIsInJvdGF0ZSI6IjQyOS44MjIxNDM1IiwiZGF0ZXRpbWUiOiIxLzMvMjAxNSAxNzowMCIsIm1hY2hpbmVJRCI6IjEiLCJ2b2x0IjoiMTY0LjE0ODA5NDYiLCJwcmVzc3VyZSI6IjEwNC41MzIxMjM2IiwidmlicmF0aW9uIjoiNDMuNzg4NjgxNTUifQ ==”}

** Json的“正文”字段包含实际的IOT设备数据,该数据以JSON格式编码并具有一些系统和消息属性。

**通过创建从JSON到CSV的管道,不会将实际数据提取到Data Lake Store。

输出CSV是运行ADF管道后JSON文件的精确副本(不提取实际数据)。

[![在此处输入图片描述] [2]] [2]

  

1https://i.stack.imgur.com/iCBvi.png

     

[2]:https://i.stack.imgur.com/HaBMn.png

我尝试使用下面的C#代码并成功解析JSON,但无法从Json“ body”字段读取数据。协助我。

#r "Microsoft.WindowsAzure.Storage"
#r "System.Linq"
#r "Newtonsoft.Json"
#r "Microsoft.Azure.WebJobs.Extensions.Storage"
#r "Microsoft.Azure.WebJobs"
#r "Microsoft.Azure.WebJobs.Extensions"
using Newtonsoft.Json;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.WebJobs.Extensions;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Security.Authentication;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class Telemetry
{
    public string num { get; set; }
    public string datetime { get; set; }
    public string machineID { get; set; }
    public string volt { get; set; }
    public string rotate { get; set; }
    public string pressure { get; set; }
    public string vibration { get; set; }
}
public static void Run(
    [BlobTrigger("blobcontainer/{name}")] String myBlob, 
    //[Blob("bloboutput/{name}"), FileAccess.Write] Stream outputBlob,
    string name, ILogger log)
{
    log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
var serializer = new JsonSerializer();
using (var sr = new StreamReader(myBlob))
    using (var jsonTextReader = new JsonTextReader(sr))
    {
        var Results = (Telemetry)serializer.Deserialize(jsonTextReader, typeof(Telemetry));
        // Do something with Results.
    }
}

1 个答案:

答案 0 :(得分:1)

我真的不会将它们保存到Blob中,而只是将它们留在IoT中心中并使功能触发。但是,如果确实需要,那么从blob开始。

使功能触发器如此

[FunctionName("BlobTriggerCSharp")]        
public static void Run(
    [BlobTrigger("input/{name}")] Stream myBlob, 
    [Blob("output/{name}"), FileAccess.Write] Stream outputBlob
    string name, ILogger log)
{
    // parse JSON with JsonConvert 
    // parse using base64 decode from SystemProperties.contentType field
    // write parsed output to outputBlob stream
}

然后使用ADF

将其作为blob存储上名为 demo.json 的文件放置在 demo 容器中。

我在ADF中创建了类型为 JSON 源数据集

enter image description here

看起来像这样

enter image description here

您可以通过按预览数据来测试它是否有效。

enter image description here

以及如下的接收器数据集

enter image description here

然后我只是进行复制活动。

enter image description here

enter image description here

enter image description here

运行后,我将 JSON转换为CSV

您可以在blob中看到它

enter image description here

enter image description here

如果您想了解更多信息,请查看我的视频

  1. 数据工厂介绍https://youtu.be/EpDkxTHAhOs
  2. 功能应用介绍https://youtu.be/Vxf-rOEO1q4
相关问题