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:
- Next step is to specify your message schema. Therefore you need to create an .avsc file and add your message specification:
- Now itās time to generate the necessary code:
- This will generate the following:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dotnet tool install -g Confluent.Apache.Avro.AvroGen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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"] | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
avrogen -s PageViewEvent.avsc . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ------------------------------------------------------------------------------ | |
// <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()"); | |
}; | |
} | |
} | |
} |
The generated type can than be used by your Producer and Consumer logic.
More information: https://www.confluent.io/blog/avro-kafka-data/