Search in sources :

Example 11 with TopicDescription

use of org.apache.kafka.clients.admin.TopicDescription in project strimzi by strimzi.

the class BaseKafkaImpl method topicMetadata.

/**
 * Get a topic config via the Kafka AdminClient API, calling the given handler
 * (in a different thread) with the result.
 */
@Override
public void topicMetadata(TopicName topicName, Handler<AsyncResult<TopicMetadata>> handler) {
    LOGGER.debug("Getting metadata for topic {}", topicName);
    ConfigResource resource = new ConfigResource(ConfigResource.Type.TOPIC, topicName.toString());
    KafkaFuture<TopicDescription> descriptionFuture = adminClient.describeTopics(Collections.singleton(topicName.toString())).values().get(topicName.toString());
    KafkaFuture<Config> configFuture = adminClient.describeConfigs(Collections.singleton(resource)).values().get(resource);
    queueWork(new MetadataWork(descriptionFuture, configFuture, result -> handler.handle(result)));
}
Also used : Config(org.apache.kafka.clients.admin.Config) Logger(org.slf4j.Logger) Vertx(io.vertx.core.Vertx) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) KafkaFuture(org.apache.kafka.common.KafkaFuture) Future(io.vertx.core.Future) AdminClient(org.apache.kafka.clients.admin.AdminClient) ExecutionException(java.util.concurrent.ExecutionException) ConfigResource(org.apache.kafka.common.config.ConfigResource) Map(java.util.Map) ListTopicsResult(org.apache.kafka.clients.admin.ListTopicsResult) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) AsyncResult(io.vertx.core.AsyncResult) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) Handler(io.vertx.core.Handler) Collections(java.util.Collections) Config(org.apache.kafka.clients.admin.Config) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) ConfigResource(org.apache.kafka.common.config.ConfigResource)

Example 12 with TopicDescription

use of org.apache.kafka.clients.admin.TopicDescription in project ksql by confluentinc.

the class KafkaTopicClientImplIntegrationTest method shouldCreateTopic.

@Test
public void shouldCreateTopic() {
    // Given:
    final String topicName = UUID.randomUUID().toString();
    // When:
    client.createTopic(topicName, 3, (short) 1);
    // Then:
    assertThatEventually(() -> topicExists(topicName), is(true));
    final TopicDescription topicDescription = getTopicDescription(topicName);
    assertThat(topicDescription.partitions(), hasSize(3));
    assertThat(topicDescription.partitions().get(0).replicas(), hasSize(1));
}
Also used : TopicDescription(org.apache.kafka.clients.admin.TopicDescription) IntegrationTest(org.apache.kafka.test.IntegrationTest) Test(org.junit.Test)

Example 13 with TopicDescription

use of org.apache.kafka.clients.admin.TopicDescription in project ksql by confluentinc.

the class KafkaTopicClientImplTest method getDescribeTopicsResult.

private DescribeTopicsResult getDescribeTopicsResult() {
    TopicPartitionInfo topicPartitionInfo = new TopicPartitionInfo(0, node, Collections.singletonList(node), Collections.singletonList(node));
    TopicDescription topicDescription = new TopicDescription(topicName1, false, Collections.singletonList(topicPartitionInfo));
    DescribeTopicsResult describeTopicsResult = mock(DescribeTopicsResult.class);
    expect(describeTopicsResult.all()).andReturn(KafkaFuture.completedFuture(Collections.singletonMap(topicName1, topicDescription)));
    replay(describeTopicsResult);
    return describeTopicsResult;
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) DescribeTopicsResult(org.apache.kafka.clients.admin.DescribeTopicsResult) TopicDescription(org.apache.kafka.clients.admin.TopicDescription)

Example 14 with TopicDescription

use of org.apache.kafka.clients.admin.TopicDescription in project ksql by confluentinc.

the class KafkaTopicClientImpl method validateTopicProperties.

private void validateTopicProperties(final String topic, final int numPartitions, final short replicationFactor) {
    Map<String, TopicDescription> topicDescriptions = describeTopics(Collections.singletonList(topic));
    TopicDescription topicDescription = topicDescriptions.get(topic);
    if (topicDescription.partitions().size() != numPartitions || topicDescription.partitions().get(0).replicas().size() < replicationFactor) {
        throw new KafkaTopicException(String.format("Topic '%s' does not conform to the requirements Partitions:%d v %d. Replication: %d " + "v %d", topic, topicDescription.partitions().size(), numPartitions, topicDescription.partitions().get(0).replicas().size(), replicationFactor));
    }
    // Topic with the partitions and replicas exists, reuse it!
    log.debug("Did not create topic {} with {} partitions and replication-factor {} since it already " + "exists", topic, numPartitions, replicationFactor);
}
Also used : KafkaTopicException(io.confluent.ksql.exception.KafkaTopicException) TopicDescription(org.apache.kafka.clients.admin.TopicDescription)

Example 15 with TopicDescription

use of org.apache.kafka.clients.admin.TopicDescription in project ksql by confluentinc.

the class KafkaTopicsListTest method shouldBuildValidTopicList.

@Test
public void shouldBuildValidTopicList() {
    Collection<KsqlTopic> ksqlTopics = Collections.emptyList();
    // represent the full list of topics
    Map<String, TopicDescription> topicDescriptions = new HashMap<>();
    TopicPartitionInfo topicPartitionInfo = new TopicPartitionInfo(1, new Node(1, "", 8088), Collections.emptyList(), Collections.emptyList());
    topicDescriptions.put("test-topic", new TopicDescription("test-topic", false, Collections.singletonList(topicPartitionInfo)));
    /**
     * Return POJO for consumerGroupClient
     */
    TopicPartition topicPartition = new TopicPartition("test-topic", 1);
    KafkaConsumerGroupClientImpl.ConsumerSummary consumerSummary = new KafkaConsumerGroupClientImpl.ConsumerSummary("consumer-id");
    consumerSummary.addPartition(topicPartition);
    KafkaConsumerGroupClientImpl.ConsumerGroupSummary consumerGroupSummary = new KafkaConsumerGroupClientImpl.ConsumerGroupSummary();
    consumerGroupSummary.addConsumerSummary(consumerSummary);
    KafkaConsumerGroupClient consumerGroupClient = mock(KafkaConsumerGroupClient.class);
    expect(consumerGroupClient.listGroups()).andReturn(Collections.singletonList("test-topic"));
    expect(consumerGroupClient.describeConsumerGroup("test-topic")).andReturn(consumerGroupSummary);
    replay(consumerGroupClient);
    /**
     * Test
     */
    KafkaTopicsList topicsList = KafkaTopicsList.build("statement test", ksqlTopics, topicDescriptions, new KsqlConfig(Collections.EMPTY_MAP), consumerGroupClient);
    assertThat(topicsList.getTopics().size(), equalTo(1));
    KafkaTopicInfo first = topicsList.getTopics().iterator().next();
    assertThat(first.getConsumerGroupCount(), equalTo(1));
    assertThat(first.getConsumerCount(), equalTo(1));
    assertThat(first.getReplicaInfo().size(), equalTo(1));
}
Also used : HashMap(java.util.HashMap) Node(org.apache.kafka.common.Node) KsqlConfig(io.confluent.ksql.util.KsqlConfig) TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) KafkaConsumerGroupClientImpl(io.confluent.ksql.util.KafkaConsumerGroupClientImpl) TopicPartition(org.apache.kafka.common.TopicPartition) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) KafkaConsumerGroupClient(io.confluent.ksql.util.KafkaConsumerGroupClient) KsqlTopic(io.confluent.ksql.metastore.KsqlTopic) Test(org.junit.Test)

Aggregations

TopicDescription (org.apache.kafka.clients.admin.TopicDescription)22 Test (org.junit.Test)10 TopicPartitionInfo (org.apache.kafka.common.TopicPartitionInfo)9 Node (org.apache.kafka.common.Node)8 Map (java.util.Map)6 HashMap (java.util.HashMap)4 AdminClient (org.apache.kafka.clients.admin.AdminClient)4 KsqlConfig (io.confluent.ksql.util.KsqlConfig)3 ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 ExecutionException (java.util.concurrent.ExecutionException)3 DescribeTopicsResult (org.apache.kafka.clients.admin.DescribeTopicsResult)3 NewTopic (org.apache.kafka.clients.admin.NewTopic)3 KafkaFuture (org.apache.kafka.common.KafkaFuture)3 KsqlTopic (io.confluent.ksql.metastore.KsqlTopic)2 KafkaConsumerGroupClient (io.confluent.ksql.util.KafkaConsumerGroupClient)2 KafkaConsumerGroupClientImpl (io.confluent.ksql.util.KafkaConsumerGroupClientImpl)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2