Search in sources :

Example 61 with NewTopic

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;
}
Also used : NewTopic(org.apache.kafka.clients.admin.NewTopic)

Example 62 with 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));
}
Also used : NewTopic(org.apache.kafka.clients.admin.NewTopic)

Example 63 with NewTopic

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;
}
Also used : IArgumentMatcher(org.easymock.IArgumentMatcher) Collection(java.util.Collection) EasyMock.anyObject(org.easymock.EasyMock.anyObject) NewTopic(org.apache.kafka.clients.admin.NewTopic)

Example 64 with NewTopic

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());
    }
}
Also used : CreateTopicsResult(org.apache.kafka.clients.admin.CreateTopicsResult) KafkaFuture(org.apache.kafka.common.KafkaFuture) NewTopic(org.apache.kafka.clients.admin.NewTopic) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map) AdminClient(org.apache.kafka.clients.admin.AdminClient) KafkaAdminClient(org.apache.kafka.clients.admin.KafkaAdminClient) Test(org.junit.Test)

Example 65 with NewTopic

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());
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) HashMap(java.util.HashMap) Node(org.apache.kafka.common.Node) ArrayList(java.util.ArrayList) NewTopic(org.apache.kafka.clients.admin.NewTopic) Test(org.junit.Test)

Aggregations

NewTopic (org.apache.kafka.clients.admin.NewTopic)132 Test (org.junit.Test)65 HashMap (java.util.HashMap)37 AdminClient (org.apache.kafka.clients.admin.AdminClient)35 Cluster (org.apache.kafka.common.Cluster)24 ExecutionException (java.util.concurrent.ExecutionException)23 TopicExistsException (org.apache.kafka.common.errors.TopicExistsException)20 MockAdminClient (org.apache.kafka.clients.admin.MockAdminClient)19 Config (org.apache.kafka.clients.admin.Config)16 MockTime (org.apache.kafka.common.utils.MockTime)16 AdminClientUnitTestEnv (org.apache.kafka.clients.admin.AdminClientUnitTestEnv)15 Map (java.util.Map)14 TopicDescription (org.apache.kafka.clients.admin.TopicDescription)13 TopicConfig (org.apache.kafka.common.config.TopicConfig)13 StreamsConfig (org.apache.kafka.streams.StreamsConfig)12 ArrayList (java.util.ArrayList)11 CreateTopicsResult (org.apache.kafka.clients.admin.CreateTopicsResult)11 TimeoutException (org.apache.kafka.common.errors.TimeoutException)11 TopicMetadataAndConfig (org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig)10 ConsumerConfig (org.apache.kafka.clients.consumer.ConsumerConfig)10