org.apache.kafka.clients.admin.CreateTopicsResult Java Examples

The following examples show how to use org.apache.kafka.clients.admin.CreateTopicsResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KafkaTestContainerManager.java    From apicurio-registry with Apache License 2.0 7 votes vote down vote up
private void createTopics(String bootstrapServers) {
    Properties properties = new Properties();
    properties.put("bootstrap.servers", bootstrapServers);
    properties.put("connections.max.idle.ms", 10000);
    properties.put("request.timeout.ms", 5000);
    try (AdminClient client = AdminClient.create(properties)) {
        CreateTopicsResult result = client.createTopics(Arrays.asList(
                new NewTopic("storage-topic", 1, (short) 1),
                new NewTopic("global-id-topic", 1, (short) 1),
                new NewTopic("snapshot-topic", 1, (short) 1)
        ));
        try {
            result.all().get();
        } catch ( InterruptedException | ExecutionException e ) {
            throw new IllegalStateException(e);
        }
    }
}
 
Example #2
Source File: AdminClientWrapper.java    From hdinsight-kafka-java-get-started with MIT License 6 votes vote down vote up
public static void createTopics(String brokers, String topicName) throws IOException {
    // Set properties used to configure admin client
    Properties properties = getProperties(brokers);

    try (final AdminClient adminClient = KafkaAdminClient.create(properties)) {
        int numPartitions = 8;
        short replicationFactor = (short)3;
        final NewTopic newTopic = new NewTopic(topicName, numPartitions, replicationFactor);

        final CreateTopicsResult createTopicsResult = adminClient.createTopics(Collections.singleton(newTopic));
        createTopicsResult.values().get(topicName).get();
        System.out.print("Topic " + topicName + " created");
    } catch (Exception e) {
        System.out.print("Create Topics denied\n");
        System.out.print(e.getMessage());
        //throw new RuntimeException(e.getMessage(), e);
    }
}
 
Example #3
Source File: AdminClientWrapper.java    From hdinsight-kafka-java-get-started with MIT License 6 votes vote down vote up
public static void createTopics(String brokers, String topicName) throws IOException {
    // Set properties used to configure admin client
    Properties properties = getProperties(brokers);

    try (final AdminClient adminClient = KafkaAdminClient.create(properties)) {
        int numPartitions = 8;
        short replicationFactor = (short)3;
        final NewTopic newTopic = new NewTopic(topicName, numPartitions, replicationFactor);

        final CreateTopicsResult createTopicsResult = adminClient.createTopics(Collections.singleton(newTopic));
        createTopicsResult.values().get(topicName).get();
        System.out.print("Topic " + topicName + " created");
    } catch (Exception e) {
        System.out.print("Create Topics denied\n");
        System.out.print(e.getMessage());
        //throw new RuntimeException(e.getMessage(), e);
    }
}
 
Example #4
Source File: KafkaChannelAutoConfiguration.java    From servicecomb-pack with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() {
  Map props = new HashMap<>();
  props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrap_servers);
  props.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 50000);
  try (final AdminClient adminClient = KafkaAdminClient.create(props)) {
    try {
      final NewTopic newTopic = new NewTopic(topic, numPartitions, replicationFactor);
      final CreateTopicsResult createTopicsResult = adminClient
          .createTopics(Collections.singleton(newTopic));
      createTopicsResult.values().get(topic).get();
    } catch (InterruptedException | ExecutionException e) {
      if (e.getCause() instanceof InterruptedException) {
        Thread.currentThread().interrupt();
      }
      if (!(e.getCause() instanceof TopicExistsException)) {
        throw new RuntimeException(e.getMessage(), e);
      }
    }
  }
  LOG.info("Kafka Channel Init");
}
 
Example #5
Source File: CreateTopics.java    From phoebus with Eclipse Public License 1.0 6 votes vote down vote up
/** Create a topic for each of the topics in the passed list.
 *  @param client {@link AdminClient}
 *  @param compact If the topics should be compacted.
 *  @param topics_to_create {@link List} of {@link String}s filled with the names of topics to create.
 */
private static void createTopics(final AdminClient client, final boolean compact, final List<String> topics_to_create)
{
    // Create the new topics locally.
    final List<NewTopic> new_topics = new ArrayList<>();
    for (String topic : topics_to_create)
    {
            logger.info("Creating topic '" + topic + "'");
            new_topics.add(createTopic(client, compact, topic));
    }
    // Create the new topics in the Kafka server.
    try
    {
        final CreateTopicsResult res = client.createTopics(new_topics);
        final KafkaFuture<Void> future = res.all();
        future.get();
    }
    catch (Exception ex)
    {
        logger.log(Level.WARNING, "Attempt to create topics failed", ex);
    }
}
 
Example #6
Source File: KafkaAdminClientImpl.java    From vertx-kafka-client with Apache License 2.0 6 votes vote down vote up
@Override
public Future<Void> createTopics(List<NewTopic> topics) {
  ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext();
  Promise<Void> promise = ctx.promise();

  CreateTopicsResult createTopicsResult = this.adminClient.createTopics(Helper.toNewTopicList(topics));
  createTopicsResult.all().whenComplete((v, ex) -> {

    if (ex == null) {
      promise.complete();
    } else {
      promise.fail(ex);
    }
  });
  return promise.future();
}
 
Example #7
Source File: KafkaTestUtils.java    From kafka-junit with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * 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.
    }
}
 
Example #8
Source File: CruiseControlMetricsReporter.java    From cruise-control with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void createCruiseControlMetricsTopic() throws TopicExistsException {
  CruiseControlMetricsUtils.retry(() -> {
    try {
      CreateTopicsResult createTopicsResult = _adminClient.createTopics(Collections.singletonList(_metricsTopic));
      createTopicsResult.values().get(_metricsTopic.name()).get(_metricsTopicAutoCreateTimeoutMs, TimeUnit.MILLISECONDS);
      LOG.info("Cruise Control metrics topic {} is created.", _metricsTopic.name());
      return false;
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
      if (e.getCause() instanceof TopicExistsException) {
        throw (TopicExistsException) e.getCause();
      }
      LOG.warn("Unable to create Cruise Control metrics topic {}.", _metricsTopic.name(), e);
      return true;
    }
  }, _metricsTopicAutoCreateRetries);
}
 
Example #9
Source File: MultiClusterTopicManagementServiceTest.java    From kafka-monitor with Apache License 2.0 6 votes vote down vote up
@BeforeMethod
private void startTest() {
  _createTopicsResult = Mockito.mock(CreateTopicsResult.class);
  _kafkaFutureMap = Mockito.mock(Map.class);
  _kafkaFuture = Mockito.mock(KafkaFuture.class);

  nodeSet = new LinkedHashSet<>();
  nodeSet.add(new Node(1, "host-1", 2132));
  nodeSet.add(new Node(2, "host-2", 2133));
  nodeSet.add(new Node(3, "host-3", 2134));
  nodeSet.add(new Node(4, "host-4", 2135));
  nodeSet.add(new Node(5, "host-5", 2136));
  nodeSet.add(new Node(6, "host-5", 2137));
  nodeSet.add(new Node(7, "host-5", 2138));
  nodeSet.add(new Node(8, "host-5", 2139));
  nodeSet.add(new Node(9, "host-5", 2140));
  nodeSet.add(new Node(10, "host-5", 2141));

  _topicManagementHelper = Mockito.mock(MultiClusterTopicManagementService.TopicManagementHelper.class);
  _topicManagementHelper._topic = SERVICE_TEST_TOPIC;
  _topicManagementHelper._adminClient = Mockito.mock(AdminClient.class);
  _topicManagementHelper._topicFactory = Mockito.mock(TopicFactory.class);
  _topicManagementHelper._topicCreationEnabled = true;
}
 
Example #10
Source File: TopicAdmin.java    From kafka-message-tool with MIT License 6 votes vote down vote up
public void createNewTopic(TopicToAdd topicToAdd) throws Exception {
    final String topicName = topicToAdd.getTopicName();
    Logger.trace(String.format("Creating topic '%s' [partitions:%d, replication factor:%d, cleanup policy:%s]",
                               topicName,
                               topicToAdd.getPartitions(),
                               topicToAdd.getReplicationFactor(),
                               topicToAdd.getCleanupPolicy()));

    final NewTopic newTopic = new NewTopic(topicName,
                                           topicToAdd.getPartitions(),
                                           (short) topicToAdd.getReplicationFactor());
    newTopic.configs(topicConfigsMapFromTopicToAdd(topicToAdd));

    final CreateTopicsResult result = kafkaClientsAdminClient.createTopics(Collections.singletonList(newTopic));
    interpretCreateTopicResult(topicName, result);
}
 
Example #11
Source File: KafkaRule.java    From kafka-pubsub-emulator with Apache License 2.0 6 votes vote down vote up
public void createTopic(String name) throws Exception {
  try (AdminClient admin = AdminClient.create(getAdminConfig())) {
    int partitions = 3;
    int replication = this.replicationFactor;
    Matcher matcher = Pattern.compile("(-\\d+[p|r])").matcher(name);
    while (matcher.find()) {
      String group = matcher.group();
      if (group.endsWith("p")) {
        partitions = getNumber(group);
      } else {
        replication = getNumber(group);
      }
    }

    NewTopic topic = new NewTopic(name, partitions, (short) replication);
    CreateTopicsResult topicsResult = admin.createTopics(singleton(topic));
    topicsResult.all().get(5, TimeUnit.SECONDS);
  } catch (Exception e) {
    LOGGER.log(Level.SEVERE, "Error on create topics " + name, e);
    throw e;
  }
}
 
Example #12
Source File: SimpleProducer.java    From kafka-platform-prometheus with Apache License 2.0 6 votes vote down vote up
private void createTopic(AdminClient adminClient, String topicName, Integer numberOfPartitions, Short replicationFactor) throws InterruptedException, ExecutionException {
    if (!adminClient.listTopics().names().get().contains(topicName)) {
        logger.info("Creating topic {}", topicName);

        final Map<String, String> configs = replicationFactor < 3 ? Map.of(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG, "1") : Map.of();

        final NewTopic newTopic = new NewTopic(topicName, numberOfPartitions, replicationFactor);
        newTopic.configs(configs);
        try {
            CreateTopicsResult topicsCreationResult = adminClient.createTopics(Collections.singleton(newTopic));
            topicsCreationResult.all().get();
        } catch (ExecutionException e) {
            //silent ignore if topic already exists
        }
    }
}
 
Example #13
Source File: KafkaEmbedded.java    From micronaut-kafka with Apache License 2.0 6 votes vote down vote up
private void createTopics(int targetPort, Integer numPartitions) throws InterruptedException, java.util.concurrent.ExecutionException {
    List<String> topics = embeddedConfiguration.getTopics();

    if (LOG.isDebugEnabled()) {
        LOG.debug("Creating Kafka Topics in Embedded Kafka: {}", topics);
    }
    if (!topics.isEmpty()) {
        Properties properties = new Properties();
        properties.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, ("127.0.0.1:" + targetPort));
        AdminClient adminClient = AdminClient.create(properties);
        final CreateTopicsResult result = adminClient.createTopics(topics.stream().map(s ->
                new NewTopic(s, numPartitions, (short) 1)).collect(Collectors.toList())
        );
        result.all().get();

        if (LOG.isInfoEnabled()) {
            LOG.info("Created Kafka Topics in Embedded Kafka: {}", topics);
        }
    }
}
 
Example #14
Source File: KafkaRangerTopicCreationTest.java    From ranger with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateTopic() throws Exception {
        final String topic = "test";
        Properties properties = new Properties();
        properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:" + port);
        properties.put("client.id", "test-consumer-id");
        properties.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SASL_PLAINTEXT");
        AdminClient client = KafkaAdminClient.create(properties);
        CreateTopicsResult result = client.createTopics(Arrays.asList(new NewTopic(topic, 1, (short) 1)));
        result.values().get(topic).get();
        for (Map.Entry<String, KafkaFuture<Void>> entry : result.values().entrySet()) {
            System.out.println("Create Topic : " + entry.getKey() + " " +
                    "isCancelled : " + entry.getValue().isCancelled() + " " +
                    "isCompletedExceptionally : " + entry.getValue().isCompletedExceptionally() + " " +
                    "isDone : " + entry.getValue().isDone());
        }
}
 
Example #15
Source File: TopicServiceImplTest.java    From kafka-helmsman with MIT License 6 votes vote down vote up
@Test
public void testCreate() {
  TopicService service = new TopicServiceImpl(adminClient, true);
  CreateTopicsResult createTopicsResult = mock(CreateTopicsResult.class);
  when(createTopicsResult.all()).thenReturn(KafkaFuture.completedFuture(null));
  when(adminClient.createTopics(any(Collection.class),
      any(CreateTopicsOptions.class))).thenReturn(createTopicsResult);

  service.create(Collections.singletonList(
      new ConfiguredTopic("test", 1, (short) 2, Collections.emptyMap())));

  ArgumentCaptor<List> newTopics = ArgumentCaptor.forClass(List.class);
  ArgumentCaptor<CreateTopicsOptions> options = ArgumentCaptor.forClass(CreateTopicsOptions.class);
  verify(adminClient).createTopics((Collection<NewTopic>) newTopics.capture(), options.capture());
  Assert.assertEquals(1, newTopics.getValue().size());
  Assert.assertEquals("test", ((NewTopic) newTopics.getValue().get(0)).name());
  Assert.assertEquals(2, ((NewTopic) newTopics.getValue().get(0)).replicationFactor());
  Assert.assertTrue(options.getValue().shouldValidateOnly());
}
 
Example #16
Source File: TopicOperatorBaseIT.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
protected String createTopic(String topicName, NewTopic o) throws InterruptedException, ExecutionException, TimeoutException {
    LOGGER.info("Creating topic {}", topicName);
    // Create a topic
    String resourceName = new TopicName(topicName).asKubeName().toString();
    CreateTopicsResult crt = adminClient.createTopics(singletonList(o));
    crt.all().get();

    // Wait for the resource to be created
    waitForTopicInKube(resourceName);

    LOGGER.info("topic {} has been created", resourceName);
    return resourceName;
}
 
Example #17
Source File: TopicST.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Tag(NODEPORT_SUPPORTED)
@Test
void testCreateTopicViaAdminClient() throws ExecutionException, InterruptedException, TimeoutException {
    String clusterName = CLUSTER_NAME + "-external-name";

    KafkaResource.kafkaEphemeral(clusterName, 3, 3)
        .editSpec()
            .editKafka()
                .editListeners()
                    .withNewKafkaListenerExternalNodePort()
                        .withTls(false)
                    .endKafkaListenerExternalNodePort()
                .endListeners()
            .endKafka()
        .endSpec()
        .done();

    Properties properties = new Properties();
    properties.setProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, AbstractKafkaClient.getExternalBootstrapConnect(NAMESPACE, clusterName));

    try (AdminClient adminClient = AdminClient.create(properties)) {

        LOGGER.info("Creating async topic {} via Admin client", TOPIC_NAME);
        CreateTopicsResult crt = adminClient.createTopics(singletonList(new NewTopic(TOPIC_NAME, 1, (short) 1)));
        crt.all().get();

        Set<String> topics = adminClient.listTopics().names().get(Constants.GLOBAL_CLIENTS_TIMEOUT, TimeUnit.MILLISECONDS);

        LOGGER.info("Verify that in Kafka cluster contains {} topics", 1);
        assertThat(topics.size(), is(1));
        assertThat(topics.contains(TOPIC_NAME), is(true));
    }

    LOGGER.info("Verify that corresponding {} KafkaTopic custom resources were created and topic is in Ready state", 1);
    KafkaTopicUtils.waitForKafkaTopicCreation(TOPIC_NAME);
    KafkaTopicUtils.waitForKafkaTopicReady(TOPIC_NAME);
}
 
Example #18
Source File: Utils.java    From kafka-monitor with Apache License 2.0 5 votes vote down vote up
/**
 * Create the topic. This method attempts to create a topic so that all
 * the brokers in the cluster will have partitionToBrokerRatio partitions.  If the topic exists, but has different parameters
 * then this does nothing to update the parameters.
 *
 * TODO: Do we care about rack aware mode?  I would think no because we want to spread the topic over all brokers.
 * @param topic topic name
 * @param replicationFactor the replication factor for the topic
 * @param partitionToBrokerRatio This is multiplied by the number brokers to compute the number of partitions in the topic.
 * @param minPartitionNum partition number to be created at least
 * @param topicConfig additional parameters for the topic for example min.insync.replicas
 * @param adminClient AdminClient object initialized.
 * @return the number of partitions created
 * @throws ExecutionException exception thrown then executing the topic creation fails.
 * @throws InterruptedException exception that's thrown when interrupt occurs.
 */
@SuppressWarnings("unchecked")
public static int createTopicIfNotExists(String topic, short replicationFactor, double partitionToBrokerRatio,
    int minPartitionNum, Properties topicConfig, AdminClient adminClient)
    throws ExecutionException, InterruptedException {
  try {
    if (adminClient.listTopics().names().get().contains(topic)) {
      LOG.info("AdminClient indicates that {} already exists in the cluster. Topic config: {}", topic, topicConfig);
      return getPartitionNumForTopic(adminClient, topic);
    }
    int brokerCount = Utils.getBrokerCount(adminClient);
    int partitionCount = Math.max((int) Math.ceil(brokerCount * partitionToBrokerRatio), minPartitionNum);
    try {
      NewTopic newTopic = new NewTopic(topic, partitionCount, replicationFactor);
      //noinspection rawtypes
      newTopic.configs((Map) topicConfig);

      List<NewTopic> topics = new ArrayList<>();
      topics.add(newTopic);
      CreateTopicsResult result = adminClient.createTopics(topics);

      // waits for this topic creation future to complete, and then returns its result.
      result.values().get(topic).get();
      LOG.info("CreateTopicsResult: {}.", result.values());

    } catch (TopicExistsException e) {
      /* There is a race condition with the consumer. */
      LOG.info("Monitoring topic " + topic + " already exists in the cluster.", e);
      return getPartitionNumForTopic(adminClient, topic);
    }
    LOG.info("Created monitoring topic {} in cluster with {} partitions and replication factor of {}.", topic,
        partitionCount, replicationFactor);

    return partitionCount;
  } finally {
    LOG.info("Completed the topic creation if it doesn't exist for {}.", topic);
  }
}
 
Example #19
Source File: KafkaBinderTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
private void invokeCreateTopic(String topic, int partitions, int replicationFactor)
		throws Exception {

	NewTopic newTopic = new NewTopic(topic, partitions, (short) replicationFactor);
	CreateTopicsResult topics = adminClient
			.createTopics(Collections.singletonList(newTopic));
	topics.all().get(DEFAULT_OPERATION_TIMEOUT, TimeUnit.SECONDS);
}
 
Example #20
Source File: IntegrationTestHarness.java    From samza with Apache License 2.0 5 votes vote down vote up
protected boolean createTopics(Collection<NewTopic> newTopics) {
  boolean createStatus = true;

  try {
    CreateTopicsResult resultFuture =
        adminClient.createTopics(newTopics);
    resultFuture.all().get(ADMIN_OPERATION_WAIT_DURATION_MS, TimeUnit.MILLISECONDS);
  } catch (Exception e) {
    LOG.error("Error creating topics: {}", StringUtils.join(newTopics, ","), e);
    createStatus = false;
  }

  return createStatus;
}
 
Example #21
Source File: MiniKafkaCluster.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
public boolean createTopic(String topicName, int numPartitions, int replicationFactor) {
  NewTopic newTopic = new NewTopic(topicName, numPartitions, (short) replicationFactor);
  CreateTopicsResult createTopicsResult = this.adminClient.createTopics(Arrays.asList(newTopic));
  try {
    createTopicsResult.all().get();
  } catch (InterruptedException | ExecutionException e) {
    LOGGER.error("Failed to create Kafka topic: {}, Exception: {}", newTopic.toString(), e);
    return false;
  }
  return true;
}
 
Example #22
Source File: KafkaAdmin.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public CreateTopicsResult createTopic(String name, int numPartitions, short replicationFactor) {
	List<NewTopic> newTopics = new ArrayList<NewTopic>();
	NewTopic topic = new NewTopic(name, numPartitions, replicationFactor);
	newTopics.add(topic);
	
	CreateTopicsOptions cto = new CreateTopicsOptions();
	cto.timeoutMs(5 * 1000);
	return adminClient.createTopics(newTopics, cto);
}
 
Example #23
Source File: TopicEnsure.java    From common-docker with Apache License 2.0 5 votes vote down vote up
public boolean createTopic(TopicSpec spec, int timeOut) throws Exception {
  NewTopic newTopic = new NewTopic(
      spec.name(), spec.partitions(), (short) spec.replicationFactor()
  );
  newTopic.configs(spec.config());
  CreateTopicsResult result = adminClient.createTopics(
      Collections.singletonList(newTopic), new CreateTopicsOptions().timeoutMs(timeOut)
  );
  result.all().get();
  return true;
}
 
Example #24
Source File: DefaultCollector.java    From paraflow with Apache License 2.0 5 votes vote down vote up
@Override
public void createTopic(String topicName, int partitionsNum, short replicationFactor)
{
    NewTopic newTopic = new NewTopic(topicName, partitionsNum, replicationFactor);
    CreateTopicsResult result = kafkaAdminClient.createTopics(Collections.singletonList(newTopic));
    KafkaFuture future = result.values().get(topicName);
    try {
        future.get();
    }
    catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
}
 
Example #25
Source File: CruiseControlMetricsReporterAutoCreateTopicTest.java    From cruise-control with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Setup the unit test.
 */
@Before
public void setUp() {
    super.setUp();

    // creating the "TestTopic" explicitly because the topic auto-creation is disabled on the broker
    Properties adminProps = new Properties();
    adminProps.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers());
    AdminClient adminClient = AdminClient.create(adminProps);
    NewTopic testTopic = new NewTopic(TEST_TOPIC, 1, (short) 1);
    CreateTopicsResult createTopicsResult = adminClient.createTopics(Collections.singleton(testTopic));

    AtomicInteger adminFailed = new AtomicInteger(0);
    createTopicsResult.all().whenComplete((v, e) -> {
        if (e != null) {
            adminFailed.incrementAndGet();
        }
    });
    assertEquals(0, adminFailed.get());

    // starting producer to verify that Kafka cluster is working fine
    Properties producerProps = new Properties();
    producerProps.setProperty(ProducerConfig.ACKS_CONFIG, "-1");
    AtomicInteger producerFailed = new AtomicInteger(0);
    try (Producer<String, String> producer = createProducer(producerProps)) {
        for (int i = 0; i < 10; i++) {
            producer.send(new ProducerRecord<>(TEST_TOPIC, Integer.toString(i)),
                    (m, e) -> {
                        if (e != null) {
                            producerFailed.incrementAndGet();
                        }
                    });
        }
    }
    assertEquals(0, producerFailed.get());
}
 
Example #26
Source File: KafkaSampleStore.java    From cruise-control with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Creates the given topic if it does not exist.
 *
 * @param adminClient The adminClient to send createTopics request.
 * @param topicToBeCreated A wrapper around the topic to be created.
 * @return {@code false} if the topic to be created already exists, {@code true} otherwise.
 */
protected static boolean createTopic(AdminClient adminClient, NewTopic topicToBeCreated) {
  try {
    CreateTopicsResult createTopicsResult = adminClient.createTopics(Collections.singletonList(topicToBeCreated));
    createTopicsResult.values().get(topicToBeCreated.name()).get(CruiseControlMetricsUtils.CLIENT_REQUEST_TIMEOUT_MS,
                                                                 TimeUnit.MILLISECONDS);
    LOG.info("Topic {} has been created.", topicToBeCreated.name());
  } catch (InterruptedException | ExecutionException | TimeoutException e) {
    if (e.getCause() instanceof TopicExistsException) {
      return false;
    }
    throw new IllegalStateException(String.format("Unable to create topic %s.", topicToBeCreated.name()), e);
  }
  return true;
}
 
Example #27
Source File: KafkaTopicClientImplTest.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private CreateTopicsResult createTopicReturningTopicExistsException() {
  CreateTopicsResult createTopicsResult = niceMock(CreateTopicsResult.class);
  expect(createTopicsResult.all())
      .andReturn(failedFuture(new TopicExistsException("Topic already exists")));
  replay(createTopicsResult);
  return createTopicsResult;
}
 
Example #28
Source File: SimpleKafkaStreams.java    From kafka-platform-prometheus with Apache License 2.0 5 votes vote down vote up
private void createTopic(AdminClient adminClient, String topicName, Integer numberOfPartitions, Short replicationFactor) throws InterruptedException, ExecutionException {
    if (!adminClient.listTopics().names().get().contains(topicName)) {
        logger.info("Creating topic {}", topicName);
        final NewTopic newTopic = new NewTopic(topicName, numberOfPartitions, replicationFactor);
        try {
            CreateTopicsResult topicsCreationResult = adminClient.createTopics(Collections.singleton(newTopic));
            topicsCreationResult.all().get();
        } catch (ExecutionException e) {
            //silent ignore if topic already exists
        }
    }
}
 
Example #29
Source File: TopicServiceImpl.java    From kafka-helmsman with MIT License 5 votes vote down vote up
@Override
public void create(List<ConfiguredTopic> topics) {
  try {
    CreateTopicsOptions options = dryRun ? new CreateTopicsOptions().validateOnly(true) : new CreateTopicsOptions();
    List<NewTopic> newTopics = topics
        .stream()
        .map(t -> new NewTopic(t.getName(), t.getPartitions(), t.getReplicationFactor()).configs(t.getConfig()))
        .collect(toList());
    CreateTopicsResult result = adminClient.createTopics(newTopics, options);
    result.all().get();
  } catch (InterruptedException | ExecutionException e) {
    // TODO: FA-10109: Improve exception handling
    throw new RuntimeException(e);
  }
}
 
Example #30
Source File: KafkaTopicClientImplTest.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 4 votes vote down vote up
private CreateTopicsResult getCreateTopicsResult() {
  CreateTopicsResult createTopicsResult = mock(CreateTopicsResult.class);
  expect(createTopicsResult.all()).andReturn(KafkaFuture.allOf());
  replay(createTopicsResult);
  return createTopicsResult;
}