Create with Java API, view (describe), enumerate (list), delete Kafka theme (Topic)

Source: Internet
Author: User

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
  1. string[] options = new string[]{
  2. "--create",
  3. "--zookeeper",
  4. "Zk_host:port/chroot",
  5. "--partitions",
  6. "",
  7. "--topic",
  8. "My_topic_name",
  9. "--replication-factor",
  10. "3",
  11. "--config",
  12. "X=y"
  13. };
  14. Topiccommand.main (options);

2. View all Topics

"Command mode": bin/kafka-topics.sh--list--zookeeper localhost:2181

"JAVA API Mode":

[Java]View Plaincopy
    1. string[] options = new string[]{
    2. "--list",
    3. "--zookeeper",
    4. "localhost:2181"
    5. };
    6. 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
    1. string[] options = new string[]{
    2. "--describe",
    3. "--zookeeper",
    4. "localhost:2181",
    5. "--topic",
    6. "My-replicated-topic",
    7. };
    8. 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
    1. string[] options = new string[]{
    2. "--alter",
    3. "--zookeeper",
    4. "Zk_host:port/chroot",
    5. "--topic",
    6. "My_topic_name",
    7. "--deleteconfig",
    8. "x"
    9. };
    10. Topiccommand.main (options);



5. Delete the quiz:

"Command mode": None

"JAVA API Mode":

[Java]View Plaincopy
      1. string[] options = new string[]{
      2. "--zookeeper",
      3. "Zk_host:port/chroot",
      4. "--topic",
      5. "My_topic_name"
      6. };
      7. Deletetopiccommand.main (options);

Create with Java API, view (describe), enumerate (list), delete Kafka theme (Topic)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.