How to produce Kafka Messages with a Key/Value

The code Asynchronously sends a single message to a Kafka topic.

var producerConfig = new ProducerConfig { Acks = Acks.All, BootstrapServers = "localhost:9092" }; var topic = args.Length > 0 ? args[0] : "test"; var builder = new ProducerBuilder<String, String>(producerConfig); using (var producer = builder.Build()) //Build a new IProducer implementation Instance { string s = Console.ReadLine(); while (!s.Contains("exit", StringComparison.InvariantCultureIgnoreCase)) { string[] r = s.Split(":"); producer.Produce(topic, new Message<string, string> { Key = r[0], Value = r[1] }, (d) => { if (d.Status == PersistenceStatus.Persisted) {Console.WriteLine("Message sent !");} }); s = Console.ReadLine(); } }