Search in sources :

Example 1 with Topic

use of io.confluent.kafkarest.entities.Topic in project kafka-rest by confluentinc.

the class TopicsResource method list.

@GET
@PerformanceMetric("topics.list+v2")
@ResourceName("api.v2.topics.list")
public void list(@Suspended AsyncResponse asyncResponse) {
    TopicManager topicManager = topicManagerProvider.get();
    CompletableFuture<List<String>> response = topicManager.listLocalTopics().thenApply(topics -> topics.stream().map(Topic::getName).collect(Collectors.toList()));
    AsyncResponses.asyncResume(asyncResponse, response);
}
Also used : TopicManager(io.confluent.kafkarest.controllers.TopicManager) List(java.util.List) Topic(io.confluent.kafkarest.entities.Topic) PerformanceMetric(io.confluent.rest.annotations.PerformanceMetric) ResourceName(io.confluent.kafkarest.extension.ResourceAccesslistFeature.ResourceName) GET(javax.ws.rs.GET)

Example 2 with Topic

use of io.confluent.kafkarest.entities.Topic in project kafka-rest by confluentinc.

the class TopicManagerImpl method createTopic.

@Override
public CompletableFuture<Void> createTopic(String clusterId, String topicName, Optional<Integer> partitionsCount, Optional<Short> replicationFactor, Map<Integer, List<Integer>> replicasAssignments, Map<String, Optional<String>> configs) {
    requireNonNull(topicName);
    Map<String, String> nullableConfigs = new HashMap<>();
    configs.forEach((key, value) -> nullableConfigs.put(key, value.orElse(null)));
    // A new topic can be created with either uniform replication according to the given partitions
    // count and replication factor, or explicitly specified partition-to-replicas assignments.
    NewTopic createTopicRequest = replicasAssignments.isEmpty() ? new NewTopic(topicName, partitionsCount, replicationFactor).configs(nullableConfigs) : new NewTopic(topicName, replicasAssignments).configs(nullableConfigs);
    return clusterManager.getCluster(clusterId).thenApply(cluster -> checkEntityExists(cluster, "Cluster %s cannot be found.", clusterId)).thenCompose(cluster -> KafkaFutures.toCompletableFuture(adminClient.createTopics(singletonList(createTopicRequest)).all()));
}
Also used : DescribeTopicsOptions(org.apache.kafka.clients.admin.DescribeTopicsOptions) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) KafkaFutures(io.confluent.kafkarest.common.KafkaFutures) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) Admin(org.apache.kafka.clients.admin.Admin) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) Collections.emptySet(java.util.Collections.emptySet) Collections.emptyList(java.util.Collections.emptyList) NewTopic(org.apache.kafka.clients.admin.NewTopic) Set(java.util.Set) TopicListing(org.apache.kafka.clients.admin.TopicListing) Partition(io.confluent.kafkarest.entities.Partition) PartitionReplica(io.confluent.kafkarest.entities.PartitionReplica) Collectors(java.util.stream.Collectors) List(java.util.List) Entities.checkEntityExists(io.confluent.kafkarest.controllers.Entities.checkEntityExists) Topic(io.confluent.kafkarest.entities.Topic) Optional(java.util.Optional) Node(org.apache.kafka.common.Node) Acl(io.confluent.kafkarest.entities.Acl) HashMap(java.util.HashMap) NewTopic(org.apache.kafka.clients.admin.NewTopic)

Example 3 with Topic

use of io.confluent.kafkarest.entities.Topic in project kafka-rest by confluentinc.

the class TopicManagerImplTest method getLocalTopic_existingTopic_returnsTopic.

@Test
public void getLocalTopic_existingTopic_returnsTopic() throws Exception {
    expect(clusterManager.getLocalCluster()).andReturn(completedFuture(CLUSTER));
    expect(adminClient.describeTopics(isA(Collection.class), anyObject())).andReturn(describeTopicResult);
    expect(describeTopicResult.all()).andReturn(KafkaFuture.completedFuture(createTopicDescriptionMap(TOPIC_DESCRIPTION_1)));
    replay(clusterManager, adminClient, describeTopicResult);
    Topic topic = topicManager.getLocalTopic(TOPIC_1.getName()).get().get();
    assertEquals(TOPIC_1, topic);
}
Also used : Collection(java.util.Collection) Topic(io.confluent.kafkarest.entities.Topic) NewTopic(org.apache.kafka.clients.admin.NewTopic) Test(org.junit.jupiter.api.Test)

Example 4 with Topic

use of io.confluent.kafkarest.entities.Topic in project kafka-rest by confluentinc.

the class TopicManagerImplTest method getTopic_existingTopic_returnsTopic.

@Test
public void getTopic_existingTopic_returnsTopic() throws Exception {
    expect(clusterManager.getCluster(CLUSTER_ID)).andReturn(completedFuture(Optional.of(CLUSTER)));
    expect(adminClient.describeTopics(isA(Collection.class), anyObject())).andReturn(describeTopicResult);
    expect(describeTopicResult.all()).andReturn(KafkaFuture.completedFuture(createTopicDescriptionMap(TOPIC_DESCRIPTION_1)));
    replay(clusterManager, adminClient, describeTopicResult);
    Topic topic = topicManager.getTopic(CLUSTER_ID, TOPIC_1.getName()).get().get();
    assertEquals(TOPIC_1, topic);
}
Also used : Collection(java.util.Collection) Topic(io.confluent.kafkarest.entities.Topic) NewTopic(org.apache.kafka.clients.admin.NewTopic) Test(org.junit.jupiter.api.Test)

Example 5 with Topic

use of io.confluent.kafkarest.entities.Topic in project kafka-rest by confluentinc.

the class TopicsResource method getTopic.

@GET
@Path("/{topic}")
@PerformanceMetric("topic.get+v2")
@ResourceName("api.v2.topics.get")
public void getTopic(@Suspended AsyncResponse asyncResponse, @PathParam("topic") String topicName) {
    TopicManager topicManager = topicManagerProvider.get();
    TopicConfigManager topicConfigManager = topicConfigManagerProvider.get();
    CompletableFuture<Topic> topicFuture = topicManager.getLocalTopic(topicName).thenApply(topic -> topic.orElseThrow(Errors::topicNotFoundException));
    CompletableFuture<GetTopicResponse> response = topicFuture.thenCompose(topic -> topicConfigManager.listTopicConfigs(topic.getClusterId(), topicName)).thenCombine(topicFuture, (configs, topic) -> GetTopicResponse.fromTopic(topic, configs));
    AsyncResponses.asyncResume(asyncResponse, response);
}
Also used : PathParam(javax.ws.rs.PathParam) AsyncResponses(io.confluent.kafkarest.resources.AsyncResponses) Provider(javax.inject.Provider) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) GetTopicResponse(io.confluent.kafkarest.entities.v2.GetTopicResponse) AsyncResponse(javax.ws.rs.container.AsyncResponse) Path(javax.ws.rs.Path) Versions(io.confluent.kafkarest.Versions) ResourceName(io.confluent.kafkarest.extension.ResourceAccesslistFeature.ResourceName) CompletableFuture(java.util.concurrent.CompletableFuture) PerformanceMetric(io.confluent.rest.annotations.PerformanceMetric) Collectors(java.util.stream.Collectors) Suspended(javax.ws.rs.container.Suspended) TopicConfigManager(io.confluent.kafkarest.controllers.TopicConfigManager) Inject(javax.inject.Inject) List(java.util.List) Consumes(javax.ws.rs.Consumes) Errors(io.confluent.kafkarest.Errors) TopicManager(io.confluent.kafkarest.controllers.TopicManager) Objects.requireNonNull(java.util.Objects.requireNonNull) Topic(io.confluent.kafkarest.entities.Topic) TopicManager(io.confluent.kafkarest.controllers.TopicManager) TopicConfigManager(io.confluent.kafkarest.controllers.TopicConfigManager) GetTopicResponse(io.confluent.kafkarest.entities.v2.GetTopicResponse) Topic(io.confluent.kafkarest.entities.Topic) Path(javax.ws.rs.Path) PerformanceMetric(io.confluent.rest.annotations.PerformanceMetric) ResourceName(io.confluent.kafkarest.extension.ResourceAccesslistFeature.ResourceName) GET(javax.ws.rs.GET)

Aggregations

Topic (io.confluent.kafkarest.entities.Topic)6 NewTopic (org.apache.kafka.clients.admin.NewTopic)4 Collection (java.util.Collection)3 List (java.util.List)3 Test (org.junit.jupiter.api.Test)3 TopicManager (io.confluent.kafkarest.controllers.TopicManager)2 ResourceName (io.confluent.kafkarest.extension.ResourceAccesslistFeature.ResourceName)2 PerformanceMetric (io.confluent.rest.annotations.PerformanceMetric)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Collectors (java.util.stream.Collectors)2 Inject (javax.inject.Inject)2 GET (javax.ws.rs.GET)2 TopicListing (org.apache.kafka.clients.admin.TopicListing)2 Errors (io.confluent.kafkarest.Errors)1 Versions (io.confluent.kafkarest.Versions)1 KafkaFutures (io.confluent.kafkarest.common.KafkaFutures)1 Entities.checkEntityExists (io.confluent.kafkarest.controllers.Entities.checkEntityExists)1 TopicConfigManager (io.confluent.kafkarest.controllers.TopicConfigManager)1 Acl (io.confluent.kafkarest.entities.Acl)1