配置信息
配置信息,更多详情配置以及说明查看AdminClientConfig
public static Properties getKafkaConfig(){
Properties properties = new Properties();
//Kafka服务器地址
properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,brokerList);
//当前客户端ID
properties.put(AdminClientConfig.CLIENT_ID_CONFIG,"admin topic");
return properties;
}
创建主题
创建主题示例代码
需要指定主题名称、分区数量、副本数量
//创建主题
public static void createTopic(){
AdminClient client = KafkaAdminClient.create(getKafkaConfig());
//主题名称
String topName = "test-topic";
//分区数量
int numPartitions = 1;
//副本数量
short replicationFactor = 1;
NewTopic topic = new NewTopic(topName,numPartitions,replicationFactor);
//创建主题
CreateTopicsResult clientTopics = client.createTopics(Collections.singleton(topic));
Iterator<Map.Entry<String, KafkaFuture<Void>>> createIterator = clientTopics.values().entrySet().iterator();
while (createIterator.hasNext()){
Map.Entry<String,KafkaFuture<Void>> entry =createIterator.next();
log.info(entry.getKey());
}
}
删除主题
代码如下(示例):
public static void deleteTopic(){
AdminClient client = KafkaAdminClient.create(getKafkaConfig());
//主题名称
String topName = "test-topic";
//删除主题
DeleteTopicsResult deleteTopicsResult = client.deleteTopics(Collections.singleton(topName));
Iterator<Map.Entry<String, KafkaFuture<Void>>> deleteIterator = deleteTopicsResult.values().entrySet().iterator();
while (deleteIterator.hasNext()){
Map.Entry<String,KafkaFuture<Void>> entry =deleteIterator.next();
log.info(entry.getKey());
}
}
查看所有主题名称
查询所有主题名称
public static void listTopics() throws Exception{
AdminClient client = KafkaAdminClient.create(getKafkaConfig());
//查看所有主题
ListTopicsResult listTopicsResult = client.listTopics();
KafkaFuture<Set<String>> listFuture = listTopicsResult.names();
Iterator<String> iterator = listFuture.get().iterator();
while (iterator.hasNext()){
log.info("主题名称:{}",iterator.next());
}
}
查看某个主题详细信息
通过主题名称,去查看某个主题的详细信息,包含分区,副本,存活副本等信息
主题名称可以通过查看所有主题名称
获得,或是自己指定主题名称
public static void describeTopics() throws Exception{
AdminClient client = KafkaAdminClient.create(getKafkaConfig());
//主题名称
String topName = "test-topic";
//查看主题详细信息
DescribeTopicsResult describeTopicsResult = client.describeTopics(Collections.singleton(topName));
Iterator<Map.Entry<String,KafkaFuture<TopicDescription>>> describeIterator = describeTopicsResult.values().entrySet().iterator();
while (describeIterator.hasNext()){
Map.Entry<String,KafkaFuture<TopicDescription>> entry = describeIterator.next();
log.info(entry.getKey());
TopicDescription description = entry.getValue().get();
log.info("主题名称: {}",description.name());
List<TopicPartitionInfo> partitionInfos = description.partitions();
for(TopicPartitionInfo info : partitionInfos){
log.info("分区位置:{}",info.partition());
log.info("分区主节点信息:{}",info.leader());
log.info("分区副本节点信息:{}",info.replicas());
log.info("分区存活节点信息:{}",info.isr());
}
}
}
删除消息记录
删除指定分区,偏移量之前的数据,并返回低水平位值
低水平位:能消费的最大offset值
public static void deleteRecords() throws Exception{
AdminClient client = KafkaAdminClient.create(getKafkaConfig());
Map<TopicPartition, RecordsToDelete> deleteMap = new HashMap<>();
//指定分区
int partition = 2;
//主题
String topName = "test-topic";
TopicPartition topicPartition = new TopicPartition(topName,partition);
//消息偏移量
int offset = 5;
//删除偏移量之前的数据
RecordsToDelete recordsToDelete = RecordsToDelete.beforeOffset(offset);
deleteMap.put(topicPartition,recordsToDelete);
DeleteRecordsResult result = client.deleteRecords(deleteMap);
//低水平位,消息的offset
Iterator<Map.Entry<TopicPartition,KafkaFuture<DeletedRecords>>> iterator = result.lowWatermarks().entrySet().iterator();
while (iterator.hasNext()){
DeletedRecords records = iterator.next().getValue().get();
log.info("低水平位:lowWatermark = {}"+records.lowWatermark());
}
}