(1)进入在线构建网址
https://start.spring.io/
(2)填写maven相关配置
(3)添加相关依赖
(5)解压后导入IDEA
(6)pom文件添加kafka-clients依赖
<dependency>
<groupId>org.apache.kafkagroupId>
<artifactId>kafka-clientsartifactId>
<version>2.4.0version>
dependency>
(7)检查相关配置
(1)编写相关代码
package net.testclass.testclasskafka;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.CreateTopicsResult;
import org.apache.kafka.clients.admin.NewTopic;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
public class KafkaAdminTest {
// 1、创建topic
private static final String TOPIC_NAME = "test-sptopic";
// 2、设置admin客户端
public static AdminClient initAdiminClient(){
Properties properties = new Properties();
properties.setProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.6.102:9092");
//创建客户端
AdminClient adminClient = AdminClient.create(properties);
return adminClient;
}
// 3、创建topic
@Test
public void createTopicTest(){
AdminClient adminClient = initAdiminClient();
// 指定分区数量为2,副本数量为3
NewTopic newTopic = new NewTopic(TOPIC_NAME,2,(short)3);
// 创建topic
CreateTopicsResult createTopicsResult = adminClient.createTopics(Arrays.asList(newTopic));
//
try{
createTopicsResult.all().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("创建新的topic:"+TOPIC_NAME);
}
}
(2)查看运行结果
(1)编写脚本
package net.testclass.testclasskafka;
import org.apache.kafka.clients.admin.*;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
public class KafkaAdminTest {
// 1、创建topic
private static final String TOPIC_NAME = "test-sptopic";
// 2、设置admin客户端
public static AdminClient initAdiminClient(){
Properties properties = new Properties();
//指定服务ip
properties.setProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.6.102:9092,192.168.6.103:9092,192.168.6.104:9092");
//创建客户端
AdminClient adminClient = AdminClient.create(properties);
return adminClient;
}
// 3、删除topic
@Test
public void deleteTopicTest(){
AdminClient adminClient = initAdiminClient();
//删除topic
DeleteTopicsResult deleteTopicsResult = adminClient.deleteTopics(Arrays.asList(TOPIC_NAME));
try{
deleteTopicsResult.all().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("删除的topic:"+TOPIC_NAME);
}
}
(2)查看运行结果
(1)编写代码
package net.testclass.testclasskafka;
import org.apache.kafka.clients.admin.*;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutionException;
public class KafkaAdminTest {
// 1、设置admin客户端
public static AdminClient initAdiminClient(){
Properties properties = new Properties();
//指定服务ip
properties.setProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.6.102:9092,192.168.6.103:9092,192.168.6.104:9092");
//创建客户端
AdminClient adminClient = AdminClient.create(properties);
return adminClient;
}
// 2、获取topic
@Test
public void listTopicTest() throws ExecutionException, InterruptedException {
AdminClient adminClient = initAdiminClient();
// 2.1拿到topic
ListTopicsResult listTopicsResult = adminClient.listTopics();
// 2.2遍历放入集合
Set<String> topic = listTopicsResult.names().get();
// 2.3循环打印输出
for(String name: topic){
System.out.println(name);
}
}
}
(1)编写代码
package net.testclass.testclasskafka;
import org.apache.kafka.clients.admin.*;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutionException;
public class KafkaAdminTest {
// 1、定义topic名称
private static final String TOPIC_NAME = "sptest-topic";
// 1、设置admin客户端
public static AdminClient initAdiminClient(){
Properties properties = new Properties();
//指定服务ip
properties.setProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.6.102:9092,192.168.6.103:9092,192.168.6.104:9092");
//创建客户端
AdminClient adminClient = AdminClient.create(properties);
return adminClient;
}
// 2、获取topic
@Test
public void descTopic() throws ExecutionException, InterruptedException {
AdminClient adminClient = initAdiminClient();
// 2.1 拿到topic的描述信息
DescribeTopicsResult describeTopicsResult = adminClient.describeTopics(Arrays.asList(TOPIC_NAME));
// 2.2 放入MAP中
Map<String,TopicDescription> stringTopicDescriptionMap = describeTopicsResult.all().get();
// 2.3 遍历放入set中
Set<Map.Entry<String,TopicDescription>> entries = stringTopicDescriptionMap.entrySet();
// 2.4循环打印输出
entries.stream().forEach(entry -> System.out.println("name:"+entry.getKey()+",value"+entry.getValue()));
}
}
(2)运行结果
(1)编写代码
package net.testclass.testclasskafka;
import org.apache.kafka.clients.admin.*;
import org.junit.jupiter.api.Test;
import java.util.*;
import java.util.concurrent.ExecutionException;
public class KafkaAdminTest {
// 1、定义topic名称
private static final String TOPIC_NAME = "sptest-topic";
// 2、设置admin客户端
public static AdminClient initAdiminClient(){
Properties properties = new Properties();
//指定服务ip
properties.setProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.6.102:9092,192.168.6.103:9092,192.168.6.104:9092");
//创建客户端
AdminClient adminClient = AdminClient.create(properties);
return adminClient;
}
// 3、获取topic
@Test
public void descTopic() throws ExecutionException, InterruptedException {
AdminClient adminClient = initAdiminClient();
// 3.1 增加分区到5个
NewPartitions newPartitions = NewPartitions.increaseTo(6);
// 3.2、创建Map并将topic名称、增加分区数传入其中
Map<String,NewPartitions> infoMap = new HashMap<>();
infoMap.put(TOPIC_NAME,newPartitions);
// 3.3、将Map传入接口
CreatePartitionsResult createPartitionsResult= adminClient.createPartitions(infoMap);
// 3.4、抛异常
createPartitionsResult.all().get();
}
}
(2)运行结果