By default when you use the Confluent Kafka .NET client, a topic is created automatically for you when you publish your first message.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a producer that can be used to send messages to kafka that have no key and a value of type string | |
using var producer = new ProducerBuilder<Null, string>(config) | |
.Build(); | |
//Construct a message to send (generic type must match what was used above when creating the producer) | |
var message = new Message<Null, string> | |
{ | |
Value = value | |
}; | |
await producer.ProduceAsync("kafka-topic-name", message) ; |
However this will create a topic using the default settings. Typically you want to have more control when creating a topic. This is possible through the usage of the AdminClient:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var config = new AdminClientConfig | |
{ | |
BootstrapServers = "localhost:9092" | |
}; | |
using var adminClient = new AdminClientBuilder(config) | |
.Build() | |
await adminClient.CreateTopicsAsync(new TopicSpecification[] { | |
new TopicSpecification { Name = topicName, ReplicationFactor = 1, NumPartitions = 1 } }); | |