Search code examples
c#.netavroconfluent-platformavro-tools

Is it possible to create Avro schema for Dictionary object


Trying to implement avro serialization and deserialization in .net for kafka messages. Message model as follows.

public class SampleMessage
{
    public string Hash{ get; set; }
    public string FileName { get; set; }
    public string Data { get; set; }
    public Dictionary<string, string> Content { get; set; }
    public string LineDetails { get; set; }
}

So, Is it possible to create Avro schema for Dictionary in c#?


Solution

  • Yes. For that, you should use the complex type "map" (https://avro.apache.org/docs/current/spec.html#Maps

    Maps use the type name "map" and support one attribute:

    values: the schema of the map's values.

    Map keys are assumed to be strings.

    Given the example, your .avsc would look something like this:

    {
        "type": "record",
        "name": "SampleMessage",
        "namespace": "samplemessage",
        "fields": [
            {
                "name": "hash",
                "type": "string"
            },
            {
                "name": "fileName",
                "type": "string"
            },
            {
                "name": "data",
                "type": "string"
            },
            {
                "name": "content",
                "type": {
                    "type": "map",
                    "values": "string"
                }
            },
            {
                "name": "lineDetails",
                "type": "string"
            }
        ]
    }