With Kafka, we know that each time we create a Kafka theme (Topic), we can specify the number of partitions and the number of copies, and if these properties are configured in the Server.properties file, the themes generated by the subsequent call to the Java API will use the default values. Change first need to use command bin/kafka-topics.sh--zookeeper localhost:2181--alter--topic my-topic--config max.message.bytes= 128000 of the changes shown, we also want this process to be set through the API before the producer call, without having to use a script before or after this article. View source discovery, in fact, all of the implementation is through the Topiccommand Main method, in this record two ways:
1. Create a Theme (Topic)
"Command mode": bin/kafka-topics.sh--zookeeper zk_host:port/chroot--create--topic my_topic_name--partitions 20-- Replication-factor 3--config x=y
"JAVA API Mode":
[Java]View Plaincopy
- string[] options = new string[]{
- "--create",
- "--zookeeper",
- "Zk_host:port/chroot",
- "--partitions",
- "",
- "--topic",
- "My_topic_name",
- "--replication-factor",
- "3",
- "--config",
- "X=y"
- };
- Topiccommand.main (options);
2. View all Topics
"Command mode": bin/kafka-topics.sh--list--zookeeper localhost:2181
"JAVA API Mode":
[Java]View Plaincopy
- string[] options = new string[]{
- "--list",
- "--zookeeper",
- "localhost:2181"
- };
- Topiccommand.main (options);
3. View the specified topic:
"Command mode": bin/kafka-topics.sh--describe--zookeeper localhost:2181--topic my-replicated-topic
"JAVA API Mode":
[Java]View Plaincopy
- string[] options = new string[]{
- "--describe",
- "--zookeeper",
- "localhost:2181",
- "--topic",
- "My-replicated-topic",
- };
- Topiccommand.main (options);
4, modify the theme:
"Command mode": bin/kafka-topics.sh--zookeeper zk_host:port/chroot--alter--topic my_topic_name--deleteConfig x
"JAVA API Mode":
[Java]View Plaincopy
- string[] options = new string[]{
- "--alter",
- "--zookeeper",
- "Zk_host:port/chroot",
- "--topic",
- "My_topic_name",
- "--deleteconfig",
- "x"
- };
- Topiccommand.main (options);
5. Delete the quiz:
"Command mode": None
"JAVA API Mode":
[Java]View Plaincopy
- string[] options = new string[]{
- "--zookeeper",
- "Zk_host:port/chroot",
- "--topic",
- "My_topic_name"
- };
- Deletetopiccommand.main (options);
Create with Java API, view (describe), enumerate (list), delete Kafka theme (Topic)