@Test public void testInvalidTopicNames() throws Exception { try (AdminClientUnitTestEnv env = mockClientEnv()) { env.kafkaClient().setNodeApiVersions(NodeApiVersions.create()); List<String> sillyTopicNames = asList("", null); Map<String, KafkaFuture<Void>> deleteFutures = env.adminClient().deleteTopics(sillyTopicNames).values(); for (String sillyTopicName : sillyTopicNames) { TestUtils.assertFutureError(deleteFutures.get(sillyTopicName), InvalidTopicException.class); } assertEquals(0, env.kafkaClient().inFlightRequestCount()); Map<String, KafkaFuture<TopicDescription>> describeFutures = env.adminClient().describeTopics(sillyTopicNames).values(); for (String sillyTopicName : sillyTopicNames) { TestUtils.assertFutureError(describeFutures.get(sillyTopicName), InvalidTopicException.class); } assertEquals(0, env.kafkaClient().inFlightRequestCount()); List<NewTopic> newTopics = new ArrayList<>(); for (String sillyTopicName : sillyTopicNames) { newTopics.add(new NewTopic(sillyTopicName, 1, (short) 1)); } Map<String, KafkaFuture<Void>> createFutures = env.adminClient().createTopics(newTopics).values(); for (String sillyTopicName : sillyTopicNames) { TestUtils.assertFutureError(createFutures .get(sillyTopicName), InvalidTopicException.class); } assertEquals(0, env.kafkaClient().inFlightRequestCount()); } }
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)); }
/** * Create new topic if not exists * * @param name topic name * @param partitions * @param rf replication factor * @param prop properties */ public static final void createTopic(final String name, final int partitions, final short rf, final Properties prop) { try (final AdminClient adminClient = KafkaAdminClient.create(prop)) { try { final NewTopic newTopic = new NewTopic(name, partitions, rf); final CreateTopicsResult createTopicsResult = adminClient.createTopics(Collections.singleton(newTopic)); createTopicsResult.values().get(name).get(); } catch (InterruptedException | ExecutionException e) { if (!(e.getCause() instanceof TopicExistsException)) { throw new RuntimeException(e.getMessage(), e); } // TopicExistsException - Swallow this exception, just means the topic already exists. } } }
/** * Creates a topic in Kafka. If the topic already exists this does nothing. * @param topicName the topic name to create. * @param partitions the number of partitions to create. * @param replicationFactor the number of replicas for the topic. */ public void createTopic(final String topicName, final int partitions, final short replicationFactor) { // Create admin client try (final AdminClient adminClient = getAdminClient()) { // Define topic final NewTopic newTopic = new NewTopic(topicName, partitions, replicationFactor); // Create topic, which is async call. final CreateTopicsResult createTopicsResult = adminClient.createTopics(Collections.singleton(newTopic)); // Since the call is Async, Lets wait for it to complete. createTopicsResult.values().get(topicName).get(); } catch (InterruptedException | ExecutionException e) { if (!(e.getCause() instanceof TopicExistsException)) { throw new RuntimeException(e.getMessage(), e); } // TopicExistsException - Swallow this exception, just means the topic already exists. } }
Map<String, KafkaFuture<Void>> newResults = admin.createTopics(topicsByName.values(), args).values();
for (final Map.Entry<String, KafkaFuture<Void>> createTopicResult : createTopicsResult.values().entrySet()) { try { createTopicResult.getValue().get();
public boolean manageTopic(Topic topic) { log.debug("Requested update for {}", topic.getName() ); if (topics().contains(topic.getName())) { updateTopic(topic); return true; } NewTopic nt = new NewTopic(topic.getName(), partitionCount(topic), replicationFactor(topic)); nt.configs(topic.getProperties()); CreateTopicsResult ct = adminClient.createTopics(singleton(nt)); try { ct.all().get(); createdTopics.inc(ct.values().size()); log.info("Created topic. name: {}, partitions: {}, replFactor: {}", topic.getName(), partitionCount(topic), replicationFactor(topic)); return true; } catch (InterruptedException | ExecutionException e) { if (e.getCause() != null && e.getCause() instanceof TopicExistsException) { log.debug("Topic exists. name {}", topic.getName()); updateTopic(topic); return true; } else { log.info("Exception occured during topic creation. name {}", topic.getName(), e); return false; } } }