Search in sources :

Example 1 with TopicListing

use of org.apache.kafka.clients.admin.TopicListing in project apache-kafka-on-k8s by banzaicloud.

the class ClientCompatibilityTest method testAdminClient.

void testAdminClient() throws Throwable {
    Properties adminProps = new Properties();
    adminProps.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, testConfig.bootstrapServer);
    try (final AdminClient client = AdminClient.create(adminProps)) {
        while (true) {
            Collection<Node> nodes = client.describeCluster().nodes().get();
            if (nodes.size() == testConfig.numClusterNodes) {
                break;
            } else if (nodes.size() > testConfig.numClusterNodes) {
                throw new KafkaException("Expected to see " + testConfig.numClusterNodes + " nodes, but saw " + nodes.size());
            }
            Thread.sleep(1);
            log.info("Saw only {} cluster nodes.  Waiting to see {}.", nodes.size(), testConfig.numClusterNodes);
        }
        tryFeature("createTopics", testConfig.createTopicsSupported, new Invoker() {

            @Override
            public void invoke() throws Throwable {
                try {
                    client.createTopics(Collections.singleton(new NewTopic("newtopic", 1, (short) 1))).all().get();
                } catch (ExecutionException e) {
                    throw e.getCause();
                }
            }
        }, new ResultTester() {

            @Override
            public void test() throws Throwable {
                while (true) {
                    try {
                        client.describeTopics(Collections.singleton("newtopic")).all().get();
                        break;
                    } catch (ExecutionException e) {
                        if (e.getCause() instanceof UnknownTopicOrPartitionException)
                            continue;
                        throw e;
                    }
                }
            }
        });
        while (true) {
            Collection<TopicListing> listings = client.listTopics().listings().get();
            if (!testConfig.createTopicsSupported)
                break;
            boolean foundNewTopic = false;
            for (TopicListing listing : listings) {
                if (listing.name().equals("newtopic")) {
                    if (listing.isInternal())
                        throw new KafkaException("Did not expect newtopic to be an internal topic.");
                    foundNewTopic = true;
                }
            }
            if (foundNewTopic)
                break;
            Thread.sleep(1);
            log.info("Did not see newtopic.  Retrying listTopics...");
        }
        tryFeature("describeAclsSupported", testConfig.describeAclsSupported, new Invoker() {

            @Override
            public void invoke() throws Throwable {
                try {
                    client.describeAcls(AclBindingFilter.ANY).values().get();
                } catch (ExecutionException e) {
                    if (e.getCause() instanceof SecurityDisabledException)
                        return;
                    throw e.getCause();
                }
            }
        });
    }
}
Also used : Node(org.apache.kafka.common.Node) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) Properties(java.util.Properties) TopicListing(org.apache.kafka.clients.admin.TopicListing) KafkaException(org.apache.kafka.common.KafkaException) NewTopic(org.apache.kafka.clients.admin.NewTopic) ExecutionException(java.util.concurrent.ExecutionException) SecurityDisabledException(org.apache.kafka.common.errors.SecurityDisabledException) AdminClient(org.apache.kafka.clients.admin.AdminClient)

Example 2 with TopicListing

use of org.apache.kafka.clients.admin.TopicListing in project eventapis by kloiasoft.

the class Eventapis method main.

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);
}
Also used : HashMap(java.util.HashMap) TopicListing(org.apache.kafka.clients.admin.TopicListing) Map(java.util.Map) HashMap(java.util.HashMap) AdminClient(org.apache.kafka.clients.admin.AdminClient)

Example 3 with TopicListing

use of org.apache.kafka.clients.admin.TopicListing in project kafka by apache.

the class WorkerUtils method getMatchingTopicPartitions.

/**
 * Returns list of existing, not internal, topics/partitions that match given pattern and
 * where partitions are in range [startPartition, endPartition]
 * @param adminClient     AdminClient
 * @param topicRegex      Topic regular expression to match
 * @return                List of topic names
 * @throws Throwable      If failed to get list of existing topics
 */
static Collection<TopicPartition> getMatchingTopicPartitions(Admin adminClient, String topicRegex, int startPartition, int endPartition) throws Throwable {
    final Pattern topicNamePattern = Pattern.compile(topicRegex);
    // first get list of matching topics
    List<String> matchedTopics = new ArrayList<>();
    ListTopicsResult res = adminClient.listTopics(new ListTopicsOptions().timeoutMs(ADMIN_REQUEST_TIMEOUT));
    Map<String, TopicListing> topicListingMap = res.namesToListings().get();
    for (Map.Entry<String, TopicListing> topicListingEntry : topicListingMap.entrySet()) {
        if (!topicListingEntry.getValue().isInternal() && topicNamePattern.matcher(topicListingEntry.getKey()).matches()) {
            matchedTopics.add(topicListingEntry.getKey());
        }
    }
    // create a list of topic/partitions
    List<TopicPartition> out = new ArrayList<>();
    DescribeTopicsResult topicsResult = adminClient.describeTopics(matchedTopics, new DescribeTopicsOptions().timeoutMs(ADMIN_REQUEST_TIMEOUT));
    Map<String, TopicDescription> topicDescriptionMap = topicsResult.allTopicNames().get();
    for (TopicDescription desc : topicDescriptionMap.values()) {
        List<TopicPartitionInfo> partitions = desc.partitions();
        for (TopicPartitionInfo info : partitions) {
            if ((info.partition() >= startPartition) && (info.partition() <= endPartition)) {
                out.add(new TopicPartition(desc.name(), info.partition()));
            }
        }
    }
    return out;
}
Also used : Pattern(java.util.regex.Pattern) ArrayList(java.util.ArrayList) TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) TopicPartition(org.apache.kafka.common.TopicPartition) ListTopicsResult(org.apache.kafka.clients.admin.ListTopicsResult) TopicListing(org.apache.kafka.clients.admin.TopicListing) DescribeTopicsResult(org.apache.kafka.clients.admin.DescribeTopicsResult) DescribeTopicsOptions(org.apache.kafka.clients.admin.DescribeTopicsOptions) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) ListTopicsOptions(org.apache.kafka.clients.admin.ListTopicsOptions) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with TopicListing

use of org.apache.kafka.clients.admin.TopicListing in project kafka by apache.

the class ClientCompatibilityTest method testAdminClient.

void testAdminClient() throws Throwable {
    Properties adminProps = new Properties();
    adminProps.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, testConfig.bootstrapServer);
    try (final Admin client = Admin.create(adminProps)) {
        while (true) {
            Collection<Node> nodes = client.describeCluster().nodes().get();
            if (nodes.size() == testConfig.numClusterNodes) {
                break;
            } else if (nodes.size() > testConfig.numClusterNodes) {
                throw new KafkaException("Expected to see " + testConfig.numClusterNodes + " nodes, but saw " + nodes.size());
            }
            Thread.sleep(1);
            log.info("Saw only {} cluster nodes.  Waiting to see {}.", nodes.size(), testConfig.numClusterNodes);
        }
        testDescribeConfigsMethod(client);
        tryFeature("createTopics", testConfig.createTopicsSupported, () -> {
            try {
                client.createTopics(Collections.singleton(new NewTopic("newtopic", 1, (short) 1))).all().get();
            } catch (ExecutionException e) {
                throw e.getCause();
            }
        }, () -> createTopicsResultTest(client, Collections.singleton("newtopic")));
        while (true) {
            Collection<TopicListing> listings = client.listTopics().listings().get();
            if (!testConfig.createTopicsSupported)
                break;
            if (topicExists(listings, "newtopic"))
                break;
            Thread.sleep(1);
            log.info("Did not see newtopic.  Retrying listTopics...");
        }
        tryFeature("describeAclsSupported", testConfig.describeAclsSupported, () -> {
            try {
                client.describeAcls(AclBindingFilter.ANY).values().get();
            } catch (ExecutionException e) {
                if (e.getCause() instanceof SecurityDisabledException)
                    return;
                throw e.getCause();
            }
        });
    }
}
Also used : Node(org.apache.kafka.common.Node) TopicListing(org.apache.kafka.clients.admin.TopicListing) KafkaException(org.apache.kafka.common.KafkaException) NewTopic(org.apache.kafka.clients.admin.NewTopic) Properties(java.util.Properties) Admin(org.apache.kafka.clients.admin.Admin) ExecutionException(java.util.concurrent.ExecutionException) SecurityDisabledException(org.apache.kafka.common.errors.SecurityDisabledException)

Example 5 with TopicListing

use of org.apache.kafka.clients.admin.TopicListing in project eventapis by kloiasoft.

the class ListTopicSchedule method runInternal.

@Override
boolean runInternal(StopWatch stopWatch) throws InterruptedException, ExecutionException {
    stopWatch.start("adminClient.listTopics()");
    Collection<String> topicNames = adminClient.listTopics().listings().get().stream().map(TopicListing::name).filter(this::shouldCollectEvent).collect(Collectors.toList());
    topicsMap.removeAll(new RemoveTopicPredicate(topicNames));
    DescribeTopicsResult describeTopicsResult = adminClient.describeTopics(topicNames);
    describeTopicsResult.all().get().forEach((topic, topicDescription) -> topicsMap.executeOnKey(topic, new SetTopicPartitionsProcessor(topicDescription.partitions().stream().map(TopicPartitionInfo::partition).collect(Collectors.toList()))));
    metaMap.set(this.getName() + TopicServiceScheduler.LAST_SUCCESS_PREFIX, System.currentTimeMillis());
    log.debug("Topics:" + topicsMap.entrySet());
    log.debug(stopWatch.prettyPrint());
    return true;
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) TopicListing(org.apache.kafka.clients.admin.TopicListing) DescribeTopicsResult(org.apache.kafka.clients.admin.DescribeTopicsResult)

Aggregations

TopicListing (org.apache.kafka.clients.admin.TopicListing)5 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Properties (java.util.Properties)2 ExecutionException (java.util.concurrent.ExecutionException)2 AdminClient (org.apache.kafka.clients.admin.AdminClient)2 DescribeTopicsResult (org.apache.kafka.clients.admin.DescribeTopicsResult)2 NewTopic (org.apache.kafka.clients.admin.NewTopic)2 KafkaException (org.apache.kafka.common.KafkaException)2 Node (org.apache.kafka.common.Node)2 TopicPartitionInfo (org.apache.kafka.common.TopicPartitionInfo)2 SecurityDisabledException (org.apache.kafka.common.errors.SecurityDisabledException)2 ArrayList (java.util.ArrayList)1 Pattern (java.util.regex.Pattern)1 Admin (org.apache.kafka.clients.admin.Admin)1 DescribeTopicsOptions (org.apache.kafka.clients.admin.DescribeTopicsOptions)1 ListTopicsOptions (org.apache.kafka.clients.admin.ListTopicsOptions)1 ListTopicsResult (org.apache.kafka.clients.admin.ListTopicsResult)1 TopicDescription (org.apache.kafka.clients.admin.TopicDescription)1 TopicPartition (org.apache.kafka.common.TopicPartition)1