1

Hi I am working Confluent kafka. I have consumer which returns generic record. I want to de-serialize it. I dint find any way. I can do manually each field like

 object options = ((GenericRecord)response.Message.Value["Product"])["Options"];

I found one here

Deserialize an Avro file with C# But how can I convert my schema into stream? I want to know If we can de-serialize into our c# model using any solution? Any help would be greatly appreciated. Thanks.

Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
Niranjan
  • 1,881
  • 6
  • 44
  • 71
  • Thanks. But my schema contains data. Consumer is returning. How can I convert my schema into stream? – Niranjan Jul 16 '19 at 14:39

1 Answers1

4

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();
    }
  }
});
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156