Azure IOT Apache Avro format

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 10:09:22

问题


Using Azure IOT, I have my device endpoint pointing to Azure Blob storage. Data is sent to the Blob, but i get non-ascii characters and am told that the file extension is incorrect. Sample Blob data is

Objavro.codecnullavro.schemaÐ{"type":"record","name":"Message","namespace":"Microsoft.Azure.Devices","fields":[{"name":"EnqueuedTimeUtc","type":"string"},{"name":"Properties","type":{"type":"map","values":"string"}},{"name":"SystemProperties","type":{"type":"map","values":"string"}},{"name":"Body","type":["null","bytes"]}]} ±¡RëZË8耑,¡…$• à82018-02-20T15:12:16.1060000Z $connectionDeviceIdLane4Free(connectionAuthMethodœ{"scope":"device","type":"sas","issuer":"iothub","acceptingIpFilterRule":null}8connectionDeviceGenerationId$636543931333384343enqueuedTime82018-02-20T15:12:16.1060000Z d{"deviceId": "myPythonDevice","PinsCCC": 302.00,\}82018-02-20T15:12:16.2000000Z

Azure IOT docs says that the format is Apache Avro, but I can't find anything on how to decode the non-ascii characters. I don't see where to specify a json extension.


回答1:


It seems by designed.

You can utilize Avro tool to read or convert to JSON format.

Here is a Python sample to read Avro file.




回答2:


From you original question, seems you need to read the content from ARVO file. Following are the steps to use C# to do that from Azure storage:

  1. Install Microsoft.Avro.Core nugget packege

  1. Add following namespaces:

    using Microsoft.Hadoop.Avro;
    using Microsoft.Hadoop.Avro.Container;
    
  2. Get AVRO records

    private async Task<List<AvroRecord>> GetAvroRecordsAsync(CloudBlockBlob cloudBlockBlob)
    {
        var memoryStream = new MemoryStream();
        await cloudBlockBlob.DownloadToStreamAsync(memoryStream);
        memoryStream.Seek(0, SeekOrigin.Begin);
        List<AvroRecord> avroRecords;
        using (var reader = AvroContainer.CreateGenericReader(memoryStream))
        {
            using (var sequentialReader = new SequentialReader<object>(reader))
            {
                  avroRecords = sequentialReader.Objects.OfType<AvroRecord>().ToList();
            }
        }
    
        return avroRecords;
    }
    
  3. Deserialize the object from avro record:

    private MyObject GetMyObject(AvroRecord avroRecord)
    {
        var body = avroRecord.GetField<byte[]>("Body");
        var dataBody= Encoding.UTF8.GetString(body);
        var myObj= JsonConvert.DeserializeObject<MyObject>(dataBody);
        return myObj;
    }
    

Hope this will be helpful to you.



来源:https://stackoverflow.com/questions/49013821/azure-iot-apache-avro-format

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!