You could use Terraform or Kubernetes Operators which will not only help you create topics (with one command), but also manage them later if you did need to delete or modify their configs.
But without custom solutions, and only for purpose of batch-creation, you can use awk
for this
Create a file
$ cat /tmp/topics.txt
test1:1:1
test2:1:2
Then use AWK system
function to execute the kafka-topics script, and parse the file
awk -F':' '{ system("./bin/kafka-topics.sh --create --zookeeper localhost:2181 --topic=" $1 " --replication-factor=" $2 --partitions=" $3 " ) }' /tmp/topics.txt
Created topic "test1".
Created topic "test2".
And we can see the topics are created
$ ./bin/kafka-topics.sh --zookeeper localhost:2181 --list
test1
test2
Note: Hundreds of topics this quickly might overload Zookeeper, so might help to add a call to "; sleep 10"
at the end.
As of Kafka 3.0, the Zookeeper flag is replaced by bootstrap-servers.
-I % -L1
, but manual says it's not correct combinationThe options --max-lines (-L, -l), --replace (-I, -i) and --max-args (-n) are mutually exclusive.
. – Lunette