简体   繁体   中英

Adding multiple event type to same kafka topic in .net

I am trying to add multiple schemas to the same subject in the schema registry, so I have set ValueSubjectNameStrategy to SubjectNameStrategy.TopicRecord , also set the register automatically to AutomaticRegistrationBehavior.Always . But while auto registering the schema it still using the SubjectNameStrategy.Topic strategy.

 var schemaRegistryConfig = new SchemaRegistryConfig { Url = "http://localhost:8081", ValueSubjectNameStrategy = SubjectNameStrategy.TopicRecord };
        var registry = new CachedSchemaRegistryClient(schemaRegistryConfig);
        var builder = new ProducerBuilder<string, SplitLineKGN>(KafkaConfig.Producer.GetConfig(_config.GetSection("KafkaProducer")))
                    .SetAvroValueSerializer(registry, registerAutomatically: AutomaticRegistrationBehavior.Always)
                    .SetErrorHandler((_, error) => Console.Error.WriteLine(error.ToString()));
        _producerMsg = builder.Build();
await _producerMsg.ProduceAsync("MyTopic", new Message<string, SampleMessage> { Key = key, Value = line });

how to auto register multiple schemas to a topic?

  1. SchemaRegistryConfig.ValueSubjectNameStrategy is deprecated, it should now be configured using the serializer's configuration: code

  2. For producing multiple event types with a single producer you have to use AvroSerializer<ISpecificRecord> as described below:

     var schemaRegistryConfig = new SchemaRegistryConfig { Url = "http://localhost:8081" }; using var schemaRegistryClient = new CachedSchemaRegistryClient(schemaRegistryConfig); var avroSerializerConfig = new AvroSerializerConfig { SubjectNameStrategy = SubjectNameStrategy.TopicRecord, AutoRegisterSchemas = true }; var producerConfig = KafkaConfig.Producer.GetConfig(_config.GetSection("KafkaProducer")); using var producer = new ProducerBuilder<string, ISpecificRecord>(producerConfig) .SetValueSerializer(new AvroSerializer<ISpecificRecord>(schemaRegistryClient, avroSerializerConfig)) .SetErrorHandler((_, error) => Console.Error.WriteLine(error)) .Build(); await producer.ProduceAsync("MyTopic", new Message<string, ISpecificRecord> { Key = key, Value = line });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM