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

The following examples show how to use org.apache.kafka.clients.admin.TopicListing. 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: Eventapis.java    From eventapis with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws ExecutionException, InterruptedException {
        Map props = new HashMap<>();
        // list of host:port pairs used for establishing the initial connections
        // to the Kakfa cluster
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,
                "kafka-local:9092");
//        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
//                JsonSerializer.class);
//        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
//                JsonSerializer.class);
        // value to block, after which it will throw a TimeoutException
        props.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 50000);
        AdminClient adminClient = AdminClient.create(props);
        adminClient.describeCluster();
        Collection<TopicListing> topicListings = adminClient.listTopics().listings().get();
        System.out.println(topicListings);
    }
 
Example #2
Source File: TopicAdmin.java    From kafka-message-tool with MIT License 5 votes vote down vote up
public Set<ClusterTopicInfo> describeTopics() throws InterruptedException, ExecutionException, TimeoutException {

        Set<ClusterTopicInfo> result = new HashSet<>();
        final ListTopicsResult listTopicsResult = kafkaClientsAdminClient.listTopics(new ListTopicsOptions().listInternal(false));
        final Collection<TopicListing> listings = listTopicsResult.listings().get(ApplicationConstants.FUTURE_GET_TIMEOUT_MS, TimeUnit.MILLISECONDS);
        Logger.debug(String.format("describeTopics.listings %s", listings));


        final Set<String> topicNames = listTopicsResult.names().get(ApplicationConstants.FUTURE_GET_TIMEOUT_MS, TimeUnit.MILLISECONDS);
        final DescribeTopicsResult describeTopicsResult = kafkaClientsAdminClient.describeTopics(topicNames);
        final Map<String, TopicDescription> stringTopicDescriptionMap = describeTopicsResult.all().get(ApplicationConstants.FUTURE_GET_TIMEOUT_MS,
                                                                                                       TimeUnit.MILLISECONDS);

        for (Map.Entry<String, TopicDescription> entry : stringTopicDescriptionMap.entrySet()) {
            final TopicDescription topicDescription = entry.getValue();
            final ClusterTopicInfo clusterTopicInfo = new ClusterTopicInfo(topicDescription.name(),
                                                                           topicDescription.partitions(),
                                                                           getConfigEntriesForTopic(topicDescription.name()));
            result.add(clusterTopicInfo);
        }
        return result;

    }
 
Example #3
Source File: KafkaTestUtils.java    From kafka-junit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Get information about all topics in Kafka.
 * @return Set of topics found in Kafka.
 */
public List<TopicListing> getTopics() {
    try (final AdminClient adminClient = getAdminClient()) {
        return new ArrayList<>(adminClient.listTopics().listings().get());
    } catch (final InterruptedException | ExecutionException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
Example #4
Source File: KafkaAdminClientTest.java    From elasticactors with Apache License 2.0 5 votes vote down vote up
@Test(enabled = false)
public void testListTopics() throws Exception {
    Map<String, Object> adminClientConfig = new HashMap<>();
    adminClientConfig.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "api.local.getbux.com:32400,api.local.getbux.com:32401,api.local.getbux.com:32402");
    adminClientConfig.put(AdminClientConfig.CLIENT_ID_CONFIG, "test-adminClient");
    AdminClient adminClient = AdminClient.create(adminClientConfig);

    // ensure all the topics are created
    ListTopicsResult listTopicsResult = adminClient.listTopics();
    Map<String, TopicListing> availableTopics = listTopicsResult.namesToListings().get();

    assertNotNull(availableTopics);
}