Developer Advocate (Presenter)
Note: This exercise is part of a larger course. You are expected to have completed the previous exercises.
This exercise shows you how to programmatically create topics on Confluent Cloud. You can see the code for modules 1–10 in a combined GitHub repo and you can also refer there for a list of imports as well as a sample build.gradle
file.
To manually create a topic on Confluent Cloud, go to Topics on the left-hand menu, then Add Topic. You can use the default settings, or you can customize your settings, specifying the number of partitions, the cleanup policy, the retention time, the maximum message size, etc.
In order to tell Spring that this particular topic should be created, you need to create a bean with the type of NewTopic
and with the number of partitions and replicas specified in your application class; then use TopicBuilder
to establish the topic:
@Bean
NewTopic hobbit2() {
return TopicBuilder.name("hobbit2").partitions(12).replicas(3).build();
}
Spring uses the topic configuration and automatically instantiates an AdminClient
to execute the required operations.
On Confluent Cloud, you should see your new hobbit2
topic.
Now change the number of partitions in the topic from 12 to 15:
@Bean
NewTopic hobbit2() {
return TopicBuilder.name("hobbit2").partitions(15).replicas(3).build();
}
You should see the change in Confluent Cloud. Note that because the topic already existed, the partitions were simply updated (had it not existed, a new topic would have been created).
We will only share developer content and updates, including notifications when new content is added. We will never send you sales emails. 🙂 By subscribing, you understand we will process your personal information in accordance with our Privacy Statement.