Kafka Getting Started 2:java creating and deleting topic

Source: Internet
Author: User

1.pom

<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.10</artifactId>
<version>0.10.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.10.2.1</version>
</dependency>

2.KafkaTopicBean
 Public classKafkatopicbean {PrivateString topicname;//Topic name    PrivateInteger partition;//partition number of partitions    PrivateInteger replication;//number of replication copies    PrivateString Descrbe;  PublicString Gettopicname () {returntopicname; }         Public voidsettopicname (String topicname) { This. Topicname =topicname; }         PublicInteger getpartition () {returnpartition; }         Public voidsetpartition (Integer partition) { This. partition =partition; }         PublicInteger getreplication () {returnreplication; }         Public voidsetreplication (Integer replication) { This. Replication =replication; }         PublicString Getdescrbe () {returnDescrbe; }         Public voidSetdescrbe (String descrbe) { This. Descrbe =Descrbe; } @Override PublicString toString () {return"Kafkatopicbean [topicname=" + Topicname + ", partition=" +Partition+ ", replication=" + Replication + ", descrbe=" + Descrbe + "]"; }  }

3.KafkaUtil
Importjava.util.Properties;Importorg.apache.kafka.common.security.JaasUtils;Importkafka.admin.AdminUtils;ImportKafka.admin.RackAwareMode;Importkafka.utils.ZkUtils; Public classKafkautil { Public Static voidcreatekafatopic (String zkstr,kafkatopicbean topic) {zkutils zkutils=zkutils. Apply (ZKSTR,30000, 30000, jaasutils.iszksecurityenabled ()); Adminutils.createtopic (Zkutils, Topic.gettopicname (), Topic.getpartition (), Topic.getreplication (), NewProperties (),Newrackawaremode.enforced$ ());    Zkutils.close (); }           Public Static voiddeletekafatopic (String zkstr,kafkatopicbean topic) {zkutils zkutils=zkutils. Apply (ZKSTR,30000, 30000, jaasutils.iszksecurityenabled ());          Adminutils.deletetopic (Zkutils, Topic.gettopicname ());    Zkutils.close (); }}

4. Invocation mode
     Public Static voidMain (string[] args) {//Zookeeper Address: PortString zkstr = "192.168.1.101:2181"; //Topic ObjectKafkatopicbean topic =NewKafkatopicbean (); Topic.settopicname ("Testtopic"); Topic.setpartition (1); Topic.setreplication (1); //Create topickafkautil.createkafatopic (zkstr,topic); //Delete TopicKafkautil.deletekafatopic (Zkstr,topic);
}

Kafka Getting Started 2:java creating and deleting topic

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.