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

The following examples show how to use org.apache.kafka.clients.admin.CreateTopicsOptions. 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: 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 #2
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 #3
Source File: KafkaSetupHandler.java    From stream-registry with Apache License 2.0 5 votes vote down vote up
private Optional<TopicDescription> createTopic(AdminClient client, NewTopic newTopic) throws ExecutionException, InterruptedException {
  client.createTopics(
      Collections.singleton(newTopic),
      new CreateTopicsOptions().timeoutMs(DEFAULT_CREATE_TOPIC_TIMEOUT_MS)
  ).all().get();

  return obtainTopicDescription(client, newTopic.name());
}
 
Example #4
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 #5
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);
}