use of org.apache.kafka.clients.admin.NewTopic in project strimzi by strimzi.
the class TopicSerialization method toNewTopic.
/**
* Create a NewTopic to reflect the given Topic.
*/
public static NewTopic toNewTopic(Topic topic, Map<Integer, List<Integer>> assignment) {
NewTopic newTopic;
if (assignment != null) {
if (topic.getNumPartitions() != assignment.size()) {
throw new IllegalArgumentException(format("Topic %s has %d partitions supplied, but the number of partitions " + "configured in ConfigMap %s is %d", topic.getTopicName(), assignment.size(), topic.getMapName(), topic.getNumPartitions()));
}
for (int partition = 0; partition < assignment.size(); partition++) {
final List<Integer> value = assignment.get(partition);
if (topic.getNumReplicas() != value.size()) {
throw new IllegalArgumentException(format("Partition %d of topic %s has %d assigned replicas, " + "but the number of replicas configured in ConfigMap %s for the topic is %d", partition, topic.getTopicName(), value.size(), topic.getMapName(), topic.getNumReplicas()));
}
}
newTopic = new NewTopic(topic.getTopicName().toString(), assignment);
} else {
newTopic = new NewTopic(topic.getTopicName().toString(), topic.getNumPartitions(), topic.getNumReplicas());
}
newTopic.configs(topic.getConfig());
return newTopic;
}
use of org.apache.kafka.clients.admin.NewTopic in project strimzi by strimzi.
the class ControllerAssignedKafkaImpl method createTopic.
/**
* 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));
}
use of org.apache.kafka.clients.admin.NewTopic in project ksql by confluentinc.
the class KafkaTopicClientImplTest method singleNewTopic.
private static Collection<NewTopic> singleNewTopic(final NewTopic expected) {
class NewTopicsMatcher implements IArgumentMatcher {
@SuppressWarnings("unchecked")
@Override
public boolean matches(final Object argument) {
final Collection<NewTopic> newTopics = (Collection<NewTopic>) argument;
if (newTopics.size() != 1) {
return false;
}
final NewTopic actual = newTopics.iterator().next();
return Objects.equals(actual.name(), expected.name()) && Objects.equals(actual.replicationFactor(), expected.replicationFactor()) && Objects.equals(actual.numPartitions(), expected.numPartitions()) && Objects.equals(actual.configs(), expected.configs());
}
@Override
public void appendTo(final StringBuffer buffer) {
buffer.append("{NewTopic").append(expected).append("}");
}
}
EasyMock.reportMatcher(new NewTopicsMatcher());
return null;
}
use of org.apache.kafka.clients.admin.NewTopic in project ranger by apache.
the class KafkaRangerTopicCreationTest method testCreateTopic.
@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());
}
}
use of org.apache.kafka.clients.admin.NewTopic in project apache-kafka-on-k8s by banzaicloud.
the class WorkerUtilsTest method testCreatesOneTopicVerifiesOneTopic.
@Test
public void testCreatesOneTopicVerifiesOneTopic() throws Throwable {
final String existingTopic = "existing-topic";
List<TopicPartitionInfo> tpInfo = new ArrayList<>();
tpInfo.add(new TopicPartitionInfo(0, broker1, singleReplica, Collections.<Node>emptyList()));
tpInfo.add(new TopicPartitionInfo(1, broker2, singleReplica, Collections.<Node>emptyList()));
adminClient.addTopic(false, existingTopic, tpInfo, null);
Map<String, NewTopic> topics = new HashMap<>();
topics.put(existingTopic, new NewTopic(existingTopic, tpInfo.size(), TEST_REPLICATION_FACTOR));
topics.put(TEST_TOPIC, NEW_TEST_TOPIC);
WorkerUtils.createTopics(log, adminClient, topics, false);
assertEquals(Utils.mkSet(existingTopic, TEST_TOPIC), adminClient.listTopics().names().get());
}
Aggregations