Skip to main content

Kafka- Using Avro as serialization format in C#

To help you with using Avro as the serialization format for your Kafka messages, a .NET core global tool avrogen is available.

  • First install the tool using dotnet tool install:
  • dotnet tool install -g Confluent.Apache.Avro.AvroGen
    view raw avrogeninstall.cmd hosted with ā¤ by GitHub
  • Next step is to specify your message schema. Therefore you need to create an .avsc file and add your message specification:
  • {
    "namespace": "MessageTypes",
    "type": "record",
    "doc": "A simple message type.",
    "name": "PageViewEvent",
    "fields": [
    {
    "name": "timestamp",
    "type": ["null","string"]
    },
    {
    "name": "ipAddress",
    "type": ["null","string"]
    },
    {
    "name": "sessionId",
    "type": ["null","string"]
    },
    {
    "name": "pageUrl",
    "type": "string"
    },
    {
    "name": "referrer",
    "type": ["null","string"]
    },
    {
    "name": "browser",
    "type": ["null","string"]
    }
    ]
    }
    view raw PageViewEvent.avsc hosted with ā¤ by GitHub
  • Now itā€™s time to generate the necessary code:
  • avrogen -s PageViewEvent.avsc .
    view raw avrogen.cmd hosted with ā¤ by GitHub
  • This will generate the following:
  • // ------------------------------------------------------------------------------
    // <auto-generated>
    // Generated by avrogen, version 1.7.7.5
    // Changes to this file may cause incorrect behavior and will be lost if code
    // is regenerated
    // </auto-generated>
    // ------------------------------------------------------------------------------
    namespace MessageTypes
    {
    using System;
    using System.Collections.Generic;
    using System.Text;
    using global::Avro;
    using global::Avro.Specific;
    public partial class PageViewEvent : ISpecificRecord
    {
    public static Schema _SCHEMA = Schema.Parse(@"{""type"":""record"",""name"":""PageViewEvent"",""namespace"":""MessageTypes"",""fields"":[{""name"":""timestamp"",""type"":[""null"",""string""]},{""name"":""ipAddress"",""type"":[""null"",""string""]},{""name"":""sessionId"",""type"":[""null"",""string""]},{""name"":""pageUrl"",""type"":""string""},{""name"":""referrer"",""type"":[""null"",""string""]},{""name"":""browser"",""type"":[""null"",""string""]}]}");
    private string _timestamp;
    private string _ipAddress;
    private string _sessionId;
    private string _pageUrl;
    private string _referrer;
    private string _browser;
    public virtual Schema Schema
    {
    get
    {
    return PageViewEvent._SCHEMA;
    }
    }
    public string timestamp
    {
    get
    {
    return this._timestamp;
    }
    set
    {
    this._timestamp = value;
    }
    }
    public string ipAddress
    {
    get
    {
    return this._ipAddress;
    }
    set
    {
    this._ipAddress = value;
    }
    }
    public string sessionId
    {
    get
    {
    return this._sessionId;
    }
    set
    {
    this._sessionId = value;
    }
    }
    public string pageUrl
    {
    get
    {
    return this._pageUrl;
    }
    set
    {
    this._pageUrl = value;
    }
    }
    public string referrer
    {
    get
    {
    return this._referrer;
    }
    set
    {
    this._referrer = value;
    }
    }
    public string browser
    {
    get
    {
    return this._browser;
    }
    set
    {
    this._browser = value;
    }
    }
    public virtual object Get(int fieldPos)
    {
    switch (fieldPos)
    {
    case 0: return this.timestamp;
    case 1: return this.ipAddress;
    case 2: return this.sessionId;
    case 3: return this.pageUrl;
    case 4: return this.referrer;
    case 5: return this.browser;
    default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Get()");
    };
    }
    public virtual void Put(int fieldPos, object fieldValue)
    {
    switch (fieldPos)
    {
    case 0: this.timestamp = (System.String)fieldValue; break;
    case 1: this.ipAddress = (System.String)fieldValue; break;
    case 2: this.sessionId = (System.String)fieldValue; break;
    case 3: this.pageUrl = (System.String)fieldValue; break;
    case 4: this.referrer = (System.String)fieldValue; break;
    case 5: this.browser = (System.String)fieldValue; break;
    default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Put()");
    };
    }
    }
    }
    view raw PageViewEvent.cs hosted with ā¤ by GitHub

The generated type can than be used by your Producer and Consumer logic.

More information: https://www.confluent.io/blog/avro-kafka-data/

    Popular posts from this blog

    Kubernetesā€“Limit your environmental impact

    Reducing the carbon footprint and CO2 emission of our (cloud) workloads, is a responsibility of all of us. If you are running a Kubernetes cluster, have a look at Kube-Green . kube-green is a simple Kubernetes operator that automatically shuts down (some of) your pods when you don't need them. A single pod produces about 11 Kg CO2eq per year( here the calculation). Reason enough to give it a try! Installing kube-green in your cluster The easiest way to install the operator in your cluster is through kubectl. We first need to install a cert-manager: kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.14.5/cert-manager.yaml Remark: Wait a minute before you continue as it can take some time before the cert-manager is up & running inside your cluster. Now we can install the kube-green operator: kubectl apply -f https://github.com/kube-green/kube-green/releases/latest/download/kube-green.yaml Now in the namespace where we want t...

    Azure DevOps/ GitHub emoji

    Iā€™m really bad at remembering emojiā€™s. So here is cheat sheet with all emojiā€™s that can be used in tools that support the github emoji markdown markup: All credits go to rcaviers who created this list.

    DevToysā€“A swiss army knife for developers

    As a developer there are a lot of small tasks you need to do as part of your coding, debugging and testing activities.  DevToys is an offline windows app that tries to help you with these tasks. Instead of using different websites you get a fully offline experience offering help for a large list of tasks. Many tools are available. Here is the current list: Converters JSON <> YAML Timestamp Number Base Cron Parser Encoders / Decoders HTML URL Base64 Text & Image GZip JWT Decoder Formatters JSON SQL XML Generators Hash (MD5, SHA1, SHA256, SHA512) UUID 1 and 4 Lorem Ipsum Checksum Text Escape / Unescape Inspector & Case Converter Regex Tester Text Comparer XML Validator Markdown Preview Graphic Col...