Provide example of creating Kafka topics
See original GitHub issueI have just started investigating Silverback to use with Kafka. Apologies for the simple question:
What is the recommended pattern for creating and configuring topics in Kafka when using Silverback?
I have a basic .NetCore project using a simple Controller. The Controller takes an IEventPublisher
in the constructor and uses this to publish messages when an HTTP end point is called.
The topic is created automatically, but I would like control over the creation options.
I have been able to take an IConfluentAdminClientBuilder
in the constructor of my controller, and use this to build an IAdminClient
which can then call CreateTopicsAsync()
. This doesn’t feel right!
Is there a better recommended way?
Thanks 😃
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
What is a Kafka Topic and How to Create it?
In Apache Kafka, you can create any number of topics based on your use cases. However, each topic should have a unique and...
Read more >Apache Kafka - Create Topic - Syntax and Examples
Kafka provides a script, kafka-topics.sh, in the <KAFKA_HOME>/bin/ directory, to create a topic in the Kafka cluster. An example is given below :...
Read more >Creating a Kafka Topic
Kafka maintains feeds of messages in categories called topics. Producers write data to topics and consumers read from topics. Since Kafka is a...
Read more >Creating Kafka Topics
Creating Kafka Topics · Step1: Initially, make sure that both zookeeper, as well as the Kafka server, should be started. · Step2: Type...
Read more >Kafka Topics CLI Tutorial
Provide the mandatory parameters: topic name, number of partitions and replication factor. Use the kafka-topics.sh CLI with the --create option.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
We use a
BrokerCallbackHandler
to do this.The following two classes show how to get the configured topics from Silverback.
The class
KafkaTopicsCreator
gets the topics from all registeredIKafkaTopicsProvider
and uses theIAdminClient
to create them.We provide this functionality as a library and hence defined the necessary registrations in an extension method.
You can use it like this
@BEagle1984 @meggima, I see no reason to keep this into our own library. Move it to Silverback?
Disclaimer: This code is sponsored by Swiss Post 😃
The admin API is not covered (not abstracted, not proxied) by Silverback. Internally I use it to get the topic metadata and the
IConfluentAdminClientBuilder
was added explicitly for that and originally intended for internal use only. It’s main purpose is allowing the tests to replace the actualAdminClient
with a mock. I currently only mock theGetMetaData
though. All other methods will thrown aNotSupportedException
, which isn’t probably ideal in your case.That being said I see nothing wrong in using the
AdminClient
to create the topics and I honestly don’t see the point in abstracting/wrapping this in Silverback, being it a just very Kafka specific. Plus I wouldn’t add any value beside proxying the calls to the underlyingAdminClient
, just adding overhead to keep up with the changes that may occur in the Confluent library.This kind of initialization should be done at startup though, not in the controller’s constructor. You could either trigger it from the
Startup.Configure
method, from within anIHostedService
or binding to a Silverback’s callback as suggested by @msallin (even though it might not be relevant and not necessary in your case).If I understood correctly, you would like to customize the topic parameters, so the approach proposed by @msallin, automatically looping into the configured topics and creating them all with the same/default settings wouldn’t suit you well. Correct?
@msallin, @meggima: we surely could move that helper into Silverback (it should be slightly modified since it relies on the
KafkaConnectionSettings
, which are SwissPost specific…but as said I already deal with the creation of theAdminClient
in Silverback, so it wouldn’t be an issue at all). I wonder if we should offer some hooks to configure the topics (and how). Note that this approach implies some assumptions, like that the user you use to authenticate at runtime actually has the necessary permissions to create topics (which probably shouldn’t and some poople would prefer to use a different user, much like many would do to run the database migrations).