This is my simple code snippet which try to to read the Avro generic record form consumer:
using (var schemaRegistry = new CachedSchemaRegistryClient(new SchemaRegistryConfig { SchemaRegistryUrl = schemaRegistryUrl }))
using (var consumer = new
ConsumerBuilder<string, GenericRecord>(new ConsumerConfig { BootstrapServers = bootstrapServers, GroupId = groupName })
.SetKeyDeserializer(new AsyncAvroDeserializer<string>(schemaRegistry).AsSyncOverAsync())
.SetValueDeserializer(new AsyncAvroDeserializer<GenericRecord>(schemaRegistry).AsSyncOverAsync())
.SetErrorHandler((_, e) => Console.WriteLine($"Error: {e.Reason}"))
.Build())
{
consumer.Subscribe(topicName);
try
{
while (true)
{
try
{
var consumeResult = consumer.Consume();
Console.WriteLine($"Key: {consumeResult.Message.Key}\nValue: {consumeResult.Value}");
Console.WriteLine(consumeResult.Value.Schema);
Console.WriteLine(consumeResult.Value.Schema["favorite_number"]);
}
catch (ConsumeException e)
{
Console.WriteLine($"Consume error: {e.Error.Reason}");
}
}
}
catch (OperationCanceledException)
{
// commit final offsets and leave the group.
consumer.Close();
}
}
As you can see i have can log the schema but dont know how to get its data value.
Console.WriteLine(consumeResult.Value.Schema);
Console.WriteLine(consumeResult.Value.Schema["favorite_number"]);
While the schema and data is like this in this object consumeResult.Value
:
{Schema: {"type":"record","name":"User","namespace":"Confluent.Kafka.Examples.AvroSpecific","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}, contents: { name: sfs, favorite_number: 41, favorite_color: blue, }}
I want to read content data.
consumeResult.Value
a string? Or is that a dynamicobject?consumeResult.Value.contents["favorite_number"]
doesn't work? Otherwise it looks like it needs a JSON deserialization first. – rene