final Map<String, CreateTopicsRequest.TopicDetails> topicsMap = new HashMap<>(newTopics.size()); for (NewTopic newTopic : newTopics) { if (topicNameIsUnrepresentable(newTopic.name())) { KafkaFutureImpl<Void> future = new KafkaFutureImpl<>(); future.completeExceptionally(new InvalidTopicException("The given topic name '" + newTopic.name() + "' cannot be represented in a request.")); topicFutures.put(newTopic.name(), future); } else if (!topicFutures.containsKey(newTopic.name())) { topicFutures.put(newTopic.name(), new KafkaFutureImpl<>()); topicsMap.put(newTopic.name(), newTopic.convertToTopicDetails());
private void addTopicsIfNeeded(AdminClient adminClient, Collection<NewTopic> topics) { if (topics.size() > 0) { Map<String, NewTopic> topicNameToTopic = new HashMap<>(); topics.forEach(t -> topicNameToTopic.compute(t.name(), (k, v) -> v = t)); DescribeTopicsResult topicInfo = adminClient .describeTopics(topics.stream() .map(NewTopic::name) .collect(Collectors.toList())); List<NewTopic> topicsToAdd = new ArrayList<>(); Map<String, NewPartitions> topicsToModify = checkPartitions(topicNameToTopic, topicInfo, topicsToAdd); if (topicsToAdd.size() > 0) { addTopics(adminClient, topicsToAdd); } if (topicsToModify.size() > 0) { modifyTopics(adminClient, topicsToModify); } } }
/** * Add topics to the existing broker(s). * The broker(s) must be running. * @param topics the topics. * @since 2.2 */ public void addTopics(NewTopic... topics) { Assert.notNull(this.zookeeper, "Broker must be started before this method can be called"); for (NewTopic topic : topics) { Assert.isTrue(this.topics.add(topic.name()), () -> "topic already exists: " + topic); Assert.isTrue(topic.replicationFactor() <= this.count && (topic.replicasAssignments() == null || topic.replicasAssignments().size() <= this.count), () -> "Embedded kafka does not support the requested replication factor: " + topic); } doWithAdmin(admin -> createTopics(admin, Arrays.asList(topics))); }
@Test public void alreadyExists() throws Exception { AtomicReference<Method> addTopics = new AtomicReference<>(); AtomicReference<Method> modifyTopics = new AtomicReference<>(); ReflectionUtils.doWithMethods(KafkaAdmin.class, m -> { m.setAccessible(true); if (m.getName().equals("addTopics")) { addTopics.set(m); } else if (m.getName().equals("modifyTopics")) { modifyTopics.set(m); } }, m -> { return m.getName().endsWith("Topics"); }); try (AdminClient adminClient = AdminClient.create(this.admin.getConfig())) { addTopics.get().invoke(this.admin, adminClient, Collections.singletonList(this.topic1)); modifyTopics.get().invoke(this.admin, adminClient, Collections.singletonMap( this.topic1.name(), NewPartitions.increaseTo(this.topic1.numPartitions()))); } }
/** * Attempt to create the topic described by the given definition, returning true if the topic was created or false * if the topic already existed. * * @param topic the specification of the topic * @return true if the topic was created or false if the topic already existed. * @throws ConnectException if an error occurs, the operation takes too long, or the thread is interrupted while * attempting to perform this operation * @throws UnsupportedVersionException if the broker does not support the necessary APIs to perform this request */ public boolean createTopic(NewTopic topic) { if (topic == null) return false; Set<String> newTopicNames = createTopics(topic); return newTopicNames.contains(topic.name()); }
private void createCruiseControlMetricsTopic() { try { final CreateTopicsResult createTopicsResult = _adminClient.createTopics(Collections.singletonList(_newTopic)); createTopicsResult.values().get(_newTopic.name()).get(); LOG.info("Cruise Control metrics topic created: {}", _cruiseControlMetricsTopic); } catch (InterruptedException | ExecutionException e) { if (!(e.getCause() instanceof TopicExistsException)) { LOG.error("Unable to create Cruise Control topic", e); } } }
/** * Create a new topic via the Kafka AdminClient API, calling the given handler * (in a different thread) with the result. */ @Override public void createTopic(Topic topic, Handler<AsyncResult<Void>> handler) { NewTopic newTopic = TopicSerialization.toNewTopic(topic, null); LOGGER.debug("Creating topic {}", newTopic); KafkaFuture<Void> future = adminClient.createTopics( Collections.singleton(newTopic)).values().get(newTopic.name()); queueWork(new UniWork<>("createTopic", future, handler)); }
/** * Add topics to the existing broker(s). * The broker(s) must be running. * @param topics the topics. * @since 2.2 */ public void addTopics(NewTopic... topics) { Assert.notNull(this.zookeeper, "Broker must be started before this method can be called"); for (NewTopic topic : topics) { Assert.isTrue(this.topics.add(topic.name()), () -> "topic already exists: " + topic); Assert.isTrue(topic.replicationFactor() <= this.count && (topic.replicasAssignments() == null || topic.replicasAssignments().size() <= this.count), () -> "Embedded kafka does not support the requested replication factor: " + topic); } doWithAdmin(admin -> createTopics(admin, Arrays.asList(topics))); }
if (topics != null) { for (NewTopic topic : topics) { if (topic != null) topicsByName.put(topic.name(), topic);
newTopics.removeIf(newTopic -> createTopicNames.contains(newTopic.name()));