Search in sources :

Example 11 with Config

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

the class KafkaTopicClientImpl method topicConfig.

private Map<String, String> topicConfig(final String topicName, final boolean includeDefaults) {
    final ConfigResource resource = new ConfigResource(ConfigResource.Type.TOPIC, topicName);
    final List<ConfigResource> request = Collections.singletonList(resource);
    try {
        final Config config = executeWithRetries(() -> adminClient.describeConfigs(request).all()).get(resource);
        return config.entries().stream().filter(e -> includeDefaults || e.source().equals(ConfigEntry.ConfigSource.DYNAMIC_TOPIC_CONFIG)).collect(Collectors.toMap(ConfigEntry::name, ConfigEntry::value));
    } catch (final Exception e) {
        throw new KafkaResponseGetFailedException("Failed to get config for Kafka Topic " + topicName, e);
    }
}
Also used : Config(org.apache.kafka.clients.admin.Config) KafkaTopicException(io.confluent.ksql.exception.KafkaTopicException) LoggerFactory(org.slf4j.LoggerFactory) ConfigEntry(org.apache.kafka.clients.admin.ConfigEntry) RetriableException(org.apache.kafka.common.errors.RetriableException) Supplier(java.util.function.Supplier) AdminClient(org.apache.kafka.clients.admin.AdminClient) ConfigResource(org.apache.kafka.common.config.ConfigResource) Lists(com.google.common.collect.Lists) Map(java.util.Map) DeleteTopicsResult(org.apache.kafka.clients.admin.DeleteTopicsResult) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) TopicConfig(org.apache.kafka.common.config.TopicConfig) Logger(org.slf4j.Logger) Collection(java.util.Collection) NewTopic(org.apache.kafka.clients.admin.NewTopic) Set(java.util.Set) KafkaFuture(org.apache.kafka.common.KafkaFuture) KafkaResponseGetFailedException(io.confluent.ksql.exception.KafkaResponseGetFailedException) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) List(java.util.List) Node(org.apache.kafka.common.Node) DescribeClusterResult(org.apache.kafka.clients.admin.DescribeClusterResult) Collections(java.util.Collections) Config(org.apache.kafka.clients.admin.Config) TopicConfig(org.apache.kafka.common.config.TopicConfig) KafkaResponseGetFailedException(io.confluent.ksql.exception.KafkaResponseGetFailedException) ConfigResource(org.apache.kafka.common.config.ConfigResource) KafkaTopicException(io.confluent.ksql.exception.KafkaTopicException) RetriableException(org.apache.kafka.common.errors.RetriableException) KafkaResponseGetFailedException(io.confluent.ksql.exception.KafkaResponseGetFailedException) ExecutionException(java.util.concurrent.ExecutionException) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException)

Example 12 with Config

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

the class KafkaDatabaseHistory method getKafkaBrokerConfig.

private Config getKafkaBrokerConfig(AdminClient admin) throws Exception {
    final Collection<Node> nodes = admin.describeCluster().nodes().get(KAFKA_QUERY_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
    if (nodes.isEmpty()) {
        throw new ConnectException("No brokers available to obtain default settings");
    }
    String nodeId = nodes.iterator().next().idString();
    Set<ConfigResource> resources = Collections.singleton(new ConfigResource(ConfigResource.Type.BROKER, nodeId));
    final Map<ConfigResource, Config> configs = admin.describeConfigs(resources).all().get(KAFKA_QUERY_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
    if (configs.isEmpty()) {
        throw new ConnectException("No configs have been received");
    }
    return configs.values().iterator().next();
}
Also used : Config(org.apache.kafka.clients.admin.Config) ProducerConfig(org.apache.kafka.clients.producer.ProducerConfig) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) Node(org.apache.kafka.common.Node) ConfigResource(org.apache.kafka.common.config.ConfigResource) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 13 with Config

use of org.apache.kafka.clients.admin.Config 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)

Aggregations

Config (org.apache.kafka.clients.admin.Config)13 ConfigResource (org.apache.kafka.common.config.ConfigResource)10 Map (java.util.Map)7 ConfigEntry (org.apache.kafka.clients.admin.ConfigEntry)7 TopicConfig (org.apache.kafka.common.config.TopicConfig)6 ExecutionException (java.util.concurrent.ExecutionException)5 NewTopic (org.apache.kafka.clients.admin.NewTopic)5 Node (org.apache.kafka.common.Node)5 AdminClient (org.apache.kafka.clients.admin.AdminClient)4 TopicDescription (org.apache.kafka.clients.admin.TopicDescription)4 KafkaResponseGetFailedException (io.confluent.ksql.exception.KafkaResponseGetFailedException)3 KafkaTopicException (io.confluent.ksql.exception.KafkaTopicException)3 Collections (java.util.Collections)3 HashMap (java.util.HashMap)3 Set (java.util.Set)3 DescribeClusterResult (org.apache.kafka.clients.admin.DescribeClusterResult)3 KafkaFuture (org.apache.kafka.common.KafkaFuture)3 RetriableException (org.apache.kafka.common.errors.RetriableException)3 TopicExistsException (org.apache.kafka.common.errors.TopicExistsException)3 Logger (org.slf4j.Logger)3