Confluent yet not provide any APIs to create topic from dot net client, however there is a workaround for the same.
Set auto.create.topics.enable = true
in kafka configuration
use var brokerMetadata = producer.GetMetadata(false, topicName);
to query available topics in existing brokers, if specified topic is
not available then kafka will create a topic with specified name.
private static bool CreateTopicIfNotExist(Producer producer, string topicName)
{
bool isTopicExist = producer.GetMetadata().Topics.Any(t => t.Topic == topicName);
if (!isTopicExist)
{
//Creates topic if it is not exist; Only in case of auto.create.topics.enable = true is set into kafka configuration
var topicMetadata = producer.GetMetadata(false, topicName).Topics.FirstOrDefault();
if (topicMetadata != null && (topicMetadata.Error.Code != ErrorCode.UnknownTopicOrPart || topicMetadata.Error.Code == ErrorCode.Local_UnknownTopic))
isTopicExist = true;
}
return isTopicExist;
}
Thus you can use this work around, I know this is dirty solution but it seems there is no other way as of now.