Search in sources :

Example 1 with CreateTopicsResponse

use of org.apache.kafka.common.requests.CreateTopicsResponse in project kafka by apache.

the class StreamsKafkaClient method createTopics.

/**
     * Create a set of new topics using batch request.
     */
public void createTopics(final Map<InternalTopicConfig, Integer> topicsMap, final int replicationFactor, final long windowChangeLogAdditionalRetention, final MetadataResponse metadata) {
    final Map<String, CreateTopicsRequest.TopicDetails> topicRequestDetails = new HashMap<>();
    for (Map.Entry<InternalTopicConfig, Integer> entry : topicsMap.entrySet()) {
        InternalTopicConfig internalTopicConfig = entry.getKey();
        Integer partitions = entry.getValue();
        final Properties topicProperties = internalTopicConfig.toProperties(windowChangeLogAdditionalRetention);
        final Map<String, String> topicConfig = new HashMap<>();
        for (String key : topicProperties.stringPropertyNames()) {
            topicConfig.put(key, topicProperties.getProperty(key));
        }
        final CreateTopicsRequest.TopicDetails topicDetails = new CreateTopicsRequest.TopicDetails(partitions, (short) replicationFactor, topicConfig);
        topicRequestDetails.put(internalTopicConfig.name(), topicDetails);
    }
    final ClientRequest clientRequest = kafkaClient.newClientRequest(getControllerReadyBrokerId(metadata), new CreateTopicsRequest.Builder(topicRequestDetails, streamsConfig.getInt(StreamsConfig.REQUEST_TIMEOUT_MS_CONFIG)), Time.SYSTEM.milliseconds(), true);
    final ClientResponse clientResponse = sendRequest(clientRequest);
    if (!clientResponse.hasResponse()) {
        throw new StreamsException("Empty response for client request.");
    }
    if (!(clientResponse.responseBody() instanceof CreateTopicsResponse)) {
        throw new StreamsException("Inconsistent response type for internal topic creation request. " + "Expected CreateTopicsResponse but received " + clientResponse.responseBody().getClass().getName());
    }
    final CreateTopicsResponse createTopicsResponse = (CreateTopicsResponse) clientResponse.responseBody();
    for (InternalTopicConfig internalTopicConfig : topicsMap.keySet()) {
        CreateTopicsResponse.Error error = createTopicsResponse.errors().get(internalTopicConfig.name());
        if (!error.is(Errors.NONE) && !error.is(Errors.TOPIC_ALREADY_EXISTS)) {
            throw new StreamsException("Could not create topic: " + internalTopicConfig.name() + " due to " + error.messageWithFallback());
        }
    }
}
Also used : ClientResponse(org.apache.kafka.clients.ClientResponse) CreateTopicsResponse(org.apache.kafka.common.requests.CreateTopicsResponse) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) StreamsException(org.apache.kafka.streams.errors.StreamsException) Properties(java.util.Properties) CreateTopicsRequest(org.apache.kafka.common.requests.CreateTopicsRequest) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ClientRequest(org.apache.kafka.clients.ClientRequest)

Aggregations

HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 ClientRequest (org.apache.kafka.clients.ClientRequest)1 ClientResponse (org.apache.kafka.clients.ClientResponse)1 CreateTopicsRequest (org.apache.kafka.common.requests.CreateTopicsRequest)1 CreateTopicsResponse (org.apache.kafka.common.requests.CreateTopicsResponse)1 StreamsException (org.apache.kafka.streams.errors.StreamsException)1