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