Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Avro Schema not encoded with data stream

Tags:

c#

avro

I'm using Avro to serialize objects and then add them to Kafka messages that will be consumed and deserialized by clients. I've tried several different approaches for serialization but none of them seem to embed the schema in the data stream. Here is the latest version of my serialization code. You can see the commented out attempts to use the various writers available.

public static byte[] Serialize<T>(T recordObj) where T : ISpecificRecord
    {
        Log.Info("Serializing {0} object to Avro.", typeof(T));
        try
        {
            using (var ms = new MemoryStream())
            {
                var encoder = new BinaryEncoder(ms);
                //var writer = new SpecificDefaultWriter(recordObj.Schema);
                var writer = new SpecificDatumWriter<T>(recordObj.Schema);
                //writer.Write(recordObj.Schema, recordObj, encoder);
                writer.Write(recordObj, encoder);
                return ms.ToArray();
            }
        }
        catch (Exception ex)
        {
            Log.Error("Failed to Avro serialize object. {0}", ex);
            return null;
        }
    }

I'm not really sure what else to try.

like image 460
reets-dev Avatar asked Feb 02 '17 16:02

reets-dev


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Why do we write C?

We write C for Carbon Because in some element the symbol of the element is taken form its first words and Co for Cobalt beacause in some elements the symbol of the element is taken from its first second letters, so that the we don't get confuse.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

After digging around in the actual Avro code, I found out I needed a FileWriter, but could not figure out how to instantiate one as DataFileWriter has no public constructor. Turns out there is a static method on the DataFileWriter class called OpenWriter which takes in a DatumWriter and a Stream and returns a DataFileWriter. The code below now properly includes object metadata in the result data stream.

 public static byte[] Serialize<T>(T recordObj) where T : ISpecificRecord
    {
        Log.Info("Serializing {0} object to Avro.",typeof(T));
        try
        {
            using(var ms = new MemoryStream())
            {
                var specDatumWriter = new SpecificDatumWriter<T>(recordObj.Schema);
                var specDataWriter = Avro.File.DataFileWriter<T>.OpenWriter(specDatumWriter, ms);
                specDataWriter.Append(recordObj);
                specDataWriter.Flush();
                specDataWriter.Close();
                return ms.ToArray();
            }
        } 
        catch(Exception ex)
        {
            Log.Error("Failed to Avro serialize object. {0}",ex);
            return null;
        }
    }
like image 200
reets-dev Avatar answered Oct 01 '22 02:10

reets-dev