Assuming that you are using the confluent-dot-net
client, you can use AvroDeserializer
:
(async) Avro deserializer. Use this deserializer with GenericRecord
,
types generated using the avrogen.exe
tool or one of the following
primitive types: int
, long
, float
, double
, boolean
, string
, byte[]
.
Example:
var consumeTask = Task.Run(() =>{
using(var schemaRegistry = new CachedSchemaRegistryClient(new SchemaRegistryConfig {
SchemaRegistryUrl = schemaRegistryUrl
}))
using(var consumer = new ConsumerBuilder < string, GenericRecord > (new ConsumerConfig {
BootstrapServers = bootstrapServers,
GroupId = groupName
}).SetKeyDeserializer(new AvroDeserializer < string > (schemaRegistry).AsSyncOverAsync()).SetValueDeserializer(new AvroDeserializer < GenericRecord > (schemaRegistry).AsSyncOverAsync()).SetErrorHandler((_, e) =>Console.WriteLine($ "Error: {e.Reason}")).Build()) {
consumer.Subscribe(topicName);
try {
while (true) {
try {
var consumeResult = consumer.Consume(cts.Token);
Console.WriteLine($ "Key: {consumeResult.Message.Key}\nValue: {consumeResult.Value}");
}
catch(ConsumeException e) {
Console.WriteLine($ "Consume error: {e.Error.Reason}");
}
}
}
catch(OperationCanceledException) {
// commit final offsets and leave the group.
consumer.Close();
}
}
});