增加分区
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package com.xuxd.kafka.console.beans.dto;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* kafka-console-ui.
|
||||
*
|
||||
* @author xuxd
|
||||
* @date 2021-10-18 19:55:40
|
||||
**/
|
||||
@Data
|
||||
public class AddPartitionDTO {
|
||||
|
||||
private String topic;
|
||||
|
||||
private int addNum;
|
||||
|
||||
private List<List<Integer>> assignment = new ArrayList<>();
|
||||
}
|
||||
@@ -21,7 +21,7 @@ public class NewTopicDTO {
|
||||
private Map<String, String> configs = new HashMap<>();
|
||||
|
||||
public NewTopic toNewTopic() {
|
||||
NewTopic topic = new NewTopic(name, numPartitions, replicationFactor);
|
||||
NewTopic topic = new NewTopic(name.trim(), numPartitions, replicationFactor);
|
||||
if (MapUtils.isNotEmpty(configs)) {
|
||||
topic.configs(configs);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.xuxd.kafka.console.controller;
|
||||
|
||||
import com.xuxd.kafka.console.beans.dto.AddPartitionDTO;
|
||||
import com.xuxd.kafka.console.beans.dto.NewTopicDTO;
|
||||
import com.xuxd.kafka.console.beans.enums.TopicType;
|
||||
import com.xuxd.kafka.console.service.TopicService;
|
||||
@@ -37,11 +38,16 @@ public class TopicController {
|
||||
|
||||
@GetMapping("/partition")
|
||||
public Object getTopicPartitionInfo(@RequestParam String topic) {
|
||||
return topicService.getTopicPartitionInfo(topic);
|
||||
return topicService.getTopicPartitionInfo(topic.trim());
|
||||
}
|
||||
|
||||
@PostMapping("/new")
|
||||
public Object createNewTopic(@RequestBody NewTopicDTO topicDTO) {
|
||||
return topicService.createTopic(topicDTO.toNewTopic());
|
||||
}
|
||||
|
||||
@PostMapping("/partition/new")
|
||||
public Object addPartition(@RequestBody AddPartitionDTO partitionDTO) {
|
||||
return topicService.addPartitions(partitionDTO.getTopic().trim(), partitionDTO.getAddNum(), partitionDTO.getAssignment());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@ package com.xuxd.kafka.console.service;
|
||||
|
||||
import com.xuxd.kafka.console.beans.ResponseData;
|
||||
import com.xuxd.kafka.console.beans.enums.TopicType;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import org.apache.kafka.clients.admin.NewTopic;
|
||||
|
||||
/**
|
||||
@@ -21,4 +24,6 @@ public interface TopicService {
|
||||
ResponseData getTopicPartitionInfo(String topic);
|
||||
|
||||
ResponseData createTopic(NewTopic topic);
|
||||
|
||||
ResponseData addPartitions(String topic, int addNum, List<List<Integer>> newAssignmentst);
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import java.util.stream.Collectors;
|
||||
import kafka.console.TopicConsole;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.kafka.clients.admin.NewPartitions;
|
||||
import org.apache.kafka.clients.admin.NewTopic;
|
||||
import org.apache.kafka.clients.admin.TopicDescription;
|
||||
import org.apache.kafka.common.TopicPartition;
|
||||
@@ -112,4 +113,21 @@ public class TopicServiceImpl implements TopicService {
|
||||
Tuple2<Object, String> createResult = topicConsole.createTopic(topic);
|
||||
return (boolean) createResult._1 ? ResponseData.create().success() : ResponseData.create().failed(String.valueOf(createResult._2));
|
||||
}
|
||||
|
||||
@Override public ResponseData addPartitions(String topic, int addNum, List<List<Integer>> newAssignments) {
|
||||
List<TopicDescription> list = topicConsole.getTopicList(Collections.singleton(topic));
|
||||
if (list.isEmpty()) {
|
||||
return ResponseData.create().failed("topic not exist.");
|
||||
}
|
||||
TopicDescription topicDescription = list.get(0);
|
||||
|
||||
Map<String, NewPartitions> param = new HashMap<>();
|
||||
param.put(topic, (newAssignments.size() > 0 ? NewPartitions.increaseTo(topicDescription.partitions().size() + addNum, newAssignments) :
|
||||
NewPartitions.increaseTo(topicDescription.partitions().size() + addNum)));
|
||||
|
||||
Tuple2<Object, String> tuple2 = topicConsole.createPartitions(param);
|
||||
boolean success = (boolean) tuple2._1();
|
||||
|
||||
return success ? ResponseData.create().success() : ResponseData.create().failed(tuple2._2());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user