Using Java API creation (create), view (describe), list (list), delete Kafka theme (Topic)--Reprint

Source: Internet
Author: User

Original: http://blog.csdn.net/changong28/article/details/39325079

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":

  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":

    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":

    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":

    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":

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

Another: The following Kafka Delete topic method (from the "Light Blog" blog, source http://caiguangguang.blog.51cto.com/1652935/1548069)

0.8 of official documents provide a command to delete topic:

Kafka-topics.sh--delete but will not find this method at run time.

Kafka-topics.sh finally ran the class Kafka.admin.TopicCommand, in 0.8 of the source code in this class did not find the delete topic related codes.

Under the Kafka Admin package, a Deletetopiccommand class is provided to remove the topic functionality.

Kafka.admin.DeleteTopicCommand

The specific implementation code for removing topic is as follows:

1234567891011121314151617181920 importorg.I0Itec.zkclient.ZkClientimportkafka.utils.{Utils, ZKStringSerializer, ZkUtils}.......    val topic = options.valueOf(topicOpt)    val zkConnect = options.valueOf(zkConnectOpt)    var zkClient: ZkClient = null    try{      zkClient = newZkClient(zkConnect, 3000030000, ZKStringSerializer)      zkClient.deleteRecursive(ZkUtils.getTopicPath(topic))  //其实最终还是通过删除zk里面对应的路径来实现删除topic的功能      println("deletion succeeded!")    }    catch{      casee: Throwable =>        println("delection failed because of "+ e.getMessage)        println(Utils.stackTrace(e))    }    finally{      if(zkClient != null)        zkClient.close()    }

Because this command will only delete the information in ZK, the real data is still not deleted, so you need to login to each broker, the corresponding topic partition data directory deleted, it may also be because of this, The delete command is not integrated into the Kafka.admin.TopicCommand class.

Using Java API creation (create), view (describe), list (list), delete Kafka theme (Topic)--Reprint

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.