Search in sources :

Example 66 with AdminClient

use of org.apache.kafka.clients.admin.AdminClient in project flink by apache.

the class KafkaTableTestBase method createTestTopic.

public void createTestTopic(String topic, int numPartitions, int replicationFactor) {
    Map<String, Object> properties = new HashMap<>();
    properties.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, getBootstrapServers());
    try (AdminClient admin = AdminClient.create(properties)) {
        admin.createTopics(Collections.singletonList(new NewTopic(topic, numPartitions, (short) replicationFactor)));
    }
}
Also used : HashMap(java.util.HashMap) NewTopic(org.apache.kafka.clients.admin.NewTopic) AdminClient(org.apache.kafka.clients.admin.AdminClient)

Example 67 with AdminClient

use of org.apache.kafka.clients.admin.AdminClient in project flink by apache.

the class KafkaEnumeratorTest method testDiscoverPartitionsPeriodically.

@Test(timeout = 30000L)
public void testDiscoverPartitionsPeriodically() throws Throwable {
    try (MockSplitEnumeratorContext<KafkaPartitionSplit> context = new MockSplitEnumeratorContext<>(NUM_SUBTASKS);
        KafkaSourceEnumerator enumerator = createEnumerator(context, ENABLE_PERIODIC_PARTITION_DISCOVERY, INCLUDE_DYNAMIC_TOPIC);
        AdminClient adminClient = KafkaSourceTestEnv.getAdminClient()) {
        startEnumeratorAndRegisterReaders(context, enumerator);
        // invoke partition discovery callable again and there should be no new assignments.
        runPeriodicPartitionDiscovery(context);
        assertEquals("No assignments should be made because there is no partition change", 2, context.getSplitsAssignmentSequence().size());
        // create the dynamic topic.
        adminClient.createTopics(Collections.singleton(new NewTopic(DYNAMIC_TOPIC_NAME, NUM_PARTITIONS_DYNAMIC_TOPIC, (short) 1))).all().get();
        // invoke partition discovery callable again.
        while (true) {
            runPeriodicPartitionDiscovery(context);
            if (context.getSplitsAssignmentSequence().size() < 3) {
                Thread.sleep(10);
            } else {
                break;
            }
        }
        verifyLastReadersAssignments(context, Arrays.asList(READER0, READER1), Collections.singleton(DYNAMIC_TOPIC_NAME), 3);
    } finally {
        try (AdminClient adminClient = KafkaSourceTestEnv.getAdminClient()) {
            adminClient.deleteTopics(Collections.singleton(DYNAMIC_TOPIC_NAME)).all().get();
        } catch (Exception e) {
        // Let it go.
        }
    }
}
Also used : KafkaPartitionSplit(org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit) MockSplitEnumeratorContext(org.apache.flink.api.connector.source.mocks.MockSplitEnumeratorContext) NewTopic(org.apache.kafka.clients.admin.NewTopic) AdminClient(org.apache.kafka.clients.admin.AdminClient) Test(org.junit.Test)

Example 68 with AdminClient

use of org.apache.kafka.clients.admin.AdminClient in project debezium by debezium.

the class KafkaDatabaseHistory method initializeStorage.

@Override
public void initializeStorage() {
    super.initializeStorage();
    try (AdminClient admin = AdminClient.create(this.producerConfig.asProperties())) {
        // Find default replication factor
        Config brokerConfig = getKafkaBrokerConfig(admin);
        final short replicationFactor = Short.parseShort(brokerConfig.get(DEFAULT_TOPIC_REPLICATION_FACTOR_PROP_NAME).value());
        // Create topic
        final NewTopic topic = new NewTopic(topicName, (short) 1, replicationFactor);
        topic.configs(Collect.hashMapOf("cleanup.policy", "delete", "retention.ms", Long.toString(Long.MAX_VALUE), "retention.bytes", "-1"));
        admin.createTopics(Collections.singleton(topic));
        logger.info("Database history topic '{}' created", topic);
    } catch (Exception e) {
        throw new ConnectException("Creation of database history topic failed, please create the topic manually", e);
    }
}
Also used : Config(org.apache.kafka.clients.admin.Config) ProducerConfig(org.apache.kafka.clients.producer.ProducerConfig) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) NewTopic(org.apache.kafka.clients.admin.NewTopic) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ConnectException(org.apache.kafka.connect.errors.ConnectException) AdminClient(org.apache.kafka.clients.admin.AdminClient) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 69 with AdminClient

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

the class AutomaticTopicConfiguration method init.

@PostConstruct
public void init() {
    AdminClient adminClient = adminClient();
    try {
        StopWatch stopWatch = new StopWatch("CheckAndCreateTopics");
        stopWatch.start("CheckAndCreateTopics");
        ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
        provider.addIncludeFilter(new AssignableTypeFilter(PublishedEvent.class));
        // provider.addExcludeFilter(new AssignableTypeFilter(ReceivedEvent.class));
        Set<BeanDefinition> candidateComponents = provider.findCandidateComponents(eventApisConfiguration.getBaseEventsPackage());
        int numberOfNodes = 1;
        try {
            Collection<Node> nodes = adminClient.describeCluster().nodes().get();
            numberOfNodes = nodes.size();
        } catch (InterruptedException | ExecutionException e) {
            log.warn("Error while finding number of Nodes:" + e.getMessage(), e);
        }
        for (BeanDefinition candidateComponent : candidateComponents) {
            Class<PublishedEvent> beanClass;
            try {
                beanClass = (Class<PublishedEvent>) Class.forName(candidateComponent.getBeanClassName());
                String topicName = beanClass.getSimpleName();
                log.info("Candidate {} to Create Topic:", topicName);
                try {
                    adminClient.describeTopics(Collections.singleton(topicName)).all().get();
                } catch (UnknownTopicOrPartitionException | ExecutionException exception) {
                    if (!(exception.getCause() instanceof UnknownTopicOrPartitionException))
                        throw exception;
                    log.warn("Topic {} does not exists, trying to create", topicName);
                    try {
                        adminClient.createTopics(Collections.singleton(new NewTopic(topicName, numberOfNodes, (short) 1)));
                        log.info("Topic {} is Created Successfully:", topicName);
                    } catch (Exception topicCreationEx) {
                        log.warn("Error while creating Topic:" + topicCreationEx.getMessage(), topicCreationEx);
                    }
                }
            } catch (ClassNotFoundException | InterruptedException | ExecutionException exception) {
                log.warn("Error while checking Topic:" + candidateComponent.toString() + " message: " + exception.getMessage(), exception);
            }
        }
        stopWatch.stop();
        log.debug(stopWatch.prettyPrint());
    } finally {
        adminClient.close();
    }
}
Also used : Node(org.apache.kafka.common.Node) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) PublishedEvent(com.kloia.eventapis.common.PublishedEvent) ExecutionException(java.util.concurrent.ExecutionException) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) StopWatch(org.springframework.util.StopWatch) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) NewTopic(org.apache.kafka.clients.admin.NewTopic) ExecutionException(java.util.concurrent.ExecutionException) AdminClient(org.apache.kafka.clients.admin.AdminClient) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter) PostConstruct(javax.annotation.PostConstruct)

Example 70 with AdminClient

use of org.apache.kafka.clients.admin.AdminClient in project drill by apache.

the class TestKafkaSuit method createTopicHelper.

public static void createTopicHelper(String topicName, int partitions) throws ExecutionException, InterruptedException {
    try (AdminClient adminClient = initAdminClient()) {
        NewTopic newTopic = new NewTopic(topicName, partitions, (short) 1);
        Map<String, String> topicConfigs = new HashMap<>();
        topicConfigs.put(TopicConfig.MESSAGE_TIMESTAMP_TYPE_CONFIG, "CreateTime");
        topicConfigs.put(TopicConfig.RETENTION_MS_CONFIG, "-1");
        newTopic.configs(topicConfigs);
        CreateTopicsResult result = adminClient.createTopics(Collections.singletonList(newTopic));
        result.all().get();
    }
}
Also used : CreateTopicsResult(org.apache.kafka.clients.admin.CreateTopicsResult) HashMap(java.util.HashMap) NewTopic(org.apache.kafka.clients.admin.NewTopic) AdminClient(org.apache.kafka.clients.admin.AdminClient)

Aggregations

AdminClient (org.apache.kafka.clients.admin.AdminClient)71 Test (org.junit.Test)38 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)31 NewTopic (org.apache.kafka.clients.admin.NewTopic)30 StreamsConfig (org.apache.kafka.streams.StreamsConfig)29 MockAdminClient (org.apache.kafka.clients.admin.MockAdminClient)27 HashMap (java.util.HashMap)24 TopicMetadataAndConfig (org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig)18 TopicDescription (org.apache.kafka.clients.admin.TopicDescription)18 Config (org.apache.kafka.clients.admin.Config)15 Map (java.util.Map)14 ConsumerConfig (org.apache.kafka.clients.consumer.ConsumerConfig)14 ProducerConfig (org.apache.kafka.clients.producer.ProducerConfig)14 TopicConfig (org.apache.kafka.common.config.TopicConfig)13 MockTime (org.apache.kafka.common.utils.MockTime)13 TopicExistsException (org.apache.kafka.common.errors.TopicExistsException)11 ArrayList (java.util.ArrayList)10 TopicPartitionInfo (org.apache.kafka.common.TopicPartitionInfo)10 ConfigResource (org.apache.kafka.common.config.ConfigResource)10 UnknownTopicOrPartitionException (org.apache.kafka.common.errors.UnknownTopicOrPartitionException)10