I would like to share my recent experience, which I described on my blog, โThe side effect of getting Kafkaโs topic metadata,โ as well as provide answers to some of the questions raised here.
1) What is the best way to create themes in kafka? Do I need to create topics before posting?
I think that if we know that we are going to use a topic with a fixed name Kafka in advance, we would be better off creating a topic before writing or reading messages from it. Usually this can be done in the script after starting with bin / kafka-topics.sh, see, for example, the official documentation . Or we can use KafkaAdminClient, which was introduced in Kafka 0.11.0.0.
On the other hand, I see some cases where we would need to generate a topic name on the fly. In these cases, we cannot find out the fixed topic name, and we can rely on the property "auto.create.topics.enable". When it is turned on, the theme will be created automatically. And that raises the second question:
2) What actions will cause creation when auto.create.topics.enable is true
In fact, as @Lan already pointed out
If this parameter is set to true when applications try to create, use, or retrieve metadata for a topic that does not exist, Kafka will automatically create a theme with a default replication rate and number of partitions.
I would like to put it even simpler:
If automatic theme creation is enabled for Kafka brokers, then whenever a Kafka broker sees a specific topic name, this theme will be created if it does not already exist.
And also the fact that the selection of metadata will automatically create a topic is often ignored by people, including me. A specific example of this is the use of the API customer.partitionFor (topic) API, this method creates the given topic if it does not exist.
For those interested in the more detailed information that I mentioned above, you can take a look at my own blog post on the same topic . A side effect of getting Kafka theme metadata .