The problem is the following.
Steps:
- An application converts some custom object to an avro fragment (byte array);
- This avro fragment is sent to an event hub in an EventData object;
- The event hub trigger an azure function that receives an Mcrosoft.ServiceBus.Messaging.EventData from the event hub;
- I can extract the body of the EventData and it contains the avro fragment (byte array) of point 1.
I'm using Microsoft.Hadoop.Avro.
I've the schema of the original custom object (point 1) so I tried to create a generic reader that read from the avro fragment but I receive the following error:
Invalid Avro object container in a stream. The header cannot be recognized.
It seems that Microsoft.Hadoop.Avro is able to manage only complete avro file (header + schema + body) and not avro fragment (body).
With java avro-tool I can add a schema to an avro fragment. Is it possible also in .Net or .Net Core? How can I do?
For simplicity in the following code I replaced the EventData that comes form the event hub with the related avro file.
using (Stream stream = new FileStream(@"...\trip-real-0-2019-03-14-12-14.avro", FileMode.Open, FileAccess.Read, FileShare.Read))
{
// create a generic reader for the event hub avro message
using (var reader = AvroContainer.CreateGenericReader(stream))
{
while (reader.MoveNext())
{
foreach (dynamic record in reader.Current.Objects)
{
//get the body of the event hub message (fragment avro bytes)
var avroFragmentByeArray = (byte[])(record.Body);
// try to create a generic reader with the schema.
// this line throws an exception
using (var r = AvroContainer.CreateGenericReader(schema, new MemoryStream(avroFragmentByeArray), true, new CodecFactory()))
{
}
}
}
}
}